如何在cmake中链接库?

I'm trying to use sdl on ubuntu. According to this instruction(https://gist.github.com/BoredBored/3187339a99f7786c25075d4d9c80fad5) i installed sdl2, sdl image and sdl mixer. Now I have to link them while building. Example how should I do it below.

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中链接它们?谢谢你的时间。

评论
留景同
留景同

To link a library (shared/static) in cmake you can use the target_link_libraries command:

target_link_libraries(<target> ... <item>... ...)

根据文档:

must have been created by a command such as add_executable() or add_library()

因此,首先我们需要找到SDL库,为此,我们将使用以下命令:

find_package(SDL2 REQUIRED)

要使其包含对您可用的目录,请使用以下命令:

include_directories(${SDL2_INCLUDE_DIRS})

最后,要链接SDL2,您需要执行以下操作:

target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})

Here ${PROJECT_NAME} is the <target>, and all the rest that follow are names of libraries.

最后结果

# 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})
点赞
评论
Me小懒虫
Me小懒虫

编辑添加了完整的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} lSDL2 lSDL2_mixer lSDL2_image lSDL2_ttf)
点赞
评论