简体   繁体   English

无法让 SDL2 在 Windows 10 上与 MinGW、VSCode 和 CMake 一起使用

[英]Can't get SDL2 to work with MinGW, VSCode and CMake on Windows 10

I want to use VS Code, mingw and Cmake on Windows 10. I made a simple app:我想在 Windows 10 上使用 VS Code、mingw 和 Cmake。我做了一个简单的应用程序:

-SDL2
-main.cpp
-CMakeLists.txt

This is the current content of my CMakeLists.txt这是我的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:我的 SDL2 文件夹中还有一个SDL2Config.cmake


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> .构建此结果会导致未找到 'SDL.h',这是包含#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.我也尝试过“vcpkg”,它带有tripletsx64-windows三元组允许我找到该文件,但它无法编译,因为它是为 VSCommunity 设计的。 I tried the other triplets for mingw x64-mingw-dynamic and x64-mingw-static but they both failed building the package.我尝试了 mingw x64-mingw-dynamicx64-mingw-static的其他三元组,但它们都未能构建包。 This is the command I used to install SDL2 vcpkg install SDL2:triplet where triplet is one of the above mentioned.这是我用来安装 SDL2 vcpkg install SDL2:triplet ,其中triplet是上述之一。

If you use vcpkg you need to set the toolchain file somewhere.如果使用vcpkg ,则需要在某处设置工具链文件。 Either in CMakeLists.txt or you can set it in a command line argument:CMakeLists.txt或您可以在命令行参数中设置它:

1. CMakeLists.txt : 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) 2. 作为命令行参数(我个人更喜欢这个,以便于移植)

cmake .. -DCMAKE_TOOLCHAIN_FILE=C:<your-path>/vcpkg/scripts/buildsystems/vcpkg.cmake

Read this on vcpkg integration .阅读有关vcpkg 集成的内容
The manifest mode is the ultimate pro tip, as this leads to clean CMakeLists.txt file and a fully portable (and painless) process. 清单模式是最终的专业提示,因为这会导致干净的CMakeLists.txt文件和完全可移植(且无痛)的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM