如何在 CMake 中手动 link SDL2
How to link SDL2 manually in CMake
最近开始学习CMake。为了练习,我正在尝试手动 link SDL2。我知道还有另一种使用 find_file
的方法,这很简单。但是我想自己做练习。
当我尝试 link libSDL2main.a
文件(运行 使用 cmd mingw32-make 的 Makefile)时出现错误
[ 50%] Linking CXX executable exe0.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -llibSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\exe0.dir\build.make:105: recipe for target 'exe0.exe' failed
mingw32-make[2]: *** [exe0.exe] Error 1
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/exe0.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/exe0.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
这是我的 CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(SDL_Test_Project)
include_directories(include)
add_executable(exe0 main.cpp)
target_link_libraries(exe0 libSDL2main.a)
这里main.cpp只是一个源文件。我已经把SDL2.dll
和libSDL2main.a
放到项目目录的根目录下。 (我在 Windows 10 中使用 CMake GUI 生成了 Makefile)。
如果您想 link 直接在 target_link_libraries()
中访问 SDL2 库(无需定义 IMPORTED
目标,或使用 find_library()
),请使用 完整 每个库的路径。 CMAKE_SOURCE_DIR
变量提供CMake项目根目录的完整路径:
target_link_libraries(exe0 PRIVATE
mingw32
${CMAKE_SOURCE_DIR}/libSDL2main.a
${CMAKE_SOURCE_DIR}/SDL2.dll
)
注意,对于SLD2,使用MinGW编译时可能还需要在该命令中添加mingw32
最近开始学习CMake。为了练习,我正在尝试手动 link SDL2。我知道还有另一种使用 find_file
的方法,这很简单。但是我想自己做练习。
当我尝试 link libSDL2main.a
文件(运行 使用 cmd mingw32-make 的 Makefile)时出现错误
[ 50%] Linking CXX executable exe0.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -llibSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\exe0.dir\build.make:105: recipe for target 'exe0.exe' failed
mingw32-make[2]: *** [exe0.exe] Error 1
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/exe0.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/exe0.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
这是我的 CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(SDL_Test_Project)
include_directories(include)
add_executable(exe0 main.cpp)
target_link_libraries(exe0 libSDL2main.a)
这里main.cpp只是一个源文件。我已经把SDL2.dll
和libSDL2main.a
放到项目目录的根目录下。 (我在 Windows 10 中使用 CMake GUI 生成了 Makefile)。
如果您想 link 直接在 target_link_libraries()
中访问 SDL2 库(无需定义 IMPORTED
目标,或使用 find_library()
),请使用 完整 每个库的路径。 CMAKE_SOURCE_DIR
变量提供CMake项目根目录的完整路径:
target_link_libraries(exe0 PRIVATE
mingw32
${CMAKE_SOURCE_DIR}/libSDL2main.a
${CMAKE_SOURCE_DIR}/SDL2.dll
)
注意,对于SLD2,使用MinGW编译时可能还需要在该命令中添加mingw32