更改 CMake 文件标准位置 [英] Changing CMake files standard location
问题描述
我有一个源目录,其中包含一个名为phantom-dir/"的文件夹,我将所有不需要的生成文件放在其中.我想将所有由 CMake 生成的文件放在这个幻影目录中(连同其他生成的丑陋"文件).
I have a source directory with a folder called "phantom-dir/" where I put all generated files I don't need. I want to put all generated files by CMake inside this phantom directory (together with other generated and "ugly" files).
一个小例子:
$ mkdir cmake-test
$ cd cmake-test
$ echo 'message("Hello World!")' > CMakeLists.txt
$ cmake . | grep "Hello"
Hello World!
$ tree
.
├── CMakeCache.txt
├── CMakeFiles
│ ├── CMakeCCompiler.cmake
│ ├── cmake.check_cache
│ ├── CMakeCXXCompiler.cmake
│ ├── CMakeDetermineCompilerABI_C.bin
│ ├── CMakeDetermineCompilerABI_CXX.bin
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeOutput.log
│ ├── CMakeSystem.cmake
│ ├── CMakeTmp
│ ├── CompilerIdC
│ │ ├── a.out
│ │ └── CMakeCCompilerId.c
│ ├── CompilerIdCXX
│ │ ├── a.out
│ │ └── CMakeCXXCompilerId.cpp
│ ├── Makefile2
│ ├── Makefile.cmake
│ ├── progress.marks
│ └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
└── Makefile
4 directories, 20 files
默认情况下,所有 CMake 文件(CMakeCache.txt、cmake_install.cmake、Makefile、CMakeFiles)都写入工作目录中.但是,我想要这样的东西:
By default, all CMake files (CMakeCache.txt, cmake_install.cmake, Makefile, CMakeFiles) are written in the working directory. But, I want something like that:
$ mkdir cmake-test
$ cd cmake-test
$ mkdir phantom-dir
$ echo 'message("Hello World!")' > CMakeLists.txt
$ // editing CMakeLists.txt to set some cmake variables.
$ cmake . | grep "Hello"
Hello World!
$ tree
.
├── phantom-dir
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── CMakeCCompiler.cmake
│ │ ├── cmake.check_cache
│ │ ├── CMakeCXXCompiler.cmake
│ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeSystem.cmake
│ │ ├── CMakeTmp
│ │ ├── CompilerIdC
│ │ │ ├── a.out
│ │ │ └── CMakeCCompilerId.c
│ │ ├── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ └── CMakeCXXCompilerId.cpp
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
├── CMakeLists.txt
└── Makefile
4 directories, 20 files
即:当前目录下的Makefile(要make,cmake .&&make"),而其余生成的文件在phantom"目录下.
That means: the Makefile in the current directory (to make, "cmake . && make"), but the remaining generated files inside the "phantom" directory.
我知道我可以做到:
$ cd phantom-dir/
$ cmake ../
但是每次我想重新编译或重新制作 cmake 时都这样做对我来说有点烦人,尤其是考虑到我要多次修改我的 CMakeLists.txt.
But it's a little tiresome for me to do it each time I want to re-compiling or remake cmake, above all taking into account that I'm modifying many times my CMakeLists.txt.
我必须在 CMakeLists.txt 文件中设置哪些变量才能实现它?
Which variables I have to set in the CMakeLists.txt file in order to achieve it?
推荐答案
你可以使用未公开的 CMake 选项 -H
和 -B
来避免离开你的源代码目录-H
指定主 CMakeLists.txt 文件的路径,-B
指定所需构建目录的路径.
You could make use of the undocumented CMake options -H
and -B
to avoid leaving your source dir. -H
specifies the path to the main CMakeLists.txt file, and -B
specifies the path to your desired build directory.
cmake -H. -Bphantom-dir
请注意,这些没有记录在案,所以我想 Kitware 人可以随时更改.
Note that these are undocumented and so I suppose could change at any time the Kitware guys feel like.
要在不离开源目录的情况下构建项目,您可以使用(官方)选项 --build
这是一种跨平台的方式来调用你选择的构建工具,你可以传递任何你想要的标志工具.例如
To build your project without leaving your source dir, you can make use of the (official) option --build
This is a cross-platform way to invoke your chosen build tool, and you can pass whatever flags you want to this tool. e.g.
cmake --build phantom-dir -- -j3
这篇关于更改 CMake 文件标准位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!