Separate-stage static linking of C or C++ code using -nostdlib

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi! I need to compile C (or C++) source code to an object file and then link it with the appropriate library in two separate stages. I've always seen this done (and done it this way myself?) by invoking gcc both times. --

gcc -c myfile.c -o myfile.o
gcc myfile.o -o myfile

This works great if I'm using the installed standard libraries and include headers. But now I want to compile a C object file (say) and link it against (say) a version of Gnu Glibc that I have downloaded, built, and installed into a separate, special directory. Then, I am finding that I have to invoke ld or GCC's helper utility collect2 rather than gcc itself for the linking stage. --

BASEDIR = /export/home/afa9/Documents/glibc/bin
LIBDIR := $(BASEDIR)/lib
START_FILES := $(LIBDIR)/crt1.o $(LIBDIR)/crti.o /usr/lib/gcc/i586- suse-linux/4.3/crtbeginT.o LINK_LIBS := -L/usr/lib/gcc/i586-suse-linux/4.3 -L/usr/i586-suse-linux/ lib -L$(LIBDIR)
GCC_LIBS = --start-group -lgcc -lgcc_eh -lc --end-group
END_FILES :=  /usr/lib/gcc/i586-suse-linux/4.3/crtend.o $(LIBDIR)/crtn.o
LD_OPTIONS := -g -nostdlib -static -m elf_i386
COMP_OPTIONS = -Wall -g -nostdinc
INCLUDES := -I$(BASEDIR)/include -I/usr/lib/gcc/i586-suse-linux/4.3/ include \
        -I/usr/lib/gcc/i586-suse-linux/4.3/include-fixed

COLLECT = /usr/lib/gcc/i586-suse-linux/4.3/collect2

hello: hello.o
ld $(LD_OPTIONS) -o hello $(LINK_LIBS) $(START_FILES) hello.o $(END_FILES) $(GCC_LIBS) # Also works: $(COLLECT) $(LD_OPTIONS) -o hello $(LINK_LIBS) $ (START_FILES) hello.o $(END_FILES) $(GCC_LIBS)

hello.o: hello.c
        gcc $(COMP_OPTIONS) $(INCLUDES) -c hello.c -o hello.o


If I try to use gcc for the link stage rather than collect2, I get a huge bunch of errors, most of which have to do with undefined references to _Unwind_Resume, to other routines with names starting with "_Unwind," and to __gcc_personality_v0. Does anybody know why this is, and why gcc doesn't just pass the relevant arguments along to ld and invoke ld? I thought that that's what it was supposed to do.

Thanks!




Amittai Aviram
Graduate Student in Computer Science
Yale University
646 483 2639
amittai.aviram@xxxxxxxx
http://www.amittai.com


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux