【问题标题】:CMake: wrong build target directoryCMake:错误的构建目标目录
【发布时间】:2016-08-02 18:53:38
【问题描述】:

系统:

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/

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

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/

【问题讨论】:

  • srctest 子目录下的文件CMakeLists.txt 的内容是什么?
  • 您是否曾经(不小心)在 /home/anon/project 中运行 CMake(导致源内构建)?

标签: ubuntu build cmake


【解决方案1】:

澄清这里发生了什么:

当运行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 ..

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

这确实是一个非常讨厌的问题,但幸运的是,一旦你意识到发生了什么,它可以很快得到解决:只需从 &lt;src_dir&gt; 中删除 CMakeCache.txt

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

-S &lt;path-to-source&gt;

CMake 项目根目录的路径 构建。

-B &lt;path-to-build&gt;

CMake 将用作的目录路径 构建目录的根目录。

如果该目录尚不存在,CMake 将创建它。

【讨论】:

    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多