Сделать вызов с помощью атрибута constructor gcc
__attribute__ ((constructor))
void construct() {
// Инициализация
}
не получается из-за флага -nostdlib. Есть ли другой способ (библиотека - чистый C)?
Stack Overflow на русском — это сайт вопросов и ответов для программистов. Присоединяйтесь! Регистрация займёт не больше минуты.
Присоединиться к сообществуТут точь в точь ваш вопрос How to run constructor even if “-nostdlib” option is defined. Правда, ответы неутешительные.
А в документации по dlopen сказано следующее:
The obsolete symbols _init() and _fini()
The linker recognizes special symbols
_init
and_fini
. If a dynamic library exports a routine named_init
, then that code is executed after the loading, beforedlopen()
returns. If the dynamic library exports a routine named_fini
, then that routine is called just before the library is unloaded. In case you need to avoid linking against the system startup files, this can be done by giving gcc the "-nostartfiles" parameter on the command line.Using these routines, or the gcc
-nostartfiles
or-nostdlib
options, is not recommended. Their use may result in undesired behavior, since the constructor/destructor routines will not be executed (unless special measures are taken).Instead, libraries 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 beforedlopen()
returns, and destructor routines are executed beforedlclose()
returns.
Так что, попробуйте копнуть в сторону _init and _fini.