2015-10-02 149 views
4

我想用Visual Studio 2010/VC10和CMake創建一個庫。CMake包含和源路徑與Windows目錄路徑不一樣

Windows的樹不同於CMake項目樹。問題是CMake不會在Visual Studio中創建帶有頭文件和源文件的foolib。

我無法更改庫的樹,因爲它是一個擁有大量共享多個包含文件的庫的舊代碼。

root 
|-'includes 
| '-foo.h 
|-'src 
| '-libprojects 
| | '-foolib 
| | | '-bin 
| | | '-project 
| | | | '-mak100 
| | | | '-CMakeLists01.txt 
| | | '-src 
| | | | '-CMakeLists02.txt 
| | | | '-foo.cxx 

的唯一的CMakeLists.txt有很多解釋。

CMakeLists01.txt

cmake_minimum_required (VERSION 2.8) 
cmake_policy (SET CMP0015 NEW) 
project (foolib) 

set (CMAKE_BUILD_TYPE Debug) 

include_directories ("${PROJECT_SOURCE_DIR}/../../../../include") 

# This dosen't works and CMake can't find the CMakeLists02.txt ??? 
add_subdirectory("${PROJECT_SOURCE_DIR}/../src") 

CMakeLists02.txt

# CMakeLists02.txt 
set (QueryHeader 
    "./../../../../include/foo.h") 

set (QuerySources 
    "foo.cxx") 

問:我怎樣才能包括CMakeLists02.txt到CMakeLists01.txt與add_subdirectory()

這是一個批處理文件,如果有人測試它

#doCMake.cmd 
@echo off 
call "c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tool\vsvars32.bat" 
mkdir mak100 
cd mak100 
cmake -G "Visual Studio 10" .. 
cd .. 
pause 
+0

我不明白你的問題,如果你發佈了一個問題,我不明白你的問題。你願意多解釋一下你期望的和你實際得到的東西嗎? – usr1234567

+1

[CMAKE添加子目錄不是真實目錄上的子目錄]的可能重複(http://stackoverflow.com/questions/7980784/cmake-add-sub-directory-which-is-not-sub-directory -on-real-directory) – LPs

+0

@ago。你的權利! – post4dirk

回答

2

我只是給你一個例子嘗試和解決方案在錯誤信息

CMake Error at CMakeLists.txt:10 (add_subdirectory): 
    add_subdirectory not given a binary directory but the given source 
    directory ".../src/libprojects/foolib/src" 
    is not a subdirectory of 
    ".../src/libprojects/foolib/project". When 
    specifying an out-of-tree source a binary directory must be explicitly 
    specified. 

因此,作爲@LPs指出,看到CMAKE add sub-directory which is not sub-directory on real directory給出。只要改變你的add_subdirectory()調用是這樣的:

add_subdirectory("../src" "src") 

而且你不會有前綴${PROJECT_SOURCE_DIR}的第一個參數,並與${CMAKE_CURRENT_BINARY_DIR}第二(均爲默認設置,見add_subdirectory())。

我的建議,你的原因是將主/庫CMakeLists01.txt放入foolib文件夾。那你甚至不需要CMakeLists02.txt

的src/libprojects/foolib /的CMakeLists.txt

cmake_minimum_required (VERSION 2.8) 

project (foolib CXX) 

include_directories("../../../include") 

add_library(foo "src/foo.cxx") 

特別是在源和頭文件是在分開的(子)的文件夾的情況下,執行類似add_library(foo src/foo.cxx)是完全OK /經常使用。

+0

非常感謝。您的解決方案只使用一個CMakeLists.txt是很好的。在我的情況下,我必須隱藏項目文件夾中的CMakeLists.txt文件,但這不會有問題。在20個或更多C文件的情況下,我將採取2 CMakeLists.txt解決方案,因爲清晰。 – post4dirk

+0

@ user3355421不客氣。關於源文件的清晰度,您可能也感興趣[在CMake中保持跨子目錄的文件層次結構](http://stackoverflow.com/questions/31538466/keeping-file-hierarchy-across-subdirectories-in-cmake) – Florian