This question already has an answer here:
-
How to add “-l” (ell) compiler flag in CMake
2 answers
I know this question has been asked already but the solutions provided really hasn't worked for me.
I want to run my OpenGL program via CLion IDE. I am able to run the same program via the terminal in Ubuntu using
$gcc progname.c -lglut -lGL -lGLU
but am unable to run the same in CLion
This is my CMakeLists.txt file
PS: I am using CLion in Ubuntu. Also there is no error in the program.
Clion uses CMake for project files, and CMake makes it easy to link libraries that are in the system paths using target_link_libraries
, which you can use in your case as shown below, which can come after the add_executable
line:
target_link_libraries(jarvis -lglut -lGL -lGLU)
The documentation for target_link_libraries is here:
https://cmake.org/cmake/help/v3.3/command/target_link_libraries.html
I have made it work in both Windows 10 and Linux (Ubuntu 16.04) after a lot of searching on Google. Apparently it's not so easy to find after all. So, I am gonna put an end to this problem now and here.
Here, I am going to show you how to configure the CMakeLists.txt file to compile a OpenGL program which is the main challenge here. I am assuming that you can write basic OpenGL programs and you have written a file named
'demoMain.cpp'.
For Windows
I am assuming you can setup OpenGL on windows. If you can't, there are plenty of tutorials on youtube and StackOverflow. After that, continue.
cmake_minimum_required(VERSION 3.10)
project(Graphics_Offline_1) # Your Project Name
set(CMAKE_CXX_STANDARD 11)
include_directories(OpenGL)
include_directories(OpenGL/include) # OpenGL/include has to contain the required OpenGL's .h files
include_directories(OpenGL/lib) # OpenGL/lib has to contain the required OpenGL's .lib files
# glut32.dll must be present in "project-directory/OpenGL/dll/"
add_custom_target(glutdlllib
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/OpenGL/dll/glut32.dll ${CMAKE_BINARY_DIR}
)
# required .lib files to be copied into compiler's proper directory
set(OpenGlLibs glaux glu32 glui32 glut32 opengl32)
#These 3 lines are just linking and making executables
add_executable(demo demoMain.cpp)
target_link_libraries(demo ${OpenGlLibs})
add_dependencies(demo glutdlllib)
For Linux (Ubuntu 16.04)
It should work for other Ubuntu versions too. Linux has made it easier to use OpenGL than Windows.
cmake_minimum_required(VERSION 3.10) # common to every CLion project
project(OpenGLLinuxTest) # project name
set(OpenGlLinkers -lglut -lGLU -lGL) # setting all the Glut libraries as one variable.
################################################
add_executable(OpenGLLinuxTest1 main.cpp ) #common to all clion project
target_link_libraries(OpenGLLinuxTest1 ${OpenGlLinkers}) # linking opengl libraries to the project
#################################################
I am assuming that you can install OpenGL on Ubuntu. If you are facing problem with that,
follow this link -
http://www.codebind.com/linux-tutorials/install-opengl-ubuntu-linux/ .
If this is not working, follow this one -
https://gist.github.com/shamiul94/a632f7ab94cf389e08efd7174335df1c