我的项目结构如下:
Libs/
Apps1/
Apps2/
每个文件夹中都有一个CMakeLists.txt
。我想为每个文件夹生成一个项目文件,每个AppsN
引用Libs
。我这样做的方法是调用CMake的add_subdirectory(../Libs/Source/LibN)
等。
现在,当我这样做时,CMake说add_subdirectory
必须为二进制输出文件夹指定一个唯一的绝对路径。
见这篇文章:
Xcode dependencies across different build directories?
当构建输出文件夹对每个目标唯一时,XCode 无法处理依赖关系。它需要一个文件夹。并且CMake默认执行此操作,它只是在文件夹不是子目录时拒绝。
我尝试在创建目标后更改和更改输出路径。这会将对象构建到输出文件夹,XCode会看到它们,但是CMake脚本中对此目标的所有引用都将使用唯一路径。
建议的解决方案是:
App1/Projects/Subdir
中包含项目文件,在不相关的位置包含重复的项目答案 0 :(得分:2)
尝试将以下内容添加到根CMakeLists.txt
:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0)
PROJECT (ContainerProject)
SET (LIBRARY_OUTPUT_PATH ${ContainerProject_BINARY_DIR}/bin CACHE PATH
"Single output directory for building all libraries.")
SET (EXECUTABLE_OUTPUT_PATH ${ContainerProject_BINARY_DIR}/bin CACHE PATH
"Single output directory for building all executables.")
MARK_AS_ADVANCED(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)
# for common headers (all project could include them, off topic)
INCLUDE_DIRECTORIES(ContainerProject_SOURCE_DIR/include)
# for add_subdirectory:
# 1) do not use relative paths (just as an addition to absolute path),
# 2) include your stuffs in build order, so your path structure should
# depend on build order,
# 3) you could use all variables what are already loaded in previous
# add_subdirectory commands.
#
# - inside here you should make CMakeLists.txt for all libs and for the
# container folders, too.
add_subdirectory(Libs)
# you could use Libs inside Apps, because they have been in this point of
# the script
add_subdirectory(Apps1)
add_subdirectory(Apps2)
Libs
CMakeLists.txt
:
add_subdirectory(Source)
Source
CMakeLists.txt
:
add_subdirectory(Lib1)
# Lib2 could depend on Lib1
add_subdirectory(Lib2)
通过这种方式,所有Apps
都可以使用所有库。所有二进制文件都将生成二进制文件${root}/bin
。
示例lib:
PROJECT(ExampleLib)
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
SET(ExampleLibSrcs
...
)
ADD_LIBRARY(ExampleLib SHARED ${ExampleLibSrcs})
示例可执行文件(具有依赖性):
PROJECT(ExampleBin)
INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${ExampleLib_SOURCE_DIR}
)
SET(ExampleBinSrcs
...
)
# OSX gui style executable (Finder could use it)
ADD_EXECUTABLE(ExampleBin MACOSX_BUNDLE ${ExampleBinSrcs})
TARGET_LINK_LIBRARIES(ExampleBin
ExampleLib
)