This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Attribute constructor and static libraries?
- From: Oscar Fuentes <ofv at wanadoo dot es>
- To: gcc-help at gcc dot gnu dot org
- Cc: Francisco dot Moya at uclm dot es
- Date: Fri, 11 Apr 2003 22:16:03 +0200
- Subject: Re: Attribute constructor and static libraries?
- Cancel-lock: sha1:qMwGEev5nboKQe8eHKmup5O7B4s=
- References: <20030411120543.GA4842@marvin.inf-cr.uclm.es>
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