Search code examples
linuxcmakeclionglfw

How do I write the cmake script for clion to include glfw?


cmake_minimum_required(VERSION 3.15)
project(hello)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
set(CMAKE_CXX_STANDARD 11)

add_executable(hello main.cpp)
INCLUDE_DIRECTORIES(${GLFW_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(hello ${GLFW_STATIC_LIBRARIES})

It tells me

CMake Error at /home/user/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/193.5233.144/bin/cmake/linux/share/cmake-3.15/Modules/FindPkgConfig.cmake:696 (message):
  None of the required 'glfw3' found

when I try to build it. My glfw folder is located at /usr/local/include/GLFW.


Solution

  • AFAIK, glfw3 is using CMake as the build system, (src: packages.debian.org/fr/sid/amd64/libglfw3-dev/filelist) which uses modern CMake, so you don't need GLFW_INCLUDE_DIRS etc...

    Inside this file /usr/lib/cmake/glfw3/glfw3Targets.cmake (loaded by /usr/lib/cmake/glfw3/glfw3Config.cmake), you'll see:

    ...
    # Create imported target glfw
    add_library(glfw SHARED IMPORTED)
    
    set_target_properties(glfw PROPERTIES
      INTERFACE_COMPILE_DEFINITIONS "GLFW_DLL"
      INTERFACE_INCLUDE_DIRECTORIES "/usr/include"
    )
    ...
    

    So you can simply use:

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

    ps: same as my previous comment