在cmake中将static lib链接到exe(在Linux上)

时间:2016-01-03 17:20:02

标签: c cmake

我有以下CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(cruceGame)

set(INCLUDE_PATH
    src/
    src/cli
    src/gameProtocol
    src/irc
    src/libCruceGame
    src/networking)

set(cruceGame_SOURCES
    src/singlePlayerCurses/singlePlayerCurses.c
    src/main.c)

file(GLOB libCruceGame_SOURCES "src/libCruceGame/*.c")

file(GLOB libIrc_SOURCES "src/irc/*.c")

file(GLOB libNetworking_SOURCES "src/networking/*.c")

file(GLOB libGameProtocol_SOURCES "src/gameProtocol/*.c")

file(GLOB libCli_SOURCES "src/cli/*.c")

set(COMPILE_FLAGS "-std=gnu99 -DGAME_HELP_MANUAL")
set(LINK_FLAGS "-lncursesw -lpthread")

include_directories(${INCLUDE_PATH})

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMPILE_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINK_FLAGS}")

add_library(CruceGame STATIC ${libCruceGame_SOURCES})
add_library(Irc STATIC ${libIrc_SOURCES})
add_library(Networking STATIC ${libNetworking_SOURCES})
add_library(GameProtocol STATIC ${libGameProtocol_SOURCES})
add_library(Cli STATIC ${libCli_SOURCES})

file(GLOB LIBS "*.a")

add_executable(cruceGame ${cruceGame_SOURCES})

静态库的编译是可以的,但我不知道如何将它们链接到最终的可执行文件,并且我在可执行链接上得到了很多“未定义的引用”错误。那么,我该怎么做呢?我试过:target_link_libraries(cruceGame $ {LIBS}),但它不起作用。

谢谢!

1 个答案:

答案 0 :(得分:1)

target_link_libraries(cruceGame Irc Networking GameProtocol ...)一样简单。

PS:命名库和可执行目标的名称只有不同的情况并不是一个好主意。我怀疑,这会导致Windows出现问题。