This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Attribute constructor and static libraries?


Paco Moya <Francisco dot Moya at uclm dot 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]