我有一个源目录,其中包含一个名为“phantom-dir/”的文件夹,其中放置了所有不需要的生成文件。我想将 CMake 生成的所有文件放在这个幻像目录中(连同其他生成的和“丑陋”的文件)。
一个小例子:
$ 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)都写入工作目录。但是,我想要这样的东西:
$ 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 (to make, "cmake . && make"),但剩余的生成文件在 "phantom"目录中。
我知道我可以做到:
$ cd phantom-dir/
$ cmake ../
但是每次我想重新编译或重新制作 cmake 时都这样做有点累,最重要的是考虑到我要多次修改我的 CMakeLists.txt。
为了实现它,我必须在 CMakeLists.txt 文件中设置哪些变量?
最佳答案
您可以使用未记录的 CMake 选项 -H
和 -B
来避免离开您的源目录。 -H
指定主 CMakeLists.txt 文件的路径,-B
指定所需构建目录的路径。
cmake -H. -Bphantom-dir
请注意,这些都是无证的,所以我想 Kitware 人可以随时更改。
要在不离开源目录的情况下构建项目,您可以使用(官方)选项 --build
这是一种调用您选择的构建工具的跨平台方式,您可以将任何您想要的标志传递给该工具。例如
cmake --build phantom-dir -- -j3
https://stackoverflow.com/questions/13712020/