问题
System :
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 Version :
anon@anon-S:~/$ cmake --version
cmake version 3.2.2
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)
But
anon@anon-S:/home/anon/project/build$ cmake ..
-- Configuring done
-- Generating done
-- Build files have been written to: /home/anon/project/
Build files have been written to: /home/anon/project/
instead /home/anon/project/build/
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/
回答1:
To clarify what happened here:
When running cmake <directory>
it does one of two things:
- 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 ..
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.
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>
.
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 <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.
来源:https://stackoverflow.com/questions/36568551/cmake-wrong-build-target-directory