2011-02-10 9 views
16

Algunos Unix compartidos proporcionan una salida cuando se llama desde la línea de comandos como si fueran ejecutables. Por ejemplo:bibliotecas Ejecución de una biblioteca compartida en Unix

$ /lib/libc.so.6 
GNU C Library stable release version 2.13, by Roland McGrath et al. 
Copyright (C) 2011 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. 
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. 
Compiled by GNU CC version 4.5.2. 
Compiled on a Linux 2.6.37 system on 2011-01-18. 
[...] 

En una biblioteca compartida, escrita en C, ¿cómo puedo proporcionar esta salida? He ejecutado ahora una biblioteca que acabo de crear y recibo un error de segmento.

Nota: Me hizo esta previamente en unix.stackechange.com https://unix.stackexchange.com/questions/7066/executing-a-shared-library

+3

Ver también http://stackoverflow.com/questions/1449987/building-a-so-that-is-also-an-executable –

+0

@BrianL. Gracias, muy interesante! – franziskus

Respuesta

10

La definición más abajo de la principal es responsable de imprimir la salida que se ve. Se define en csu/version.c del árbol fuente de glibc. Espero que esto ayude.

 
#ifdef HAVE_ELF 
/* This function is the entry point for the shared object. 
    Running the library as a program will get here. */ 

extern void __libc_main (void) __attribute__ ((noreturn)); 
void 
__libc_main (void) 
{ 
    __libc_print_version(); 
    _exit (0); 
} 
#endif 
Cuestiones relacionadas