我有以下 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})
静态库的编译没问题,但我不知道如何将它们链接到最终的可执行文件,而且我在可执行文件链接时遇到很多“ undefined reference ”错误。那么,我该怎么做呢?我尝试了:target_link_libraries(cruceGame ${LIBS})
,但它不起作用。
谢谢!
就像 target_link_libraries(cruceGame Irc Networking GameProtocol ...)
一样简单。
PS:用仅区分大小写的名称命名库和可执行目标不是一个好主意。我怀疑这会导致 Windows 出现问题。
我是一名优秀的程序员,十分优秀!