Make a call using the gcc constructor attribute

__attribute__ ((constructor)) void construct() { // Инициализация } 

fails because of the -nostdlib flag. Is there any other way (library - pure C)?

    1 answer 1

    Here is exactly your question. How to run constructor even if “-nostdlib” option is defined . True, the answers are disappointing.

    And the dlopen documentation says the following:

    The obsolete symbols _init () and _fini ()

    The linker recognizes special symbols _init and _fini . If this is a custom library _init , it was executed after the dlopen() , before dlopen() returns. If the library is _fini , it is called In this case, it can be done by giving the gcc parameter "the -nostartfiles" parameter on the command line.

    Using these routines, or the gcc -nostartfiles or -nostdlib options, is not recommended. It has been noted that since it has been adopted, it has been the case that it has been adopted.

    Instead, library should export routines using the __attribute__((constructor)) and __attribute__((destructor)) function attributes. See the gcc info pages for information on these. Constructor routines are executed before dlopen() returns, and destructor routines are executed before dlclose() returns.

    So, try to dig in the direction of _init and _fini.

    • Thanks, and the question on SO is my question. :) - stanislav