Attribute constructor and static libraries?

Oscar Fuentes ofv@wanadoo.es
Fri Apr 11 20:15:00 GMT 2003


Paco Moya <Francisco.Moya@uclm.es> writes:

> A simple example illustrates the problem:
>
> p.c------------------------------------
>
> void f(void) __attribute__((constructor));
>
> void f(void)
> {
> 	printf("Hola\n");
> }
>
> main.c----------------------------------
>
> int
> main()
> {
> 	exit(0);
> }
> ---------------------------------------
>
> Now try compiling the example:
>
> gcc -c p.c
> gcc -c main.c
> gcc -o first main.o p.o
>
> And also in this way:
>
> gcc -c p.c
> gcc -c main.c
> ar rcs libp.a p.o
> gcc -o second main.o libp.a
>
> The second version does not work as expected!
>
> Any clue?

As main.o doesn't reference any symbol defined on p.o, that object
module is not picked from the library. For static libraries, the
linker only uses those object modules that defines symbols referenced
on previous objects. The first case works because you force the
inclusion of p.o on the executable by explicitly mentioning it on the
object list.

IIRC, ld has a switch for forcing the inclusion of all the objects
contained on a library.

-- 
Oscar



More information about the Gcc-help mailing list