3

我的项目结构如下:

Libs/
Apps1/
Apps2/

每个文件夹中都有一个CMakeLists.txt. 我想为每个文件夹和每个AppsN引用生成一个项目文件Libs。我这样做的方法是调用 CMakeadd_subdirectory(../Libs/Source/LibN)等。

现在当我这样做时,CMake 说add_subdirectory必须为二进制输出文件夹指定一个唯一的绝对路径。

看到这个帖子:

跨不同构建目录的 Xcode 依赖项?

当每个目标的构建输出文件夹是唯一的时,XCode无法处理依赖项。它需要一个文件夹。CMake 默认情况下会这样做,它只是在文件夹不是子目录时拒绝。

创建目标后,我尝试更改和更改输出路径。这会将对象构建到输出文件夹,XCode 会看到它们,但是 CMake 脚本中对该目标的所有引用都将使用唯一路径。

建议的解决方案是:

  • 在不相关的位置包含项目文件App1/Projects/Subdir和复制项目
  • 将我的文件夹重新组织到一个共享的父文件夹以避免这种 CMake 疯狂,这给我带来了一些安全问题(因为一些目录是不公开的)
  • 永远不要通过其 CMake 名称来引用目标,而是使用共享路径名。不确定如何正确执行此操作
  • 尝试以某种方式在 CMake 方面进行修补
  • 切换到预制
4

1 回答 1

2

尝试将以下内容添加到 root 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

一个示例库:

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
)

这是一个愚蠢而有效的例子。

于 2012-05-18T12:50:37.147 回答