SDL2 Cmake对Win10 MinGW上的'WinMain @ 16'的未定义引用

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

我正在尝试使用MinGW在CLion中设置SDL,但是接连发生错误,而且我无法想象哪里出了问题。现在,我有了这个Cmake代码,并且得到了错误undefined reference to 'WinMain@16'。我想我忘了链接一些东西,可能是SDLmain,但是我想我是在target_link_libraries()中写的。我将不胜感激一些帮助使其工作。

cmake_minimum_required(VERSION 3.15)
project(SDL_project)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

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

add_executable(SDL_project main.cpp)

target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY} -lmingw32 -lSDL2main -lSDL2 -mwindows)

错误消息:

    ====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
Scanning dependencies of target SDL_project
[ 50%] Building CXX object CMakeFiles/SDL_project.dir/main.cpp.obj
[100%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o): in function `main':
D:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
Makefile:117: recipe for target 'SDL_project' failed

而且我还在项目文件夹中创建了一个cmake文件夹,以查找SDL,但不确定是否有必要。

set(FIND_SDL2_PATHS D:/SDL2/SDL2-devel-2.0.12-mingw/SDL2-2.0.12/x86_64-w64-mingw32)

find_path(SDL2_INCLUDE_DIR SDL2
        PATH_SUFFIXES include
        PATHS ${FIND_SDL2_PATHS})

find_library(SDL2_LIBRARY
        NAMES SDL2
        PATH_SUFFIXES lib
        PATHS ${FIND_SDL2_PATHS})
cmake mingw static-libraries clion
1个回答
0
投票

解决方案是在末尾添加新行:add_definitions(-DSDL_MAIN_HANDLED)

现在CMakeLists.txt代码看起来像这样:

cmake_minimum_required(VERSION 3.15)
project(SDL_project)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

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

add_executable(SDL_project main.cpp)

target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY} -lmingw32 -mwindows)
add_definitions(-DSDL_MAIN_HANDLED)

并且在名为cmake的单独文件夹中的项目文件夹中,有FindSDL2.cmake文件,用于找到SDL2:

set(FIND_SDL2_PATHS D:/SDL2/SDL2-devel-2.0.12-mingw/SDL2-2.0.12/x86_64-w64-mingw32)

find_path(SDL2_INCLUDE_DIR SDL2
        PATH_SUFFIXES include
        PATHS ${FIND_SDL2_PATHS})

find_library(SDL2_LIBRARY
        NAMES SDL2 SDL2main
        PATH_SUFFIXES lib
        PATHS ${FIND_SDL2_PATHS})

从CMakeLists.txt中,我删除了-lSDL2和-lSDL2main标志,因为它们导致错误:

====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
[ 50%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2
A:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
Makefile:117: recipe for target 'SDL_project' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2

此外,使用64位mingw-w64也很重要,因为mingw-w32也会产生错误,例如:

====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
[ 50%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\SDL_project.dir/objects.a(main.cpp.obj):D:/SDL_project/main.cpp:17: undefined reference to `SDL_Init'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\SDL_project.dir/objects.a(main.cpp.obj): in function `main':
D:/SDL_project/main.cpp:19: undefined reference to `SDL_GetError'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:24: undefined reference to `SDL_CreateWindow'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:27: undefined reference to `SDL_GetError'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:32: undefined reference to `SDL_GetWindowSurface'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:35: undefined reference to `SDL_MapRGB'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:35: undefined reference to `SDL_FillRect'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:38: undefined reference to `SDL_UpdateWindowSurface'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:41: undefined reference to `SDL_Delay'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:45: undefined reference to `SDL_DestroyWindow'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:48: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
Makefile:117: recipe for target 'SDL_project' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2

现在只剩下一个谜:Process finished with exit code -1073741515 (0xC0000135)

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