CMake / Ninja试图编译删除的.cpp文件 [英] CMake/Ninja attempting to compile deleted `.cpp` file
问题描述
我发现当我使用 cmake
和从我的项目中删除
,我不能轻易地编译它,而没有首先完全删除我的构建目录,从头开始。 CMake和/或Ninja显然松动了一些对它编译的所有 cpp
> ninja cpp
文件的引用,甚至在重新运行CMake之前删除CMake缓存不会删除所有参考。
这是一个已知问题吗?有解决方案吗?我偶尔只是运行 rm $(grep -R< filename>< builddir>)
,但这是一个可怕的kludge。
编辑:看来我错了,因为我无法复制此问题。手动重新运行CMake似乎总是生成 .cpp
文件的正确列表,即使使用 GLOB $ c
>
使用文件(GLOB ...)收集源文件
是,使用文件(GLOB ...)
命令收集源文件时,CMake不会知道新的或删除的源文件。这是一个已知的限制与CMake。我改变了我的CMake项目,以单独列出所有源文件,正是因为这个。出于方便,我仍然用文件(GLOB ...)
命令收集我的头文件。
引用CMake的 文件()
命令文档:
我们不建议使用GLOB从$ b $中收集源文件列表b你的源码树。如果没有CMakeLists.txt文件更改,当源是
添加或删除,那么生成的构建系统无法知道什么时候
要求CMake重新生成。
删除 CMakeCache.txt
到retrigger配置
只要删除 CMakeCache.txt
可能不足以重新触发CMake配置。 问题0014820:警告用户只需删除您需要的CMakeCache.txt 版权声明删除所有 CMakeFiles
目录。
根据我的经验,重新触发CMake配置的最可靠方法是触摸其中一个项目 CMakeLists.txt
文件。
注意: ninja
CMake添加了 rebuild_cache
目标
从源代码管理更新后重新触发
只是一个想法:if删除源文件发生,因为它们从源控件中删除可能有一个解决方法,仍然允许您使用文件(GLOB ...)
您的源文件。 / p>
例如如果你使用GIT,你可以添加以下到你的主 CMakeLists.txt
:
configure_file($ {CMAKE_SOURCE_DIR} /。git / index $ {PROJECT_BINARY_DIR} /git_index.tmp)
缺点:每个GIT操作(更新,提交,...)的重新触发配置。
/ strong>:
I've found that when I delete cpp
files from my project using cmake
and ninja
, I can't easily compile it without first completely deleting my build directory and starting from scratch. CMake and/or Ninja apparently squirrels away a number of references to all the cpp
files that it compiles, even deleting the CMake cache before re-running CMake doesn't remove all the references.
Is this a known issue? Is there a solution? I've occasionally just run rm $(grep -R <filename> <builddir>)
, but that's a terrible kludge.
EDIT: It appears I was mistaken, as I have not been able to duplicate this problem. Manually re-running CMake appears to always generate the correct list of .cpp
files, even using GLOB
to generate lists of sources.
Turning my comments into an answer
Collecting your source files with file(GLOB ...)
Yes, CMake won't know about new or deleted source files when collecting your source files with the file(GLOB ...)
command. This is a known restriction with CMake. I've changed my CMake project(s) to list all source files individually exactly because of this. Out of convenience I'm still collecting my header files with the file(GLOB ...)
command.
Quoting from CMake's file()
command documentation:
We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.
Deleting CMakeCache.txt
to retrigger Configuration
Just deleting CMakeCache.txt
may not be enough to retrigger the CMake configuration. Issue 0014820: warn users about removing only CMakeCache.txt claims you need also to delete all CMakeFiles
directories.
From my experience the most reliable way to retrigger the CMake configuration is to touch one of the projects CMakeLists.txt
files.
Note: For ninja
CMake adds a rebuild_cache
target for conveniently running CMake for your project again.
Retrigger after Updates from Source Control
Just one thought: if the deletion of source files happens because they were removed from your source control there maybe a workaround that still allows you to use file(GLOB ...)
on your source files.
E.g. if you use GIT you could add the following to your main CMakeLists.txt
:
configure_file(${CMAKE_SOURCE_DIR}/.git/index ${PROJECT_BINARY_DIR}/git_index.tmp)
Disadvantage: It would retrigger configuration which each GIT operation (update, commit, ...).
Some References:
这篇关于CMake / Ninja试图编译删除的.cpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!