I am using gcc 4.3.4 and ld 2.20.51 on Cygwin under Windows 7. Here is a simplified version of my problem:
foo.o contains the function foo_bar() , which calls bar() in bar.obar.o contains the function bar()main.c calls functions in foo.o , but foo_bar() not part of the call chain
If I try to compile main.c and link it to foo.o, I get an undefined reference to _foo_bar from ld. As you can see from my Makefile, except below, I tried to use flags to place each function in my section and delete unused sections with the linker.
COMPILE_CYGWIN = gcc -iquote$(INCDIR) COMPILE = $(COMPILE_CYGWIN) -g -MMD -MP -Wall -ffunction-sections -Wl,-gc-sections $(DEFINE) main_OBJECTS = main.o foo.o main.exe : $(main_OBJECTS) $(COMPILE) -o main.exe $(main_OBJECTS)
The foo_bar() function is a short function that provides a connection between two network layers in the protocol stack. Some programs do not need this, so they will not be linked in other object files associated with the top level of the stack. This is a small feature, and it seems inappropriate to put it in your own .o file.
I do not understand why ld throws an error - nothing calls foo_bar() , so there is no need to include bar() in the final executable. An employee just told me that ld not a "smart linker", so maybe what I'm trying to do is not possible?
source share