Building GLFW3 Application with CMAKE - GLFW_LIBRARIES doesnt set
Asked Answered
B

2

11

I'm attempting to build a small project using glfw3 but no matter what I do I can't get pkgconfig to set GLFW_LIBRARIES.

Here is my CMakeList.txt

cmake_minimum_required(VERSION 3.3)
project(LearnGLSL)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

if (CMAKE_BUILD_TYPE STREQUAL "")
    set(CMAKE_BUILD_TYPE Debug)
endif()


if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/debug")
    set(PROJECT_BINARY_DIR "${CMAKE_SOURCE_DIR}/build/debug")
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")


file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})


find_package(OpenGL REQUIRED)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GLFW REQUIRED glfw3)



include_directories(
        ${OPENGL_INCLUDE_DIR}
        ${GLFW_INCLUDE_DIRS}
)


set(SOURCE_FILES main.cpp gl_core_4_3.cpp)

message(WARNING "${GLFW_LIBRARIES}")

add_executable(LearnGLSL ${SOURCE_FILES})
target_link_libraries(LearnGLSL ${OPENGL_gl_LIBRARY} ${GLFW_LIBRARIES})


add_custom_command(TARGET LearnGLSL  POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/assets
        ${PROJECT_BINARY_DIR}
        COMMENT "Copy resources to build tree")

Here is where glfw3 is installed

-- Installing: /usr/local/include/GLFW
-- Installing: /usr/local/include/GLFW/glfw3native.h
-- Installing: /usr/local/include/GLFW/glfw3.h
-- Installing: /usr/local/lib/cmake/glfw/glfw3Config.cmake
-- Installing: /usr/local/lib/cmake/glfw/glfw3ConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/glfw/glfwTargets.cmake
-- Installing: /usr/local/lib/cmake/glfw/glfwTargets-noconfig.cmake
-- Installing: /usr/local/lib/pkgconfig/glfw3.pc
-- Installing: /usr/local/lib/libglfw3.a

I'll be the first to admit I'm not super comfortable with CMAKE but this seems simple enough and I've done everything I can google to find. maybe its a typo i'm not noticing. Any help is appreciated thanks

Oh i forgot to mention I get undefined references to the glfw functions when building this project. I assumed this is a result of GLFW_LIBRARIES not properly getting set tho.

Beater answered 19/12, 2015 at 5:0 Comment(0)
L
14

I don't know about finding GLFW with pkgconfig but I don't think you need pkgconfig in this case. Since GLFW itself builds with CMake it should install a native CMake config module, which it does.

Well, almost. The official GLFW CMake config-module support is a bit buggy as of v3.1.2. Instead, use shaxbee's fork or the adasworks fork (based on shaxbee's but newer)

With that GLFW all you need to find it is just 2 lines:

find_package(glfw3 REQUIRED)
...
target_link_libraries(LearnGLSL ... glfw)

I also found a few other problems in your CMakeLists.txt so I repeat the whole script, revised:

cmake_minimum_required(VERSION 3.3)
project(LearnGLSL)

set(CMAKE_CXX_STANDARD 11) # no explicit compiler flags if possible

# don't read CMAKE_BUILD_TYPE, it has no meaning with multiconfig
# generators
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_SOURCE_DIR}/build/debug")

# PROJECT_BINARY_DIR should not be set at all
# You establish the BINARY_DIR with the initial cmake command
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})

find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)

include_directories(${OPENGL_INCLUDE_DIR})

add_executable(LearnGLSL main.cpp gl_core_4_3.cpp)
target_link_libraries(LearnGLSL ${OPENGL_gl_LIBRARY} glfw)

add_custom_command(TARGET LearnGLSL POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CMAKE_SOURCE_DIR}/assets
    ${PROJECT_BINARY_DIR}
    COMMENT "Copy resources to build tree")
Lecompte answered 19/12, 2015 at 21:37 Comment(1)
For target link libraries, one can use ${GLFW3_LIBRARY} instead of glfw.Apodictic
H
0

Since CMake 3.1 pkg_check_modules uses additional paths from CMAKE_PREFIX_PATH variable for search .pc files. Searching is performed in similar manner as in command find_library, but additional subdirectory pkgconfig/ is added to the resulted path. Specifically, for each <prefix> in CMAKE_PREFIX_PATH, .pc file is searched in next directory:

<prefix>/lib[64]/[<arch>/]pkgconfig

(suffix 64 and arhitecture-specific subdirectory is added when appropriate).

So having file /usr/local/lib/pkgconfig/glfw3.pc, you need to set CMAKE_PREFIX_PATH to /usr/local for pkg_check_modules is able to find it. Variable can be set either:

1) In the CMakeLists.txt script itself, or

2) In the command line

cmake -DCMAKE_PREFIX_PATH=<...> <source-dir>

3) As environment one (OS-dependent).


Before CMake 3.1 (and after it) additional search directory can be specified via PKG_CONFIG_PATH environment variable.

E.g. with file /usr/local/lib/pkgconfig/glfw3.pc variable PKG_CONFIG_PATH should contain /usr/local/lib/pkgconfig.

Hirza answered 19/12, 2015 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.