2015-10-02 151 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