Search code examples
c++linuxopenglg++glut

How to embed glut library into c++ project?


I have a c++ project but when distribute this, need install libglut.so.3.:

./bin: error while loading shared libraries: libglut.so.3: cannot open shared object file: No such file or directory

I want the user not to have to install this dependency. How to embed the library into project?

I try compile as static library in c++ project using g++. My make file contains:

@g++ \
    -o bin \
    -std=c++11 \
    main.cpp \
    -lm -lGL -lGLU -lglut \
    ;

I try define glut as static library:

-lm -lGL -lGLU -Wl,-Bstatic -lglut -Wl,-Bdynamic

But the compiler says:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libglut.a(libglut_la-freeglut_state.o): undefined reference to symbol 'XGetWindowAttributes'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libX11.so: error adding symbols: DSO missing from command line

I try download freeglut-3.0.0 and compile as static library:

Change CMakeLists.txt file for static compile:

OPTION(FREEGLUT_BUILD_SHARED_LIBS "Build FreeGLUT shared library." OFF)
OPTION(FREEGLUT_BUILD_STATIC_LIBS "Build FreeGLUT static library." ON)

And in the CMakeCache.txt:

//Build FreeGLUT shared library.
FREEGLUT_BUILD_SHARED_LIBS:BOOL=OFF

//Build FreeGLUT static library.
FREEGLUT_BUILD_STATIC_LIBS:BOOL=ON

And compile it:

$ cmake .
$ make
[ 62%] Built target freeglut_static
...
[100%] Built target shapes_static

And verify:

$ ll lib/libglut.a 
-rw-r--r-- 1 me me 690860 nov 17 19:11 lib/libglut.a
$ file lib/libglut.a
lib/libglut.a: current ar archive

Now, change the makefile:

-lm -lGL -lGLU freeglut-3.0.0/lib/libglut.a

And compile:

$ make
usr/bin/ld: freeglut-3.0.0/lib/libglut.a(fg_state_x11.c.o): undefined reference to symbol 'XGetWindowAttributes'
//usr/lib/x86_64-linux-gnu/libX11.so.6: error adding symbols: DSO missing from command line

But, same error message.

In similar problem: fglut/libfglut.a(freeglut_state.o): undefined reference to symbol 'XGetWindowAttributes' , i try add x11 libraries into project:

-lm -lGL -lGLU -lX11 freeglut-3.0.0/lib/libglut.a

Or:

-lm -lGL -lGLU -lX11 -Wl,-Bstatic -lglut -Wl,-Bdynamic

But have a same error message. What happened?


Solution

  • I have successfully linked my a GLUT program using this command

    g++ -o ogl-test main.cpp freeglut-3.0.0/lib/libglut.a -lGL -lGLU -lX11 -lXxf86vm -lXext -lXrandr -lXt -lXi

    Hopefully this will also work for you. Make sure you change the path to glut library.