GNU LD linker option Issue statically linking standard libs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ja
Posts: 11
Joined: Thu Jan 24, 2013 4:53 pm

GNU LD linker option Issue statically linking standard libs

Post 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
User avatar
Trex
Posts: 4
Joined: Wed Nov 21, 2012 8:06 am
Location: Earth

Re: GNU LD linker option Issue statically linking standard l

Post 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 $@
Last edited by Trex on Thu Jan 24, 2013 9:42 pm, edited 1 time in total.
Grrrrrrrrr...
ja
Posts: 11
Joined: Thu Jan 24, 2013 4:53 pm

Re: GNU LD linker option Issue statically linking standard l

Post 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
ja
Posts: 11
Joined: Thu Jan 24, 2013 4:53 pm

Re: GNU LD linker option Issue statically linking standard l

Post 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
ja
Posts: 11
Joined: Thu Jan 24, 2013 4:53 pm

Re: GNU LD linker option Issue statically linking standard l

Post by ja »

I tried this solution and it doesn't work :( I am still getting undefined issue..

Thanks,
-ja
ja
Posts: 11
Joined: Thu Jan 24, 2013 4:53 pm

Re: GNU LD linker option Issue statically linking standard l

Post by ja »

Sorry my mistake the solution works perfectly fine I found bug in my Makefile...
Thanks for the help !!

Regards,
- ja
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: GNU LD linker option Issue statically linking standard l

Post 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.
Learn to read.
Post Reply