Search code examples
openglmakefileg++shared-librariesfreeglut

Shared library with freeglut - undefined symbol


I'm actually experiencing some issues while linking an OpenGL/freeglut shared library (.so) with a C++ project. I'm sure that the problem is in my Makefile since the code I use to load (using the dlopen/dlsym/dlclose functions) works fine with other shared libraries. I thought it comes from headers inclusions but the OpenGL project I'm trying to work with compiles when I create an executable of it. I've also checked the glut FAQ but the solution now redirect to a dead link So there is my Makefile content, does anyone see where I am wrong ?

TARGET   = lib_opengl.so
CC       = g++

SRC      = GL_Handler.cpp       \
           GL_Utils.cpp

DEVIL_CFLAGS    := $(shell pkg-config --cflags IL)
DEVIL_LIBS      := $(shell pkg-config --libs IL)

LIBS    += -lGL -lGLU -lglut $(DEVIL_CFLAGS) $(DEVIL_LIBS)

CFLAGS   = -W -Werror -Wall -ansi -pedantic -fPIC -shared  -L/usr/X11R6/lib/ $(LIBS)

SRCDIR   = src
OBJDIR   = obj

SOURCES  := $(addprefix src/, $(SRC))

OBJECTS  := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)

rm       = rm -rf
mkdir    = mkdir -p

$(TARGET): $(OBJECTS)
        @$(CC) $(CFLAGS) -o $@ $(OBJECTS)
        @echo $(TARGET)" compiled !"

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp
        @$(mkdir) $(OBJDIR)
        @$(CC) $(CFLAGS) -c $< -o $@

all     : $(TARGET)

clean   :
        @$(rm) $(OBJDIR)
        @echo "Binary files deleted"

fclean  : clean
        @$(rm) $(TARGET) $(LINK)
        @echo "Binary and executable files are deleted"

re      : fclean all

.PHONY: all clean fclean re

And there is the result when I'm trying to link it with my shared libraries loader.

./so_loader ./lib/lib_opengl.so
./so_loader: symbol lookup error: ./lib/lib_opengl.so: undefined symbol: glutInit

I hope that my problem is understandable and thanks for reading.


Solution

  • As a start, use variable LDFLAGS for linking instead of CFLAGS which is meant for compilation. Something like this:

    LDFLAGS   = -L/usr/X11R6/lib
    ...
    $(TARGET): LDFLAGS += -shared -Wl,--no-undefined
    $(TARGET): $(OBJECTS)
            @$(CC) $(LDFLAGS) -o $@ $(OBJECTS) ${LIBS}