NO

Author Topic: #pragma startup  (Read 5715 times)

Anonymous

  • Guest
#pragma startup
« on: December 25, 2004, 12:37:54 AM »
New problem.... :cry:

I have a lib under developement.  In the library I am using #pragma startup UseFancyExceptionHandler() as a startup routine to install an alternative unhandled exception filter.  The code is simple enough...

Code: [Select]

void _cdecl UseFancyExceptionHandler(void)
{ oldfilter = SetUnhandledExceptionFilter(&NewExceptHandler); }


However, since this routine is never explicitly called, it doesn't get linked into the final executable unless I call some other routine from the same library in my code.  Result... the new exception handler isn't installed.

I can manually install it as the first call inside Main or WinMain and it works perfectly... but it won't install reliably from the #prama method.

Now since a lot of startup routines (exception handlers, background tasks, etc.) may not actually be called directly from within a program, this could be a real problem...

Am I missing something?   Shouldn't #pragma startup be exempt from smart linking and optomization rules?

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
#pragma startup
« Reply #1 on: December 27, 2004, 05:06:21 PM »
It's not optimized away - it can't be!

I suspect it's not making it into the object file. Note that a prototype or definition for function must preceed a #pragma function - otherwise you will just get a warning, and noting is emitted to the object file. A #pragma is very compiler specific, so the compiler will never produce a severe error for #pragma's (would prevent compiling source files from other compilers). Also note that #pragma startup requires support from the runtime library, so it *will* work with the Pelles C runtime, but *might* work with the Microsoft runtime.

Pelle
/Pelle

Anonymous

  • Guest
#pragma startup
« Reply #2 on: December 27, 2004, 05:20:32 PM »
Quote from: "Pelle"
It's not optimized away - it can't be!

I suspect it's not making it into the object file. Note that a prototype or definition for function must preceed a #pragma function - otherwise you will just get a warning, and noting is emitted to the object file.



A hex view of the LIB file indicates the routine is there.  Also I can call it in my programs without any problems...  It's only in the one specific situation where it doesn't work....

1) Where the header is used in a program
2) There is no call to any other function in the library.

It is the situation where I just want the enhanced error reporting (exception code, program address, string description in a message box) that's not working.  If I reference even one of the functions in the library, it all gets hooked up and works properly and, of course, if I call the function explicitly it works.  

It's not really a big deal (like the exception keyword trashing pointers). I can always just call the thing as the first step in my main and winmain functions.... It's just that the #pragma method gets it into the program startup earler in the process...  :lol: before I have the chance to make any mistakes.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
#pragma startup
« Reply #3 on: December 27, 2004, 05:46:15 PM »
It depends on where you put the #pragma startup. If it's put in a module inside a library, and there are no references to this module, it will not be pulled in 'automagically'. The linker will not include a module from a library just because it contains a #pragma startup.

Maybe if you post a sample project, it will be easier to understand the problem. I don't think there is a general problem with #pragma startup.

Pelle
/Pelle

Anonymous

  • Guest
#pragma startup
« Reply #4 on: December 27, 2004, 10:27:50 PM »
Quote from: "Pelle"
It depends on where you put the #pragma startup.


 :oops: I am such a "noob" at times... I just solved this problem.  All I did was to move the #pragma startup from the top of the .c source code file for the library to the bottom of it's .h header file...

Works perfectly every time now...

Sorry about that...