Search code examples
cmakeclionpkg-config

Cmakelist working outside of Clion


I've wanted to use Clion for awhile but I've always had trouble with Cmake. Armed with Cygwin, I've almost gotten this stupid thing to work.

The issue is while I can compile a cmake file from within a cygwin terminal, in Clion I am told it cannot find the library I want.

Error:A required package was not found

The cmakelist.txt file

cmake_minimum_required(VERSION 3.3)
project(Test)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    set(PKG_CONFIG_PATH /usr/lib/pkgconfig)
    set(PKG_CONFIG_EXECUTABLE /usr/bin/pkg-config.exe)
    set(SOURCE_FILES main.cpp)
    add_executable(Test ${SOURCE_FILES})

    INCLUDE(FindPkgConfig)

    pkg_check_modules(SDL2 REQUIRED "sdl2")

    MESSAGE(STATUS "SDL library:    " ${SDL2_LDFLAGS})

    TARGET_LINK_LIBRARIES(Test ${SDL2_LDFLAGS})

I have no idea if setting the variables PKG_CONFIG_PATH and others work, but they successfully build a makefile for my use in cygwin that builds correctly.

I've deleted the cache, remade the project and everything. It just refuses to work in Clion


Solution

  • If I understood correctly, your cmake config is unable to find SDL library. I found it better to use find_package command instead of pkg_check_modules. In order to find_package(SDL2) to work, there must be FindSDL2.cmake module in directory, specified by CMAKE_MODULE_PATH variable (usually, it is cmake/Modules directory inside your source tree).

    FindSDL2.cmake is not a part of CMake, but you can find one online easily (check my own modules, for example: https://github.com/dragn/cmake-modules). Refer to this doc for details: https://cmake.org/Wiki/CMake:How_To_Find_Libraries.

    Put FindSDL2.cmake to cmake/Modules directory and add this to your CMakeLists.txt:

    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake/Modules)
    find_package(SDL2 REQUIRED)
    include_directories(${SDL2_INCLUDE_DIR})
    
    ...
    
    target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
    

    NOTE: Sadly, it appears that Leonardo has not succeeded in finding volunteers for maintaining FindSDL2.cmake in SDL community: https://cmake.org/Bug/view.php?id=14826.