Issue
I want to use VS Code, mingw and Cmake on Windows 10. I made a simple app:
-SDL2
-main.cpp
-CMakeLists.txt
This is the current content of my CMakeLists.txt
cmake_minimum_required(VERSION 3.19.0)
project(main VERSION 1.0.0)
list(APPEND CMAKE_PREFIX_PATH SDL2)
find_package(SDL2 REQUIRED)
add_executable(main main.cpp)
target_link_directories(main PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(main PRIVATE ${SDL2_LIBRARIES})
I also have a SDL2Config.cmake
inside my SDL2 folder:
set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include")
# Support both 32 and 64 bit builds
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x64/SDL2main.lib")
else ()
set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/lib/x86/SDL2main.lib")
endif ()
string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
Building this results in 'SDL.h' not found, this is the include #include<SDL2.h>
.
I also tried "vcpkg", this comes with triplets
, the x64-windows
triplet allows me to find the file but it wont compile since it's meant for VSCommunity. I tried the other triplets for mingw x64-mingw-dynamic
and x64-mingw-static
but they both failed building the package. This is the command I used to install SDL2 vcpkg install SDL2:triplet
where triplet
is one of the above mentioned.
Solution
If you use vcpkg
you need to set the toolchain file somewhere.
Either in CMakeLists.txt
or you can set it in a command line argument:
1. CMakeLists.txt
:
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake
CACHE STRING "Vcpkg toolchain file")
NB! Make sure that you get the path right!
2. As command line argument (I personally prefer this for easier portability)
cmake .. -DCMAKE_TOOLCHAIN_FILE=C:<your-path>/vcpkg/scripts/buildsystems/vcpkg.cmake
Read this on vcpkg integration.
The manifest mode is the ultimate pro tip, as this leads to clean CMakeLists.txt
file and a fully portable (and painless) process.
Answered By - Tamás Panyi