ubuntu - CMake:错误的构建目标目录

标签 ubuntu build cmake

系统:

Linux anon-S 3.19.0-31-generic #36~14.04.1-Ubuntu SMP Thu Oct 8 10:21:08 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

CMake 版本:

anon@anon-S:~/$ cmake --version
cmake version 3.2.2

主CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)

set(CMAKE_CXX_STANDARD 11)

add_subdirectory(${PROJECT_SOURCE_DIR}/src)
add_subdirectory(${PROJECT_SOURCE_DIR}/test)

但是

anon@anon-S:/home/anon/project/build$ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/anon/project/

构建文件已写入:/home/anon/project/ 而不是 /home/anon/project/build/

我在使用 cmake 3.4.1 的 Debian jessie 上没有这个问题:

anon@anon-S:/home/anon/project/build$ cmake ..
-- The C compiler identification is GNU 5.2.1
-- The CXX compiler identification is GNU 5.2.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Doxygen: /usr/bin/doxygen (found version "1.8.11") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/anon/project/build/

最佳答案

澄清这里发生的事情:

运行时 cmake <directory>它执行以下两件事之一:

  • 如果<directory>是一个源目录(即其中有 CMakeLists.txt),它将在该源上运行 CMake 并使用当前工作目录作为构建目录。
  • 如果<directory>是一个构建目录(即其中有 CMakeCache.txt),它将在用于创建构建目录的任何源目录上运行 CMake。

请注意,后一种情况优先于第一种情况。也就是说,如果您执行以下操作:

cd <src_dir>
cmake .
 *oops, wrong directory, i didn't mean that*
cd build
cmake ..

您可能期望这将运行 CMake <src_dir>作为来源和 build作为构建目录。但由于第一次意外的 CMake 运行,<src_dir>现在也是源内构建的构建目录!因此,第二次运行将忽略 build 中的所有内容。而是使用 src_dir作为源目录和构建目录。

这确实是一个非常令人讨厌的问题,但幸运的是,一旦您意识到发生了什么,它就可以很快得到解决:只需删除 CMakeCache.txt来自<src_dir> .

请注意,在较新的 CMake 版本(3.13 及更高版本)中,您可以使用 the -S and -B command line options消除目录参数含义的歧义:

-S <path-to-source>

Path to root directory of the CMake project to build.

-B <path-to-build>

Path to directory which CMake will use as the root of build directory.

If the directory doesn’t already exist CMake will make it.

关于ubuntu - CMake:错误的构建目标目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568551/

相关文章:

build - 选择脚本/构建工具

c - nmake v6 递归构建 c 文件

cmake - 如何更改程序的工作目录

mongodb - 在 docker 容器中安装 mongodb

python - 长时间运行的批处理在 Ubuntu 中过早终止

c++ - Qt - 建立一个项目

linux - 在 openSUSE 下编译 QtProject 时出错

visual-studio-2010 - Qt5、CMake、AUTOMOC 和预编译头文件

eclipse - 在ubuntu中没有环境变量的maven工作

python - 使用 PyGTK 的滑动布局