Linking to a shared library and still having undefined references

Hi,
I am new in the forum as on unix. I am developing a project on Windows and I am trying to port it to unix. I have installed Ubuntu 14 and I have create Makefiles to compile those projects.
I have a project that generates a shared library (.so) and another project that generates an executable. When I generate the shared library I dont have any problem, but the nightmare begins when I try to compile the executable. When I try to compile it I am having a lot of undefined references. The wierd thing is that the compiler finds all the .so dependencies but I am having undefined references for all of them. This is the makefile of this project:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
program_NAME := MyExecutable

program_ODIR := $(INTERMEDIATE_FILES)unix/$(program_NAME)/Release/

program_SDIR := ./

program_OUTDIR := ../bin/unix/Release/

program_SRCS := $(wildcard $(program_SDIR)*.cpp)
program_OBJS := $(program_SRCS:.cpp=.o)
program_ODIR_OBJS := $(patsubst $(program_SDIR)%,$(program_ODIR)%,$(program_OBJS))

program_INCLUDE_DIRS := ../MySharedLib/include/ $(CPP_INCLUDE_PATH)glm/ \
 $(CPP_INCLUDE_PATH)websocketpp/ $(CPP_INCLUDE_PATH)boost/ $(CPP_INCLUDE_PATH)
program_LIBRARY_DIRS := $(program_OUTDIR) $(CPP_LIB_PATH)x86_64-linux-gnu/
program_LIBRARIES := MySharedLib boost_system GLEW glut GL

CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L $(librarydir))
LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))

CXX=g++ -std=gnu++0x -v

all: main-build

main-build: target

$(program_ODIR)%.o: $(program_SDIR)%.cpp 
	$(CXX) -c $(CPPFLAGS) -o $@ $<

target: $(program_ODIR_OBJS)
	$(CXX) $(LDFLAGS) $(program_ODIR_OBJS) -o $(program_OUTDIR)$(program_NAME)

clean:
	@- $(RM) $(program_OUTDIR)$(program_NAME)
	@- $(RM) $(program_ODIR_OBJS)


I am having undefined references of my library, Boost, Glew, Glut and GL. If I remove the library directories (-L options) then I am having an error, the linker does not find libMySharedLib.so, so this makes me think that before the linker was finding my library. I have to say that I have downloaded all the dependencies using apt-get.

I dont know if this is a common error or I am doing something wrong.

Thanks in advance.
Last edited on
When you compile a library in Windows or OSX, you do link it. If you unmet have references, you do get an error.

When you compile a library in Linux, there is no apparent linking. There will be no warnings of references that cannot be linked to something. You will get those errors when you try to link an executable.


IIRC, you can look with nm what is in your .so and what does it refer to.
Windows and Unix reference dependent libraries differently.

On Unix, a shared liibrary can reference another shared library, but not require it to build. The owning program will need to link to it however.

This is not the case on Windows; if a DLL references another DLL, it must find it when building that DLL. The EXE is not required to mention it all.

Your problem is almost certainly libraries used by your shared libraries not being specified in the program's link line.
Thanks for the quick response, I didnt know that ;)

I am still having the same problems, I think I am using the right libraries but I dont know. This is the link command my Makefile generates.

1
2
g++ -std=gnu++0x -L ../bin/unix/Release/ -L /usr/lib/x86_64-linux-gnu/ -lMySharedLib 
-lboost_system -lGLEW -lglut -lGL class1.o main.o -o ../bin/unix/Release/MyExecutable


And this is some of the errors I obtain in the output (from the main.o)

1
2
main.cpp:(.text+0xfcc): undefined reference to `glReadBuffer'
main.cpp:(.text+0xffc): undefined reference to `glReadPixels'


Maybe is a problem of x86 and x64 compatibility?
Thanks
Last edited on
I have found the problem. The problem was the order of the parameters in the compiler instruction, I had to put at the end the list of shared libraries to link.

1
2
g++ -std=gnu++0x  class1.o main.o -o ../bin/unix/Release/MyExecutable -L ../bin/unix/Release/ 
-L /usr/lib/x86_64-linux-gnu/ -lMySharedLib -lboost_system -lGLEW -lglut -lGL
Last edited on
Topic archived. No new replies allowed.