SDL2 和 CMake 设置有问题

问题描述 投票:0回答:1

你好,

我正在使用 C++、SDL2 和 CMake 创建游戏(在 Visual Studio Code 中)。 遗憾的是我收到一个错误,找不到 SDL2,我尝试了几个小时的很多事情,所以现在我打开这篇文章。

我尝试了多种方法来做到这一点。我当前的解决方案是,我有一个名为“CMAKE_PREFIX_PATH”的环境变量设置为“G:\Coding\sdks”,该文件夹中的“SDL2-2.28.4”是从 GitHub 下载的 SDL2-VC。

SDL.h: No such file or directory

这是我的CMakeLists.txt

cmake_minimum_required(VERSION 3.26)
project(sdl2_tutorial)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin")

set(SOURCE_FILES src/main.cpp src/game.cpp src/game.hpp)

include_directories(.)

add_executable(sdl2_tutorial ${SOURCE_FILES})
target_link_libraries(sdl2_tutorial SDL2::SDL2 SDL2::SDL2main)

这是尝试运行或构建项目时的输出:

[main] Building folder: sdl2_tutorial 
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build g:/Coding/sdl2_tutorial/build --config Debug --target ALL_BUILD -j 18 --
[build] MSBuild version 17.6.3+07e294721 for .NET Framework
[build] 
[build]   main.cpp
[build] SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol SDL_main referenced in function main_getcmdline [G:\Coding\sdl2_tutorial\build\sdl2_tutorial.vcxproj]
[build]     Hint on symbols that are defined and could potentially match:
[build]       "int __cdecl SDL_main(int,char const * * const)" (?SDL_main@@YAHHQEAPEBD@Z)
[build] G:\Coding\sdl2_tutorial\bin\Debug\sdl2_tutorial.exe : fatal error LNK1120: 1 unresolved externals [G:\Coding\sdl2_tutorial\build\sdl2_tutorial.vcxproj]
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build g:/Coding/sdl2_tutorial/build --config Debug --target ALL_BUILD -j 18 -- exited with code: 1
[driver] Build completed: 00:00:02.242
[build] Build finished with exit code 1

这里是找不到SDL.h的头文件

#include <iostream>

#include <SDL.h>

class Game {
public:
    Game();
    ~Game();

    void init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);

    void handleEvents();
    void update();
    void render();
    void clean();

    bool running() {return isRunning;}
private:
    bool isRunning;
    SDL_Window *window;
    SDL_Renderer *renderer;
};
c++ c cmake sdl sdl-2
1个回答
0
投票

嗯,我在 SDL2 上也遇到了类似的问题。我不确定你的问题是什么,但是一个帮助我的解决方案是在 include 编译器指令中包含 dll 的文件结构,例如,我这样做了:

#include <SDL2/SDL.h>

不是

#include <SDL.h>

不确定这是否有帮助,因为我不使用 Visual Studio。然而,它可能值得尝试。

© www.soinside.com 2019 - 2024. All rights reserved.