CMake 输出/构建目录

CMake output/build directory(CMake 输出/构建目录)

本文介绍了CMake 输出/构建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 CMake 的新手,阅读了一些关于如何使用它的教程,并编写了一些复杂的 50 行 CMake 脚本,以便为 3 个不同的编译器制作程序.这可能总结了我在 CMake 中的所有知识.

现在我的问题是我有一些源代码,我在制作程序时不想触摸/弄乱它的文件夹.我希望所有 CMake 和 make 输出文件和文件夹都进入 ../Compile/,所以我为此更改了 CMake 脚本中的一些变量,并且它起作用了有一段时间我在笔记本电脑上做了这样的事情:

编译$ cmake ../src编译$ make

我现在所在的文件夹中有一个干净的输出,这正是我正在寻找的.

现在我转移到另一台计算机,并重新编译了 CMake 2.8.11.2,我几乎回到了原点!它总是将东西编译到我的 CMakeLists.txt 所在的 src 文件夹中.

我在我的 CMake 脚本中选择目录的部分是这样的:

set(dir ${CMAKE_CURRENT_SOURCE_DIR}/../Compile/)设置(EXECUTABLE_OUTPUT_PATH ${dir} CACHE PATH构建目录"FORCE)设置(LIBRARY_OUTPUT_PATH ${dir} CACHE PATH构建目录"FORCE)设置(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dir})设置(CMAKE_BUILD_FILES_DIRECTORY ${dir})设置(CMAKE_BUILD_DIRECTORY ${dir})设置(CMAKE_BINARY_DIR ${dir})SET(EXECUTABLE_OUTPUT_PATH ${dir})SET(LIBRARY_OUTPUT_PATH ${dir}lib)设置(CMAKE_CACHEFILE_DIR ${dir})

现在它总是以:

-- 构建文件已写入:/.../src

我错过了什么吗?

解决方案

几乎不需要设置您正在设置的所有变量.CMake 将它们设置为合理的默认值.你绝对应该不要修改CMAKE_BINARY_DIRCMAKE_CACHEFILE_DIR.将这些视为只读.

首先从src目录中删除现有的有问题的缓存文件:

cd srcrm CMakeCache.txt光盘..

然后删除所有 set() 命令并执行:

cd 编译 &&rm -rf *cmake ../src

只要您在运行 CMake 时在源目录之外,它就不会修改源目录,除非您的 CMakeList 明确告诉它这样做.

一旦你有了这个,你可以看看CMake默认把东西放在哪里,只有当你对默认位置(例如EXECUTABLE_OUTPUT_PATH的默认值)不满意时,修改只有你需要的那些.并尝试相对于 CMAKE_BINARY_DIRCMAKE_CURRENT_BINARY_DIRPROJECT_BINARY_DIR 等来表达它们.

如果您查看 CMake 文档,您会看到变量被划分为语义部分.除非常特殊情况外,您应该对待提供信息的变量"下列出的所有内容.在 CMakeLists 中为只读.

I'm pretty new to CMake, and read a few tutorials on how to use it, and wrote some complicated 50 lines of CMake script in order to make a program for 3 different compilers. This probably concludes all my knowledge in CMake.

Now my problem is that I have some source code, whose folder I don't want to touch/mess with when I make the program. I want that all CMake and make output files and folders to go into ../Compile/, so I changed a few variables in my CMake script for that, and it worked for sometime when I did something like this on my laptop:

Compile$ cmake ../src
Compile$ make

Where with that I had a clean output in the folder I'm in right now, which is exactly what I'm looking for.

Now I moved to another computer, and recompiled CMake 2.8.11.2, and I'm almost back to square one! It always compiles the thing into the src folder where my CMakeLists.txt is located.

The part where I choose the directory in my CMake script is this:

set(dir ${CMAKE_CURRENT_SOURCE_DIR}/../Compile/)
set(EXECUTABLE_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)
set(LIBRARY_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dir})
set(CMAKE_BUILD_FILES_DIRECTORY ${dir})
set(CMAKE_BUILD_DIRECTORY ${dir})
set(CMAKE_BINARY_DIR  ${dir})
SET(EXECUTABLE_OUTPUT_PATH ${dir})
SET(LIBRARY_OUTPUT_PATH ${dir}lib)
SET(CMAKE_CACHEFILE_DIR ${dir})

And now it always ends with:

-- Build files have been written to: /.../src

Am I missing something?

解决方案

There's little need to set all the variables you're setting. CMake sets them to reasonable defaults. You should definitely not modify CMAKE_BINARY_DIR or CMAKE_CACHEFILE_DIR. Treat these as read-only.

First remove the existing problematic cache file from the src directory:

cd src
rm CMakeCache.txt
cd ..

Then remove all the set() commands and do:

cd Compile && rm -rf *
cmake ../src

As long as you're outside of the source directory when running CMake, it will not modify the source directory unless your CMakeList explicitly tells it to do so.

Once you have this working, you can look at where CMake puts things by default, and only if you're not satisfied with the default locations (such as the default value of EXECUTABLE_OUTPUT_PATH), modify only those you need. And try to express them relative to CMAKE_BINARY_DIR, CMAKE_CURRENT_BINARY_DIR, PROJECT_BINARY_DIR etc.

If you look at CMake documentation, you'll see variables partitioned into semantic sections. Except for very special circumstances, you should treat all those listed under "Variables that Provide Information" as read-only inside CMakeLists.

这篇关于CMake 输出/构建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:CMake 输出/构建目录