CMake包含和源路径与Windows目录路径

时间:2015-10-02 08:26:19

标签: c++ c cmake

我想用Visual Studio 2010 / VC10和CMake创建一个库。

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")

问题:如何使用add_subdirectory()

将CMakeLists02.txt包含到CMakeLists01.txt中

如果有人测试它,这是一个批处理文件

#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

1 个答案:

答案 0 :(得分: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())。

我在您的业务中的建议是将main / libraries 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)的操作完全正常/经常使用。