如何在 CMake 中链接 SDL2?
How to link SDL2 in CMake?
我正在尝试在 ubuntu 上使用 sdl。根据此说明(https://gist.github.com/BoredBored/3187339a99f7786c25075d4d9c80fad5(,我安装了sdl2,sdl图像和sdl混合器。现在我必须在构建时链接它们。下面我应该怎么做的例子。
g++ myProgram.cpp -o myProgram `sdl2-config --cflags --libs` -lSDL2 -lSDL2_mixer -lSDL2_image -lSDL2_ttf
我正在使用 Cmake,我不知道如何链接它们......
下面是仅用于测试 sdl 是否正常工作的代码。
//MAIN
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
}
CMakeList 如下
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
# Add an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES})
# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_include_directories(sdl
PRIVATE
${PROJECT_SOURCE_DIR}/inc
)
如何在 Cmake 中链接它们?谢谢你的时间。
要在cmake中链接库(共享/静态(,您可以使用 target_link_libraries 命令:
target_link_libraries(<target> ... <item>... ...)
根据文档:
创建
<target>
必须由命令(如add_executable()
或add_library()
因此,首先我们需要找到 SDL 库,为此我们将使用以下命令:
find_package(SDL2 REQUIRED)
要使其包含目录可供您使用,请使用以下命令:
include_directories(${SDL2_INCLUDE_DIRS})
最后,要链接 SDL2,您需要执行以下操作:
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
或者:
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)
PRIVATE
,意味着${PROJECT_NAME}
在其实现中使用SDL2
,但SDL2
不用于${PROJECT_NAME}
公共API的任何部分。更多在这里
这里${PROJECT_NAME}
是<target>
,后面的其余都是库的名称。
最终结果
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
find_package(SDL2 REQUIRED)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
# Add an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(sdl ${SDL2_LIBRARIES})
# Set the directories that should be included in the build command for this target
include_directories(SDL2Test ${SDL2_INCLUDE_DIRS})
裁判:
https://cmake.org/cmake/help/latest/command- https://cmake.org/pipermail/cmake/2016-May/063400.html
EDIT 添加了完整的CMAKE
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
target_include_directories(sdl
PRIVATE
${PROJECT_SOURCE_DIR}/inc
)
# Add an executable with the above sources
link_directories(path_to_lib)
add_executable(${PROJECT_NAME} ${SOURCES})
# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_link_libraries((${PROJECT_NAME} SDL2 SDL2_mixer SDL2_image SDL2_ttf)
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- CMake-按正确顺序将项目与C运行时对象文件链接
- 从链接列表c++中删除一个项目
- 有根的二进制搜索树.保留与其父级的链接
- 读取文件的最后一行并输入到链接列表时出错
- 静态数据成员的问题-修复链接错误会导致编译器错误
- node-gyp 在 macOS 上未正确链接库
- 基于boost的程序的静态链接——zlib问题
- 无法链接 CMake 中的本地库
- 内联函数中具有内部链接的全局变量
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 将 SDL2 与 CMake 链接起来
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- 无法在 DLL 中链接 SDL2-static.lib
- 如何在 CMake 中链接 SDL2?
- 带有 mingw-w64 的 SDL2 - 链接器找到 libSDL2.a,但抛出"undefined reference"错误
- C++SDL2-无法理解的链接错误
- SDL2 无法正确链接
- 在QtCreator中链接SDL2
- 链接器错误从SDL2在我的项目