5

I wrote c++ project in which I am using OpenGL and Glut library.

When I compile it from command line everything works fine. Here is sample.

g++ -o prog source.cpp -lGL -lGLU -lglut --std=c++11 -L /usr/lib/nvidia-331/

But when I want to use CMake in QtCreator:

project(proj)
cmake_minimum_required(VERSION 2.8)

aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lGL -lGLU -lglut -L /usr/lib/nvidia-331/") 

I recieve msg: error: undefined reference to `glColor3f' and so on.

Can anybody help me?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Bender
  • 51
  • 1
  • 1
  • 2

1 Answers1

5
project(proj)
cmake_minimum_required(VERSION 2.8)

find_package(OpenGL)
find_package(GLUT)

aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(
    ${PROJECT_NAME}
    ${OPENGL_gl_LIBRARY}
    ${GLUT_LIBRARIES} )

Note: You should not use the project name for the executable

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Still does not work: 'error: [cmake_check_build_system] Error 1'. As i know parameter "-L /usr/lib/nvidia-331/" is necessery for program to work fine. There is something wrong with default OpenGL library. – Bender Nov 07 '14 at 20:16
  • 2
    @Bender: Please speak with me: **"I'll never link to libGL.so with an absolute or a non-default path!"** If you can't properly link against libGL.so on your system you should fix *that*. I'm not quite sure if your use of `aux_source_directory` is valid or sensible. But the rest of that CMake I took directly from one of my own OpenGL projects, so on a properly configured system it should work. – datenwolf Nov 07 '14 at 20:23
  • For future users, you are supposed to add -L paths as in /usr/lib/nvidia-331/ to variable LD_LIBRARY_PATH. Example: export LD_LIBRARY_PATH=/home/sk/anaconda3/lib/:$LD_LIBRARY_PATH, will prepend the path /home/sk/anaconda3/lib/ to LD_LIBRARY_PATH – saurabheights Jul 18 '18 at 23:12
  • 1
    @saurabheights: No! Don't do this! This interferes with GLvnd or symlinks set by the installer. You should **never** specify extra library search paths for system level libraries. Ohterwise hooking, or injecting like it's required for things like VirtualGL gets problematic. **_Don't do that!_** – datenwolf Jul 19 '18 at 06:26
  • I'm not doing that. For any library, if it's built using CMake, we should load it using CMake and it will provide proper include and lib directory variables. My advise on LD_LIBRARY_PATH is in general on how to link with custom libraries. I am currently working on python code which uses custom built C++ libraries with no CMAKE. P.S. I copied your answer, as its the right way. – saurabheights Jul 19 '18 at 09:40
  • P.S. I just didnt know from where CMake gets glut variable such as GLUT_LIBRARIES, ran locate glut but no cmake. Furthermore, variable GLUT_LIBRARIES didnt work on my system, and printing all cmake variables, I found GLUT_glut_LIBRARY. – saurabheights Jul 19 '18 at 09:43
  • All installed using apt-get, freeglut3 and freeglut3-dev – saurabheights Jul 19 '18 at 09:51