CMake:错误的构建目标目录 [英] CMake: wrong build target directory
问题描述
系统:
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:
Main 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/
Build files have been written to: /home/anon/project/
instead /home/anon/project/build/
我在使用cmake 3.4.1的Debian jessie上没有这个问题:
I haven't this problem on my Debian jessie with 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/
推荐答案
要澄清此处发生的情况:
To clarify what happened here:
运行 cmake< directory>
时,它会执行以下两项操作之一:
When running cmake <directory>
it does one of two things:
- 如果
< directory>
是源目录(即其中包含CMakeLists.txt
),它将在该源上运行CMake并使用当前的工作方式目录作为构建目录. - 如果
< directory>
是构建目录(即其中包含CMakeCache.txt
),它将在用于创建的任何源目录上运行CMake.构建目录.
- If
<directory>
is a source directory (ie. has aCMakeLists.txt
in it), it will run CMake on that source and use the current working directory as the build directory. - If
<directory>
is a build directory (ie. has aCMakeCache.txt
in it), it will run CMake on whatever source directory was used to create the build directory.
请注意,后一种情况优先于第一种情况.也就是说,如果您执行以下操作:
Note that the latter case takes precedence over the first one. That is, if you do the following:
cd <src_dir>
cmake .
*oops, wrong directory, i didn't mean that*
cd build
cmake ..
您可能希望这将以< src_dir>
作为源,而 build
作为构建目录运行CMake.但是由于第一次偶然的CMake运行,< src_dir>
现在也是源代码构建的构建目录!因此,第二次运行将忽略 build
中的所有内容,而是将 src_dir
用作源目录和构建目录.
You might expect that this will run CMake with <src_dir>
as the source and build
as the build directory. But because of the first accidental CMake run, <src_dir>
is now also a build directory for an in-source build! Hence the second run will ignore everything that is in build
and instead use src_dir
as both the source and build directory.
这确实是一个非常令人讨厌的问题,但是幸运的是,一旦您意识到发生了什么,就可以很快解决:只需从< src_dir>
中删除 CMakeCache.txt
This is indeed a very nasty issue, but fortunately it can be solved very quickly once you realize what's happening: Simply remove the CMakeCache.txt
from <src_dir>
.
请注意,在较新的CMake版本(3.13及更高版本)中,可以使用 -S
和 -B
命令行选项,用于消除目录参数的含义:
Note that in newer CMake versions (3.13 and up), you can use the -S
and -B
command line options to disambiguate the meaning of the directory argument:
-S<到源的路径>
到CMake项目的根目录的路径构建.
Path to root directory of the CMake project to build.
-B<构建路径>
CMake将用作目录的路径构建目录的根.
Path to directory which CMake will use as the root of build directory.
如果该目录尚不存在,CMake将创建该目录.
If the directory doesn’t already exist CMake will make it.
这篇关于CMake:错误的构建目标目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!