连接SDL2和Clion

3

我已经将clion指向了SDL2的目录和库,但是在尝试构建时它无法链接库。有什么想法如何解决这个问题吗?

CMakeLists文件:

cmake_minimum_required(VERSION 3.3)

project(cavestory_development)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -lSDL2")

set(SDL2_INCLUDE_DIR C:/SDL2-2.0.3/i686-w64-mingw32/include/SDL2)

set(SDL2_LIBRARY C:/SDL2-2.0.3/i686-w64-mingw32/lib)

find_package(SDL2 REQUIRED)

include_directories(${SDL2_INCLUDE_DIR})

set(SOURCE_FILES main.cpp)

add_executable(cavestory_development ${SOURCE_FILES})

target_link_libraries(cavestory_development ${SDL2_LIBRARY})

构建错误:

"C:\Program Files (x86)\JetBrains\CLion 1.1\bin\cmake\bin\cmake.exe" --build C:\Users\conne_000\.clion11\system\cmake\generated\8a943732\8a943732\Debug --target cavestory_development -- -j 8
[ 50%] Linking CXX executable cavestory_development.exe
CMakeFiles\cavestory_development.dir/objects.a(main.cpp.obj): In function `SDL_main':
C:/Users/conne_000/Documents/ClionProjects/cavestory_development/main.cpp:11: undefined reference to `SDL_Init'
C:/Users/conne_000/Documents/ClionProjects/cavestory_development/main.cpp:21: undefined reference to `SDL_CreateWindow'
C:/Users/conne_000/Documents/ClionProjects/cavestory_development/main.cpp:26: undefined reference to `SDL_GetError'
C:/Users/conne_000/Documents/ClionProjects/cavestory_development/main.cpp:32: undefined reference to `SDL_Delay'
C:/Users/conne_000/Documents/ClionProjects/cavestory_development/main.cpp:35: undefined reference to `SDL_DestroyWindow'
C:/Users/conne_000/Documents/ClionProjects/cavestory_development/main.cpp:38: undefined reference to `SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [cavestory_development.exe] Error 1
CMakeFiles\cavestory_development.dir\build.make:96: recipe for target 'cavestory_development.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/cavestory_development.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/cavestory_development.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/cavestory_development.dir/rule] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/cavestory_development.dir/rule' failed
mingw32-make.exe: *** [cavestory_development] Error 2
Makefile:117: recipe for target 'cavestory_development' failed
1个回答

0

这里有一个最简单的示例,让你在Windows上开始使用SDL2 (main.cpp只包含来自LazyFoo的hello SDL)

cmake_minimum_required(VERSION 3.0)
project(hello_sdl2)

# configure the SDL (cf. "SDL2-2.0.3\i686-w64-mingw32\lib\pkgconfig\sdl2.pc")
# C++ flags
set(SDL2_Flags "-mwindows -Wl,--no-undefined -static-libgcc")
# library paths
set(SDL2_ROOT     "D:/PATH/TO/SDL2-2.0.3/i686-w64-mingw32")
set(SDL2_Includes "${SDL2_ROOT}/include")
set(SDL2_LibDir   "${SDL2_ROOT}/lib")
# imported targets for CMake (cf. https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets)
add_library(SDL2     STATIC IMPORTED)
add_library(SDL2main STATIC IMPORTED)
set_property(TARGET SDL2     PROPERTY IMPORTED_LOCATION "${SDL2_LibDir}/libSDL2.a")
set_property(TARGET SDL2main PROPERTY IMPORTED_LOCATION "${SDL2_LibDir}/libSDL2main.a")
# the libs to link against
# note: as always with gcc, the order is important...
set(SDL2_Libs mingw32 SDL2 SDL2main m dinput8 dxguid dxerr8 user32 gdi32 winmm imm32 ole32 oleaut32 shell32 version uuid)

# configure the project
# include the SDL flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${SDL2_Flags}")
# collect the sources
set(SOURCE_FILES main.cpp)
# define the target
add_executable(hello_sdl2 ${SOURCE_FILES})
# include the SDL headers
target_include_directories(hello_sdl2 SYSTEM PRIVATE ${SDL2_Includes})
# link against the SDL (and its dependencies)
target_link_libraries(hello_sdl2 ${SDL2_Libs})

在Win8.1 64位上测试,使用SDL2-2.0.3, MinGW-W64 i686-5.2.0-posix-dwarf-rt_v4-rev0CLion 1.2 EAP (build 142.5239.6)


我在链接库方面遇到了问题。我的lib文件夹位于${SDL2_ROOT}/lib/x64或${SDL2_ROOT}/lib/x86。而且,文件libSDL2.a和libSDL2main.a不存在。我使用了您提供的链接来下载库。 - Siamaster
1
如果你已经下载了SDL2-2.0.3,你会在${SDL2_ROOT}/i686-w64-mingw32下找到所需的一切。参见第8行。 - maddouri
使用标志“-mwindows”会导致我的“std::cout”无法工作。 - Siamaster
1
-mwindows еҸӘжҳҜз”ЁдәҺйҡҗи—ҸжҺ§еҲ¶еҸ°зӘ—еҸЈпјҲиҝҷйҮҢжңүдёҖдәӣж—§зҡ„дҪҶд»Қ然зӣёе…ізҡ„й“ҫжҺҘ1е’Ңй“ҫжҺҘ2е…ідәҺ -mwindows зҡ„дҪҝз”ЁпјүгҖӮ std::cout иҝҗиЎҢиүҜеҘҪгҖӮ жӮЁеә”иҜҘиғҪеӨҹеңЁеҸҜжү§иЎҢж–Ү件зҡ„е·ҘдҪңзӣ®еҪ•дёӯжүҫеҲ°иҫ“еҮәзҡ„ .txt ж–Ү件гҖӮ - maddouri
有没有可能通过find_library()或其他方式避免使用绝对路径"D:/PATH/TO/SDL2-2.0.3/i686-w64-mingw32"? 绝对路径是完全不合适的。 - Alexo Po.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接