英文:
How to exclude CMake generated project files from Git
问题
我有一个CMake项目,我想要从版本控制中排除生成的项目文件。
根据我的理解,没有办法强制CMake使用特定的构建目录。用户可以指定任何他们想要的目录,因此我无法在我的.gitignore中排除它。通常,用户会将它放在源目录内的一个名为“build”的目录中,我会将它放在.gitignore中。但我有个疯狂的想法,要将它命名为“meta_build”,这就导致了这个问题。而且,一个经验不足的用户可能会在源代码目录内进行内部构建。只是时间问题,直到有人意外地将生成的项目文件检入。
对于这个问题,是否有任何想法或解决方法将不胜感激。
英文:
I have a CMake project and I would like to exclude the generated project files from version control.
From my understanding there is no way to force CMake to use a certain build directory. The user can specify any directory they want and therefore I cant exclude it in my .gitignore. Usually the user would place it in a directory called build
inside the source directory and I would put that in .gitignore. But I had the crazy idea to call it meta_build
, which led me to this problem. Also, an inexperienced user might do an in-source build. It's just a matter of time until someone accidentally checks in the generated project files.
Any ideas or workarounds for this matter are much appreciated.
答案1
得分: 1
以下是翻译好的内容:
有几种处理这个问题的选项:
-
教育您的用户。清晰地记录生成的构建文件不应该提交,并推荐使用源外构建。这不是技术解决方案,但可能会有效。
-
使用预提交钩子来检查生成的文件。您可以编写一个脚本,在预提交时运行,检查常见的构建文件名称并警告用户。他们仍然需要手动删除这些文件,但这会提醒他们存在问题。
-
使用CMake的EXCLUDE_FROM_ALL目标属性。您可以将生成构建文件的目标标记为EXCLUDE_FROM_ALL,意味着默认情况下不会构建它。用户必须显式构建该目标以生成构建文件。这降低了意外提交的可能性。
-
在子目录中生成构建文件。将顶级CMakeLists.txt中生成构建文件放在一个build_files子目录中。然后,您可以将build_files/添加到.gitignore。用户必须从build_files运行CMake进行源内构建,从而清楚地表明该目录应该被忽略。
-
考虑根本不生成项目文件。如果您构建和分发二进制库,可以仅提供一个CMakeLists.txt供用户链接该库。不会生成可能被意外提交的项目文件。当然,如果您需要项目文件来实际构建项目,则这不适用!
英文:
There are a few options to handle this:
Educate your users. Document clearly that generated build files should not be committed, and that an out-of-source build is recommended. This is not a technical solution but can be effective.
Use a pre-commit hook to check for generated files. You can write a script to run on pre-commit to check for common build file names and warn the user. They would still have to manually remove the files, but it alerts them to the issue.
Use CMake's EXCLUDE_FROM_ALL target property. You can mark the target that generates the build files as EXCLUDE_FROM_ALL, meaning it will not be built by default. Users would have to explicitly build that target to generate the build files. This makes it less likely they will be accidentally committed.
Generate the build files in a subdirectory. Have your top-level CMakeLists.txt generate the build files in a build_files subdirectory. Then you can add build_files/ to .gitignore. Users would have to run CMake from build_files for an in-source build, making it clear that directory should be ignored.
Consider not generating project files at all. If you build and distribute a binary library, you could provide just a CMakeLists.txt for people to link against the library. No project files are generated that could be accidentally committed. Of course, this doesn't work if you need project files to actually build your project!
答案2
得分: 0
我认为这对你可能是一个问题,因为那些其他用户正在共同参与这个项目,而且可能还会推送到共享的远程仓库。在这种情况下,你有以下选择:
-
设定一个规范,只构建到一个具有共同名称的子目录中。例如,PitchFork 布局的
build/
。我个人将所有的构建放在out/build/<build_name>
目录中。然后你可以在.gitignore
中忽略这个目录。 -
允许与你协作的用户在他们的本地克隆中将构建放在任何位置,为了避免
.gitignore
的任意扩展,告诉他们使用$XDG_CONFIG_HOME/git/ignore
,或者本地克隆的$GIT_DIR/info/exclude
(文档)。 -
你可以提示用户使用预设值来命名特定的构建目录名称:https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html(查看配置预设的
binaryDir
属性)。 -
从技术上讲,你可以独立于构建目录的位置按文件名编写
.gitignore
,但这听起来像是一件非常麻烦的事情,特别是考虑到不同的构建系统生成器会生成不同的构建产物和特定的构建系统配置文件,而不同的平台会为目标输出文件名使用不同的前缀和后缀(例如CMAKE_EXECUTABLE_SUFFIX
)。
英文:
I assume this is an issue for you because those other users are collaborating on the project and also potentially pushing to a shared remote. In that case, you have options:
-
Set a convention for only building to a subdirectory with a common name. For example, The PitchFork Layout's
build/
. I personally put all my builds in aout/build/<build_name>
directory. Then you can just gitignore that one directory. -
Allow your collaborating users to put build anywhere they want in their local clone, and in order to avoid arbitrary explosion of the gitignore, tell them to use their
$XDG_CONFIG_HOME/git/ignore
, or the local clone's$GIT_DIR/info/exclude
(docs). -
You can hint users to use a particular build directory name(s) using presets: https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html (see the
binaryDir
property for configuration presets). -
Technically you can write the gitignore on a filename basis independent of where the build directory is (Ex. listing out individually
*.exe
, etc.), but that sounds like a real pain in the butt- especially given that different buildsystem generators will have different build artifacts and specific buildsystem configuration files, and that different platforms have different prefixes and suffixes for target output file names (see for exampleCMAKE_EXECUTABLE_SUFFIX
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论