2012-05-17 50 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.