Cmake link library target link error
Asked Answered
P

1

32

Hi I have problem with linkg Glfw and other libraries using cmake. From command line i compile like this

g++ main.cpp -lGL -lGLU -lGLEW -lglfw

But I wanted to use cmake for compiling. I tried to use target_linkg_libraries but this produce error

CMake Error at CMakeLists.txt:18 (target_link_libraries): Cannot specify link libraries for target "GL" which is not built by this
project.

I tried do this using add definitions. I dont see error but this don't link libraries.

cmake_minimum_required (VERSION 2.6)
project (test)

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

ADD_DEFINITIONS(
    -lGL
    -lGLU
    -lGLEW
    -lglfw
)

add_executable(test.out
    main.cpp
)

target_link_libraries(GL GLU GLEW glfw)
Platform answered 20/10, 2013 at 13:45 Comment(2)
ADD_DEFINITIONS is for extra defines not for libraries, try add_libraries() then.Nonpareil
unknown command add_librariesPlatform
D
64

The syntax for target_link_libraries is:

target_link_libraries(your_executable_name libraries_list)

And you don't have to add add_definition statements (target_link_libraries adds this options)

There are also some useful variables provided by OpenGL and GLEW packages.

Your CMakeLists.txt should be like:

cmake_minimum_required (VERSION 2.6)
project (test)

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)

include_directories(${OPENGL_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})

add_executable(test
    main.cpp
)

target_link_libraries(test ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES})

One important detail to keep in mind is to place the target_link_libraries after the add_executable (or add_library) line.

Dodgson answered 20/10, 2013 at 13:56 Comment(3)
An important detail is to place target_link_libraries after the add_executable and find_package lines, so all linked components are known.Clearstory
.. or, if you are making a library, to place it after add_library.Odelle
How do I know if the name is PKGNAME_INCLUDE_DIR or PKGNAME_INCLUDE_DIRS ?Ema

© 2022 - 2024 — McMap. All rights reserved.