How to embed glut library into c++ project?

I have a c++ project (using linux, ubuntu 18.04) 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: makefile - fglut/libfglut.a(freeglut_state.o): undefined reference to symbol 'XGetWindowAttributes' - Stack Overflow , 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?

From your linked thread:

or add -lX11 after -lfglut

The order matters here.

Don’t take it for the truth because I am no Linux expert but as far as I know the X11 library is for creating Windows and GUIs in Linux. freeglut or glut need them to create the GL window. So when you build the static libs, you need to link the library to your glut object files.

With the current linking order, your program will first link the x11 lib. Every missing reference to a x11 function you have used (which is probably none, because you didn’t use the lib directly) will be filled by the linker. Afterwards the linker resolves all references to your glut function calls with the data from the compiled object file. In this object file there are still unresolved references to x11 functions but the linker has already done the linking to the x11 lib and won’t do it again unless you tell him to do so. So just move the -lx11 to the end of your command and it should work.

Since you are using cmake:
Try to use the find_package functionality of cmake:


find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(GLUT REQUIRED)

It defines you cmake targets that you can link to your programs: FindGLUT — CMake 3.9.6 Documentation

For example


target_link_libraries(
    OpenGL::GL
    OpenGL::GLU
    GLUT::GLUT
    GLEW::GLEW
    )

Benefit: CMake figures out all necessary dependencies and links them. So you don’t need to mess around with linking orders.