0

I'm a newbie in CMake, i know there are many StackOverflow post asking this, but none of those posts helped me fix this error, this is what my workspace looked like

project
    .vscode
    build
    include\SDL2
    lib\SDL2_x64
    src
        CMakeLists.txt
        main.cpp
    CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(project VERSION 0.1.0)

include(CTest)
enable_testing()

add_subdirectory(src)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

src/CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)

enable_testing()


add_executable(${CMAKE_PROJECT_NAME} main.cpp)

get_filename_component(PARENT_DIRECTORY ../ ABSOLUTE)

find_library(
    SDL2_X64_LIB
    PATHS "${PARENT_DIRECTORY}/lib"
)

find_path(
    INCLUDE_DIR
    PATHS "${PARENT_DIRECTORY}/include"
)

target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC "${SDL2_X64_LIB}")
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC "${INCLUDE_DIR}")

# For debugging
message(STATUS "SDL2_LIB => ${SDL2_X64_LIB}")
message(STATUS "include_DIR => ${INCLUDE_DIR}")

I'm trying to add SDL2 include and library folder to CMake but it didn't find the file path

-- SDL2_LIB => SDL2_X64_LIB-NOTFOUND
-- include_DIR => INCLUDE_DIR-NOTFOUND
-- Configuring done

I tried putting the path into set() but it still can't find SDL2/SDL.h, I'm using the latest version of CMake

TheNoobProgrammer
  • 1,013
  • 4
  • 10
  • 21
  • In your `find_library` call you forgot to specify **name of the library** you are searching for. See [documentation](https://cmake.org/cmake/help/latest/command/find_library.html) for that command. The similar problem is with `find_path` call. – Tsyvarev Jul 30 '21 at 06:25
  • You can also pass to `cmake` additional parameter `--debug-find`, so CMake will print paths which are actually searched. Then you may compare these paths with the path to the library file you actually have. – Tsyvarev Jul 30 '21 at 06:49
  • Can you tell us which SDL2 release you downloaded from the SDL2 website? – Botje Jul 30 '21 at 07:39
  • I downloaded the SDL2 development libraries for MinGW – TheNoobProgrammer Jul 30 '21 at 07:58
  • I marked you question as a duplicate, because your ultimate task is to link with the library which location is already known for you. And while you state "i know there are many StackOverflow post asking this, but none of those posts helped me fix this error" I see no evidence in your question post that you have tried at least a single approach **correctly**: call a function without missing arguments, pass correct paths, etc. – Tsyvarev Jul 30 '21 at 09:05

1 Answers1

0

The proper solution is to create an IMPORTED target from the files you downloaded and link to it. You grabbed the MinGW version, so I am focusing on that: Add this to your CMakeLists.txt in the root:

add_library(SDL2 STATIC IMPORTED)
set_target_properties(SDL2 PROPERTIES
  IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/lib/SDL2_x64/libSDL2.a"
  INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")

And then you link to that target in your other CMake file:

target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC SDL2)
Botje
  • 26,269
  • 3
  • 31
  • 41