Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLEW + cmake linking fails "undefined reference to symbol glDrawElements" + "DSO missing from command line"

I'm linking GLEW, SDL2 and Assimp with Cmake. It seems to be working fine while building the .o files, however when linking them I get these errors

:-1: error: CMakeFiles/"Projectpath".cpp.o: undefined reference to symbol 'glDrawElements'
/usr/lib/x86_64-linux-gnu/mesa/libGL.so.1:-1: error: error adding symbols: DSO missing from command line
:-1: error: collect2: error: ld returned 1 exit status

Here's the part of the Cmakefile that links the libraries

find_package(OpenGL)
find_package(GLEW)
find_package(SDL2)
find_package(Assimp)
#Include(FindPkgConfig)
#PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
set(INCLUDE_DIRS ${OpenGL_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${Assimp_INCLUDE_DIRS})
set(LIBS ${LIBS} ${OpenGL_LIBRARIES} ${SDL2_LIBRARIES} ${GLEW_LIBRARIES} ${Assimp_LIBRARIES} )
include_directories(${INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME} ${LIBS})

I tried changing the linking order as I read that could be a problem. I also added the OpenGL thing because of the libGL thing in the error, it doesn't seem to work. I also read about checking dependencies using pkg-config --print-requires --print-requires-private glew, but the problem is that it has like 20 different libraries it depends on. Would I need to link all of them?

It's not that I don't have the correct libraries, I created a basic OpenGL program on this computer last week and that worked fine (I used another way of linking my libraries and also used Code::Blocks instead of Qt-Creator)

I assume that the DSO thing is the problem after reading the answer to this question. But shouldn't that mean that doing the OpenGL thing in the cmakefile fix it?

Thanks!

EDIT: I can create VertexArrays, VertexBuffers etc. But as soon as I add in calls to either. glDrawElements(...) or glDrawArrays(...) I get that error. (I might get that error on some other functions as well, but those are the only ones I get that error on when I try to render a basic mesh)

like image 709
v_johan Avatar asked Jul 06 '15 15:07

v_johan


2 Answers

The problem was that in OpenGL_INCLUDE_DIRS and OpenGL_LIRARIES is actually OPENGL_INCLUDE_DIRS (<-- not quite sure I need that one, I probably don't) and OPENGL_LIBRARY So the CmakeFile snippet I had in my question should actually look like this

find_package(OpenGL)
find_package(GLEW)
find_package(SDL2)
find_package(Assimp)
#Include(FindPkgConfig)
#PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
set(INCLUDE_DIRS ${INCLUDE_DIRS} ${OPENGL_INCLUDE_DIRS} ${SDL2_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${Assimp_INCLUDE_DIRS}) #<--- Changed this from ${OpenGL_INCLUDE_DIRS} to ${OPENGL_INCLUDE_DIRS} (Again not sure I need that variable)
set(LIBS ${LIBS} ${OPENGL_LIBRARY} ${SDL2_LIBRARIES} ${GLEW_LIBRARIES} ${Assimp_LIBRARIES} ) #<---(Changed this from ${OpenGL_LIBRARIES} to ${OPENGL_LIBRARY}
include_directories(${INCLUDE_DIRS})

target_link_libraries(${PROJECT_NAME} ${LIBS})

And that fixed it! :)

like image 142
v_johan Avatar answered Sep 19 '22 18:09

v_johan


I got a similar issue configuring GLEW, with CMake, applying to the basic tutorial #2 from http://www.opengl-tutorial.org for testing purpose.

I am using GLFW instead of SDL, but the root cause is the same.

With recent CMake (>= 3.1), we simply need:

find_package(OpenGL REQUIRED)
target_link_libraries( myApp OpenGL::GL )

find_package ( GLEW REQUIRED )
target_link_libraries( myApp GLEW::GLEW )   

As you pointed out, we got an "undefined reference" issue if not looking for openGL.

like image 41
Grumot Avatar answered Sep 17 '22 18:09

Grumot