gpt4 book ai didi

c++ - 如何在CMake中设置此文件夹结构?

转载 作者:行者123 更新时间:2023-12-01 14:42:48 27 4
gpt4 key购买 nike

我正在尝试从Xcode项目切换到基于CMake的项目。 (Xcode将所有项目配置工作自动化)。我有这个项目设置:

game/ (Includes the core utils used by the client and the server. Has libraries -llua, -lbullet, etc.)
| util.cpp
| CMakeLists.txt
game-client/ (The client. Has libraries -lglfw)
| window.cpp
| CMakeLists.txt
game-server/ (The server)
| server.cpp
| CMakeLists.txt
game-test/ (Tests source files in the game/ directory)
| util-tester.cpp

game-client /和game-server /都取决于game /。但是,当我尝试将 add_subdirectory(../tdgame)添加到game-client / CMakeLists.txt文件时,它告诉我:
"game/" is not a subdirectory of "game-client/".  When specifying an
out-of-tree source a binary directory must be explicitly specified.

那么我该如何设置呢?我以前从未使用过CMake。

最佳答案

这是我设置此类项目的方法:
./CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.2)
project(my_game)

add_subdirectory(game)
add_subdirectory(game-client)
add_subdirectory(game-server)

请注意,至关重要的是应按相反的依赖关系顺序添加子目录。例如。 game-server取决于 game,因此应在它之后添加。
./game/CMakeLists.txt:
find_package(Lua REQUIRED)

add_library(game utils.cpp)
target_include_directories(game PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${LUA_INCLUDE_DIR})
target_link_libraries(game PUBLIC ${LUA_LIBRARIES})
target_include_directoriestarget_link_libraries在这里至关重要。当您使用 PUBLIC模式时,您通知 cmake,每个依赖于库的目标也应将这些目录添加到预处理器搜索路径中,如果它是可执行文件,则还应与您的依赖项链接。
./game-client/CMakeLists.txt:
add_executable(game-server main.cpp)
target_link_libraries(game-server PUBLIC game)

您还需要让 cmake知道您的可执行文件取决于您的库。

这是一个示例(示例源在 /tmp/so/game中)
$ make VERBOSE=1 | grep main.cpp
[...]
cd /tmp/a/game-client && /usr/bin/c++ -I/tmp/so/game -I/usr/include/lua5.3 -o CMakeFiles/game-client.dir/main.cpp.o -c /tmp/so/game-client/main.cpp
/usr/bin/c++ -rdynamic CMakeFiles/game-client.dir/main.cpp.o -o game-client ../game/libgame.a -llua5.3 -lm

关于c++ - 如何在CMake中设置此文件夹结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62049311/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com