我正在尝试创建一个具有 2 个目标的 CMake 项目。其中一个必须是库,另一个必须是可执行文件。但是当我尝试构建可执行文件时,我收到一条错误消息,指出找不到该库。
库 CMake:
file(GLOB SOURCES ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)
set(HEADERS include)
add_library(arcade-lib STATIC ${SOURCES})
target_include_directories(arcade-lib PUBLIC ${HEADERS})
target_link_libraries(arcade-lib PRIVATE sfml-window sfml-graphics)
可执行的CMake:
file(GLOB SOURCES ${CMAKE_CURRENT_LIST_DIR}/src/*.cpp)
set(INCLUDE include)
set(ARCADE_GAMES_LIB_INCLUDE ../arcade-games-lib/include)
add_executable(snake-game snake-game.cpp ${SOURCES})
target_link_libraries(snake-game PRIVATE arcade-games-lib sfml-graphics sfml-window sfml-system sfml-main )
target_include_directories(snake-game PRIVATE ${INCLUDE} ${ARCADE_GAMES_LIB_INCLUDE})
主要CMake:
cmake_minimum_required(VERSION 3.25)
project(arcade_games)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc")
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML 2.5 COMPONENTS graphics window system REQUIRED)
add_subdirectory(arcade-games-lib)
add_subdirectory(snake-game)
错误信息:
C:\Program Files\JetBrains\CLion 2022.3.2\bin\mingw\bin/ld.exe: cannot find -larcade-games-lib
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
我什至不知道该怎么办,因为一切看起来都很正常。我预计将构建一个包含我的库的可执行文件
add_library
正如评论中指出的,问题在于和中的库名称target_link_libraries
需要匹配。正如 @Botje 和Why is cmake file GLOB bad?
file(GLOB
中指出的那样,使用收集源文件是有问题的。我冒昧地从你的 CMakeLists.txt 中删除了一些臃肿的内容:
库 CMake:
可执行的CMake: