0

Using Ubuntu 14.04, g++, I want to port a project from make to cmake. Getting the following error when I sudo make install.

Linking CXX executable texmapper
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../..
/lib/libplibssg.a(ssgLeaf.o): undefined reference to symbol 'glNewList'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libGL.so: 
error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

A linker error like many encountered on this website. But the usually suggested fix, "-lGL -lGLU -lglut, does not work. I also tried some permutations of this.

Before the undefined reference to glNewList I had an undefined reference to glPopClientAttrib, which I solved by adding lGL, like this:

set(CMAKE_CXX_FLAGS "-lGL")

So I am on the right track, but the answer is eluding me.

Edit: full command (verbose):

/usr/bin/c++   -lGLEW -lGL -lGLU -lglut    CMakeFiles/texmapper.dir/maintexmapper.o CMakeFiles/texmapper.dir/ssgSaveAC.o  -o texmapper -rdynamic -Wl,-Bstatic -lplibsg -lplibssg -lplibul -Wl,-Bdynamic ../../libs/tgf/libtgf.so ../../libs/tgfclient/libtgfclient.so -lglut -lXmu -lXi -lpng -lz -lSM -lICE -lX11 -lXext -lXrandr -Wl,-rpath,/home/jeroen/Thesis/torcsCMAKE/trunk/src/libs/tgf:/home/jeroen/Thesis/torcsCMAKE/trunk/src/libs/tgfclient
benk
  • 377
  • 1
  • 4
  • 13
  • Many OpenGL programs use the glew library so maybe try adding -lGLEW to there too? – Jas Apr 06 '16 at 17:25
  • Thanks for the suggestion. Tried -lGLEW before and after the others, but it didn't solve the error. – benk Apr 06 '16 at 17:36
  • 1
    CMAKE_CXX_FLAGS contains the options to be passed to the C++ compiler. `-l` options, e.g. `-lGL`, are linker options, not compiler options. CMake expects linker options in `CXX_EXE_LINKER_FLAGS`. See [cmake-variables](https://cmake.org/cmake/help/v3.0/manual/cmake-variables.7.html) – Mike Kinghan Apr 06 '16 at 18:08
  • Cool, I didn't know that. But adding `set(CXX_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-lGLEW -lGL -lGLU -lglut")` could not solve my error. I also tried omitting `${}` and extending the double quotes. Also, I could not do without `set(CMAKE_CXX_FLAGS "-lGL")`; if I replace `CMAKE_CXX_FLAGS` with `CXX_EXE_LINKER_FLAGS`, the other undefined reference to `glPopClientAttrib` returns. – benk Apr 06 '16 at 19:56
  • @Jas: glNewLists originates in the very first version of OpenGL, namely version 1.0. You definitely don't need GLEW to use it. Actually it's so old and busted, that it's been removed from OpenGL-3 onward. However it should be still be exported by libGL.so as this is required by the ABI contract. – datenwolf Apr 06 '16 at 20:08
  • Linking **order** is meaningfull: library should be mention **after** the object (or other library) which uses it. That is why linking via `CMAKE_EXE_FLAGS` is incorrect and `target_link_libraries` should be used instead. See [here](http://stackoverflow.com/questions/19901934/strange-linking-error-dso-missing-from-command-line) for more info about linking order. – Tsyvarev Apr 06 '16 at 21:43

1 Answers1

1

With CMake the required libraries are set using the target_link_libraries command. Usually you'll use find_package(OpenGL) to collect the locations of the OpenGL development and library files and use the package variables for the target_link_libraries command. Like this:

find_package(OpenGL)
add_executable(foo …)
target_link_libraries(foo ${OPENGL_gl_LIBRARY} …)
datenwolf
  • 159,371
  • 13
  • 185
  • 298