gcc_lib_dynamque
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
gcc_lib_dynamque [2007/03/27 22:54] – créée thierry | gcc_lib_dynamque [2007/03/28 13:37] (Version actuelle) – I don't like colored code thierry | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
== Creer une librairie simple: == | == Creer une librairie simple: == | ||
- | '' | + | < |
- | #include < | + | #include < |
- | #include < | + | #include < |
- | #include < | + | #include < |
- | \\ | + | |
- | static int lib_load = 0;\\ | + | |
- | \\ | + | |
- | unsigned int my_inc( unsigned int *value )\\ | + | |
- | {\\ | + | |
- | if ( value )\\ | + | |
- | {\\ | + | |
- | (*value)++; | + | |
- | | + | |
- | }\\ | + | |
- | \\ | + | |
- | | + | |
- | }\\ | + | |
- | \\ | + | |
- | int my_stat( void )\\ | + | |
- | {\\ | + | |
- | | + | |
- | }\\ | + | |
- | \\ | + | |
- | void __attribute__ ((constructor)) onload( void )\\ | + | |
- | {\\ | + | |
- | | + | |
- | }\\ | + | |
- | \\ | + | |
- | void __attribute__ ((destructor)) onunload( void )\\ | + | |
- | {\\ | + | |
- | | + | |
- | }\\ | + | |
- | '' | + | |
- | Pour compiler:\\ | + | static int lib_load = 0; |
- | '' | + | |
+ | unsigned int my_inc( unsigned int *value ) | ||
+ | { | ||
+ | if ( value ) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | int my_stat( void ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | void __attribute__ ((constructor)) onload( void ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | void __attribute__ ((destructor)) onunload( void ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Pour compiler: | ||
+ | $ gcc -g -Wall -shared -fPIC mylibtest.c -o mylibtest.so | ||
== A savoir: == | == A savoir: == | ||
- | Tout ce qui n'est pas **static** sera visible.\\ | + | Tout ce qui n'est pas '' |
- | **onload** et **onunload** sont juste la pour montrer la possibilite d' | + | '' |
+ | |||
+ | == Démonstration du chargement == | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main( int argc, char *argv[] ) | ||
+ | { | ||
+ | void *hlib; | ||
+ | int ( *my_stat )( void ); | ||
+ | | ||
+ | |||
+ | if ( ! ( hlib=dlopen(" | ||
+ | { | ||
+ | printf(" | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | |||
+ | if ( ! (my_stat = ((int(*)(void))dlsym(hlib," | ||
+ | { | ||
+ | printf(" | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | if ( ! (my_inc = ((unsigned int(*)(unsigned int *))dlsym(hlib," | ||
+ | { | ||
+ | printf(" | ||
+ | return EXIT_FAILURE; | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Compiler: | ||
+ | $ gcc -g -Wall -o mytest mytest.c -ldl | ||
+ | |||
+ | A l' | ||
+ | $ ./mytest | ||
+ | my stat= 1 | ||
+ | v= 501 | ||
- | A suivre... | + | Et voila pour les premiers tests... |
gcc_lib_dynamque.1175036064.txt.gz · Dernière modification : 2007/03/27 22:54 de thierry