2012-05-17 57 views
3

我有结构化的,像这样的项目:如何解决CMake + XCode 4路径依赖关系?

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项目文件,并在无关的位置重复的项目
  • 重新安排我的文件夹复制到共享父文件夹,以避免这种CMake的疯狂,它提出了一些安全问题,对我来说(因为一些dirs不公开)
  • 决不会使用其CMake名称引用目标,而是使用共享路径名称。不知道如何做到这一点正确
  • 尝试并获得这个补丁的CMake的侧莫名其妙
  • 开关premake
+0

我要离开这个开放的,但我最终选择了我的第一选择。我使用'add_custom_target'将必要的'CMakeLists.txt'复制到当前目录中,并解决了问题。 – nullspace

回答

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) 

这样所有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 
) 

Here is a stupid and working example.