找不到 SDL.h - Windows、MingW、Cmake 和 SLD2(在 VSCode 和 CPP 中)

Cant find SDL.h - Windows, MingW, Cmake and SLD2 (in VSCode with CPP)

我想将 SDL2 添加到 Cmake 项目中,在 VSCode 中使用 C++ on Windows,但使用 Msys2 (g++) 中的 Mingw64。

这是我现在的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(TileGameStudio_Runtime LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(PROJ_SRC
    ${PROJECT_SOURCE_DIR}/src
)

add_subdirectory(${PROJECT_SOURCE_DIR}/SDL2)
include_directories(${PROJECT_SOURCE_DIR}/SDL2/include)

file(GLOB PROJECT_SOURCES CONFIGURE_DEPENDS
    ${PROJ_SRC}/*.cpp
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}\Game")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}\Game")

# add the executable
add_executable(TileGameStudio_Runtime
    ${PROJECT_SOURCES}
)

target_link_libraries(TileGameStudio_Runtime PRIVATE
    SDL2-static
    SDL2main
)

set_target_properties(
    TileGameStudio_Runtime
    PROPERTIES
        OUTPUT_NAME "Game"
        SUFFIX ".exe"
)

This is my current Project Structure (img)

如你所见。我从 https://github.com/libsdl-org/SDL 克隆了 Repo 作为我项目的子目录。而这个,我通过 cmake 添加 add_subdirectory.

我的Main.cpp就是这样:

#include <iostream>
#include <vector>
#include <string>
#include <SDL.h>

#ifdef main
# undef main
#endif /* main */

using namespace std;

int main(int argc, char *argv[]) {
    SDL_Window *win = NULL;
    SDL_Renderer *renderer = NULL;
    int posX = 100, posY = 100, width = 320, height = 240;

    SDL_Init(SDL_INIT_VIDEO);

    win = SDL_CreateWindow("Hello World", posX, posY, width, height, 0);

    renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
    //================================================ Draw a Text
    //this opens a font style and sets a size
    TTF_Font* Sans = TTF_OpenFont("Sans.ttf", 24);
    // this is the color in rgb format,
    // maxing out all would give you the color white,
    // and it will be your text's color
    SDL_Color White = {255, 255, 255};

    // as TTF_RenderText_Solid could only be used on
    // SDL_Surface then you have to create the surface first
    SDL_Surface* surfaceMessage = TTF_RenderText_Solid(Sans, "put your text here", White); 
    SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
    SDL_Rect Message_rect; //create a rect
    Message_rect.x = 0;  //controls the rect's x coordinate 
    Message_rect.y = 0; // controls the rect's y coordinte
    Message_rect.w = 100; // controls the width of the rect
    Message_rect.h = 100; // controls the height of the rect
    SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
    //================================================ Draw a Text End

    while (1) {
        SDL_Event e;
        if (SDL_PollEvent(&e)) {
            if (e.type == SDL_QUIT) {
                break;
            }
        }
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);

    SDL_Quit();

    return 0;
}

应该创建一个 Window.. 并可能在其中绘制文本。 但是,SDL.h 找不到...所以我不能在这里构建这些东西..

我更新了 CMakeLists.txt,因为我重新安装了整个 msys2(由于安装问题)。 这是新的:

cmake_minimum_required(VERSION 3.5)

project(TileGameStudio_Runtime LANGUAGES CXX)

set(CMAKE_CXX_FLAGS -v)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(PROJ_SRC
    ${PROJECT_SOURCE_DIR}/src
)

file(GLOB PROJECT_SOURCES CONFIGURE_DEPENDS
    ${PROJ_SRC}/*.cpp
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}\Game")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}\Game")

INCLUDE(FindPkgConfig)
pkg_check_modules(SDL2 REQUIRED sdl2)
pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image)
pkg_check_modules(SDL2_TTF REQUIRED SDL2_ttf)
pkg_check_modules(SDL2_MIXER REQUIRED SDL2_mixer)

include_directories(
    ${SDL2_INCLUDE_DIRS}
    ${SDL2_IMAGE_INCLUDE_DIRS}
    ${SDL2_TTF_INCLUDE_DIRS}
    ${SDL2_MIXER_INCLUDE_DIRS}
)

link_directories (
    ${SDL2_LIBRARY_DIRS}
    ${SDL2_IMAGE_LIBRARY_DIRS}
    ${SDL2_TTF_LIBRARY_DIRS}
    ${SDL2_MIXER_LIBRARY_DIRS}
)

# add the executable
add_executable(TileGameStudio_Runtime
    ${PROJECT_SOURCES}
)

target_link_libraries (TileGameStudio_Runtime 
    ${SDL2_LIBRARIES}
    ${SDL2_IMAGE_LIBRARIES}
    ${SDL2_TTF_LIBRARIES}
    ${SDL2_MIXER_LIBRARIES}
)

set_target_properties(
    TileGameStudio_Runtime
    PROPERTIES
        OUTPUT_NAME "Game"
        SUFFIX ".exe"
)

我现在通过msys(在msys2的MingW64模式下)安装了整个mingw64工具链和SDL2。

它仍然找不到 SDL.h 或 SDL2/SDL.h

我检查了 Cmake GUI 中的 SDL2_Dir.. 它有正确的路径:“C:/msys64/mingw64/lib/cmake/SDL2”

编辑: 我现在将我所有项目(虚幻、统一等)的主文件夹从“游戏设计”更改为“Game_Design”。现在像魅力一样工作......原因..空间是邪恶的..非常邪恶的... 感谢您的帮助尝试 :D <3