如何解决CMake + XCode 4路径依赖关系问题?

3

我的项目结构如下:

Libs/
Apps1/
Apps2/

在每个文件夹下都有一个 CMakeLists.txt。我想为每个文件夹生成一个项目文件,其中每个 AppsN 引用了 Libs。我采用的方法是调用 CMake 的 add_subdirectory(../Libs/Source/LibN) 等命令。
但是当我这样做时,CMake 会提示必须为二进制输出文件夹指定唯一的绝对路径。
参见此帖子: Xcode dependencies across different build directories? 当构建输出文件夹针对每个目标都是唯一的时,XCode 无法处理依赖关系。它需要一个文件夹。CMake 默认情况下就可以做到这一点,只不过在文件夹不是子目录时,它会拒绝执行。
我尝试在创建目标后更改输出路径。这将把对象构建到输出文件夹中,XCode 可以看到它们,但是在 CMake 脚本中对该目标的所有引用都将使用唯一路径。
提出的解决方案包括:
  • App1/Projects/Subdir中包含项目文件,并在不相关的位置复制项目
  • 将我的文件夹重新组织到共享父文件夹中,以避免这种 CMake 的疯狂方式,这对我来说存在一些安全问题(因为某些目录不是公共的)
  • 永远不要用 CMake 名称引用目标,而要使用共享路径名称。不确定如何正确地做到这一点
  • 尝试在 CMake 方面进行修补
  • 切换到 premake

我会让这个问题保持开放状态,但最终我选择了我的第一个选项。我使用add_custom_target将必要的CMakeLists.txt复制到当前目录中,这样就可以很好地解决问题了。 - nullspace
1个回答

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)

LibsCMakeLists.txt 文件中:
add_subdirectory(Source)

SourceCMakeLists.txt 文件中:
add_subdirectory(Lib1)
# Lib2 could depend on Lib1
add_subdirectory(Lib2)

这样所有的应用程序都可以使用所有的库。所有的二进制文件将被制作成您的二进制文件${root}/bin

一个示例库:

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
)

这是一个简单而有效的例子。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接