在 CMake 中设置中间文件(如 .obj)的目录

Set the directory for Intermediate files (like .obj) in CMake

我看到 CMake 将中间文件(如 .obj)放在这样的目录中:

project.dir/sort/of/copy/of/source/directory

有没有办法拥有类似的东西:

project.dir/Debug/ myfiles.obj    |--> for my debug

project.dir/Release/ myfiles.obj    |--> for my release

目前,我使用 2 个单独的目录来生成每次用于调试和发布的库或可执行文件。在我也有了平台之后...

是否有类似于CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE或CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE的东西...

中级 files.obj ?

我也尝试过 /Fo 但是当我使用这个 FLAG 时,Cmake 会用他的配置覆盖 :

warning D9025 : overriding '/Fo;../x64/Debug/' with '/FoCMakeFiles\project.dir\src\project\main.cpp.obj'

请问有人有解决办法吗?

您不能 - 至少目前,请参阅 0014999: Changing Intermediate Directory of Visual Studio 2012 feature request - change the intermediate directories in CMake and for makefile generators - like in your case NMake - you can have only one build configuration type 每个二进制构建输出目录。

正如@usr1234567 评论的那样,使用两个构建目录是正确的做法。

或者 - 如果这是一个选项 - 使用 Visual Studio 多配置生成器。它确实使用了您建议的中间目录:

project.dir/Debug/...
project.dir/Release/...

NMake 与 Visual Studio 命令行解决方案

在我通常用来构建基于 CMake 的系统的包装器脚本中也可以看出差异。

所以 NMake 看起来像这样:

@ECHO off
"\Program Files (x86)\Microsoft Visual Studio 14.0\vc\vcvarsall.bat" x64
IF NOT EXIST "x64\Debug\Makefile" (
    cmake -H"." -B"x64/Debug" -DCMAKE_BUILD_TYPE=Debug -G"NMake Makefiles"
)
cmake --build "x64/Debug" --target "project"
IF NOT EXIST "x64\Release\Makefile" (
    cmake -H"." -B"x64/Release" -DCMAKE_BUILD_TYPE=Release -G"NMake Makefiles"
)
cmake --build "x64/Release" --target "project"

我喜欢的 Visual Studio 解决方案变体是这样的:

@ECHO off
IF NOT EXIST "x64\*.sln" (
    cmake -H"." -B"x64" -G"Visual Studio 14 2015 Win64"
)
cmake --build "x64" --target "project" --config "Debug"
cmake --build "x64" --target "project" --config "Release"

其他参考资料