[LCP] How to do intremental linking with gcc and ld

Bryan Christ bryan.christ at gmail.com
Sun Jun 21 14:34:50 EST 2009


Thanks David.  I am actually not building a shared library--that I've
done many times.  I really appreciate you providing the reason to the
answer as well.  Gracias!

On Sat, Jun 20, 2009 at 7:09 PM, David Nugent<davidn at datalinktech.com.au> wrote:
> On Sat, 20 Jun 2009 11:48:34 +1000, Bryan Christ <bryan.christ at gmail.com>
> wrote:
>
>> I have a top-level directory from which I want to share as common code
>> with two different applications (a client and a server).  I thought it
>> would be quite easy to create a combined object file in the common
>> directory and then link to it from each of the individual programs.
>> While I don't have any problem compiling the larger common object
>> file, I have having unresolved symbols when I try to link the two
>> different programs to it.
>>
>> Here is how I am creating the common object file:
>>
>>  gcc -c $(CFLAGS) $(INCLUDES) $(DEFS) *.c
>>  ld -r -o common_tools.o *.o
>>
>> And here is how I try to link against the common object file:
>>
>> gcc -c $(CFLAGS) $(INCLUDES) $(LIBS) $(NORMAL_DEFS) *.c
>> ld -o myprogram *.o ../../common/common_tools.o
>>
>> I'm sure I'm missing something trivial but don't know what that is.
>
>
> Don't use ld to link directly except in very special circumstances, but
> use the gcc front-end instead. Just replace ld in the command line above
> with gcc and it should work fine. In fact, you can just include the
> "-o myprogram ../../../common/common_tools.o" to the gcc command above
> it and it will do it all in one step.
>
> Reason: the unix link editor usually needs a whole bunch more than you're
> providing it to successfully link userland code, such as reference to the
> standard C library, c startup object file and other platform specific
> directives which the gcc front-end knows about but ld does not.  ld makes
> no assumptions like that, it is used in a far wider variety of link-edit
> scenarios than for userland applications.
>
> I'm not sure what you're doing with the common files, although it looks
> like you're attempting to build a shared library? The extension used
> there is usually .so, not .o.  gcc can do this also with -shared in its
> argument list. If you're just wanting to include the objects, then use ar
> to build the static library instead as a .a file.
>
> Regards,
> David
>
>
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List
>



-- 
Bryan
<><



More information about the linuxCprogramming mailing list