Page 1 of 1

GNU LD linker option Issue statically linking standard libs

Posted: Thu Jan 24, 2013 5:05 pm
by ja
Hi

I am trying to link some standard libraries using "ld" toolchain and facing issue while including the standard libraries.
I know using "gcc" is the better option as it always include standard libraries but i have same symbols reused so i can not use "gcc" option for linking.
Please let me know how i can link using "ld" and provide standard library such as libgcc or libc ?

For reference following is the command i am trying - Here my assumption is -lc command can link the libc library but i don't see it happens and i am still getting undefined reference to library function names.


compiler tool chain/ld -T {linkerscript} -lc a.o b.o -o final.bin

Has any one tried before statically linking standard library using ld ?

Thanks,
- ja

Re: GNU LD linker option Issue statically linking standard l

Posted: Thu Jan 24, 2013 6:49 pm
by Trex
If you have using Makefile to compile, you are able to do it as follows:

Code: Select all

TARGET = foobar

LD := $(CROSS_COMPILE)ld
CC := $(CROSS_COMPILE)gcc

SRCS = foo.c bar.c main.c
OBJS = $(SRCS:.c=.o)

LIBS += $(shell $(CC) -print-file-name=libc.a)
LIBS += $(shell $(CC) -print-libgcc-file-name)

$(TARGET): $(OBJS)
    $(LD) $(LDFLAGS) -T $(TARGET).lds $(OBJS) $(LIBS) -o $@

Re: GNU LD linker option Issue statically linking standard l

Posted: Thu Jan 24, 2013 8:47 pm
by ja
Hi

Why are you still using the gcc for the libs ?
I suppose the $(LIBS) expands to $(CROSS_COMPILE)gcc -print-file-name=libc.a ?

Can you point me where to find information about -print-file-name ?

Thanks,
-ja

Re: GNU LD linker option Issue statically linking standard l

Posted: Thu Jan 24, 2013 8:50 pm
by ja
I found the option -print-file-name=library option as part of gcc not ld...
Is there any way to pass directly to ld tool ?

Thanks,
- ja

Re: GNU LD linker option Issue statically linking standard l

Posted: Thu Jan 24, 2013 9:07 pm
by ja
I tried this solution and it doesn't work :( I am still getting undefined issue..

Thanks,
-ja

Re: GNU LD linker option Issue statically linking standard l

Posted: Thu Jan 24, 2013 9:18 pm
by ja
Sorry my mistake the solution works perfectly fine I found bug in my Makefile...
Thanks for the help !!

Regards,
- ja

Re: GNU LD linker option Issue statically linking standard l

Posted: Sat Jan 26, 2013 5:56 am
by dozniak
ja wrote:Sorry my mistake the solution works perfectly fine I found bug in my Makefile...
Thanks for the help !!

Regards,
- ja
You need to specify libraries AFTER the object files because ld searches for undefined symbols only linearly and does not go back looking in previous libraries after it have found a new undefined symbol.