Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

build - Changing CMake files standard location

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).

A mini example:

$ 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

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

That means: the Makefile in the current directory (to make, "cmake . && make"), but the remaining generated files inside the "phantom" directory.

I know I can make it with:

$ cd phantom-dir/
$ cmake ../

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.

Which variables I have to set in the CMakeLists.txt file in order to achieve it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Note that these are undocumented and so I suppose could change at any time the Kitware guys feel like.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...