Here is a description of the problem.
I have a library that needs an initialization function to be called before its APIs can be used. Instead of forcing application to call this initialization function, I would like to plug the init function into .pinit (actually .init_array). This way the init function will be called before main. To achieve this I added a C++ file to my application withe the following symbol
#pragma RETAIN
volatile int PINIT_FXN = foo_init();
My problem is that the symbol is not retained in the application. Any ideas on how to achieve this. Please not that I have a library that contains this symbol. How do I inform the linker without adding any linker options to the application? I want this function to be added to the application if my library is "used".
Also I would prefer a method that would work across compilers.
I am using the MSP430 compiler.
Thanks,
Nitya Ramdas
Even if the section containing foo_init is retained, that does not mean foo_init gets called before main runs. There is no standard way to arrange for a user function to be called before main.
Thanks and regards,
-George
TI C/C++ Compiler Forum ModeratorPlease click Verify Answer on the best reply to your question.The Compiler Wiki answers most common questions.Track an issue with SDOWP. Enter your bug id in the "Find Record ID" box.
I already have an init.cpp file which causse foo_init to get plugged into the .pinit table. All I need is a way to tell the linker to retain foo_init().
#pragma RETAIN deos not appera to work...at least on the MSP430 compiler that I am using.
Nitya
The object file containing PINIT_FXN is not being pulled in from the library because PINIT_FXN is not referenced. You could:
a) move the definition of PINIT_FXN to the file that implements the API;b) add some kind of reference from the API to PINIT_FXN;c) use -u PINIT_FXN on the linker command line to synthesize a reference
Note that #pragma RETAIN (or its sibling --retain) does not force inclusion from a library; it only prevents the linker from *throwing away* unreferenced objects once they are included.
-Alan
I am using option c) right now, but I try a) and b).
So what is wrong with
void main() { foo_init(); /* existing main() statements here */ }