Outils pour utilisateurs

Outils du site


gcc_lib_dynamque

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
gcc_lib_dynamque [2007/03/27 22:54] – créée thierrygcc_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: ==
  
-'' +<code> 
-#include <unistd.h>\\ +#include <unistd.h> 
-#include <stdio.h>\\ +#include <stdio.h> 
-#include <stdlib.h>\\ +#include <stdlib.h>
-\\ +
-static int lib_load = 0;\\ +
-\\ +
-unsigned int my_inc( unsigned int *value )\\ +
-{\\ +
-   if ( value )\\ +
-   {\\ +
- (*value)++;\\ +
-       return( (*value) );\\ +
-   }\\ +
-\\ +
-   return( 0 );\\ +
-}\\ +
-\\ +
-int my_stat( void )\\ +
-{\\ +
-   return( lib_load );\\ +
-}\\ +
-\\ +
-void __attribute__ ((constructor)) onload( void )\\ +
-{\\ +
-   lib_load++;\\ +
-}\\ +
-\\ +
-void __attribute__ ((destructor)) onunload( void )\\ +
-{\\ +
-   lib_load--;\\ +
-}\\ +
-''+
  
-Pour compiler:\\ +static int lib_load = 0; 
-''$ gcc -g -Wall %blue%-shared -fPIC%% mylibtest.c -o mylibtest.so''+ 
 +unsigned int my_inc( unsigned int *value ) 
 +
 +   if ( value ) 
 +   { 
 +       (*value)++; 
 +       return( (*value) ); 
 +   } 
 + 
 +   return( 0 ); 
 +
 + 
 +int my_stat( void ) 
 +
 +   return( lib_load ); 
 +
 + 
 +void __attribute__ ((constructor)) onload( void ) 
 +
 +   lib_load++; 
 +
 + 
 +void __attribute__ ((destructor)) onunload( void ) 
 +
 +   lib_load--; 
 +
 +</code> 
 + 
 +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 ''static'' sera visible.\\ 
-**onload** et **onunload** sont juste la pour montrer la possibilite d'executer du code au chargement et déchargement de la lib (mais il paraît que ce n'est pas 'portable')\\+''onload'' et ''onunload'' sont juste la pour montrer la possibilite d'executer du code au chargement et déchargement de la lib (mais il paraît que ce n'est pas 'portable')\\ 
 + 
 +== Démonstration du chargement == 
 +<code> 
 +#include <stdio.h> 
 +#include <dlfcn.h> 
 +#include <stdlib.h> 
 + 
 +int main( int argc, char *argv[] ) 
 +
 +   void *hlib; 
 +   int ( *my_stat )( void ); 
 +   unsigned int (*my_inc)( unsigned int * ); 
 + 
 +   if ( ! ( hlib=dlopen("./mylibtest.so", RTLD_LAZY ) ) ) 
 +   { 
 +      printf("error: %s\n",dlerror()); 
 +      return EXIT_FAILURE; 
 +   } 
 + 
 +   if ( ! (my_stat = ((int(*)(void))dlsym(hlib,"my_stat")) ) ) 
 +   { 
 + printf("error: %s\n",dlerror()); 
 + return EXIT_FAILURE; 
 +   } 
 + 
 +   printf("my stat= %d\n",my_stat()); 
 + 
 +   if ( ! (my_inc = ((unsigned int(*)(unsigned int *))dlsym(hlib,"my_inc")) ) ) 
 +   { 
 + printf("error: %s\n",dlerror()); 
 + return EXIT_FAILURE; 
 +   } 
 + 
 +   unsigned int v = 500; 
 +   printf("v= %u\n",my_inc(&v)); 
 + 
 +   dlclose( hlib ); 
 + 
 +   return( EXIT_SUCCESS ); 
 +
 +</code> 
 + 
 +Compiler: 
 +  $ gcc -g -Wall -o mytest mytest.c -ldl 
 + 
 +A l'execution: 
 +  $ ./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