使用 CMake 生成 Visual Studio C++ 项目文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/395169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:07:02  来源:igfitidea点击:

Using CMake to generate Visual Studio C++ project files

c++visual-studiobuild-processcross-platformcmake

提问by amit

I am working on an open source C++ project, for code that compiles on Linux and Windows. I use CMake to build the code on Linux. For ease of development setup and political reasons, I must stick to Visual Studio project files/editor on Windows (I can't switch to Code::Blocks, for example). I see instructions to generate Visual Studio files using CMake, as here.

我正在开发一个开源 C++ 项目,用于在 Linux 和 Windows 上编译的代码。我使用 CMake 在 Linux 上构建代码。为了便于开发设置和原因,我必须坚持使用 Windows 上的 Visual Studio 项目文件/编辑器(例如,我无法切换到Code::Blocks)。我看到的方式来产生使用CMake的Visual Studio的文件,在这里

Have you used CMake to generate Visual Studio files before? How has been your experience? Suppose I want to add a new file to my project. What is the workflow for this?

你以前用过 CMake 生成过 Visual Studio 文件吗?您的体验如何?假设我想向我的项目添加一个新文件。这是什么工作流程?

采纳答案by Alex

CMake is actually pretty good for this. The key part was everyone on the Windows side has to remember to run CMake before loading in the solution, and everyone on our Mac side would have to remember to run it before make.

CMake实际上非常适合这一点。关键是 Windows 端的每个人都必须记住在加载解决方案之前运行 CMake,而我们 Mac 端的每个人都必须记住在 make 之前运行它。

The hardest part was as a Windows developer making sure your structural changes were in the cmakelist.txt file and not in the solution or project files as those changes would probably get lost and even if not lost would not get transferred over to the Mac side who also needed them, and the Mac guys would need to remember not to modify the make file for the same reasons.

最困难的部分是作为 Windows 开发人员确保您的结构更改在 cmakelist.txt 文件中,而不是在解决方案或项目文件中,因为这些更改可能会丢失,即使没有丢失也不会转移到 Mac 端谁也需要它们,Mac 人员需要记住不要出于同样的原因修改 make 文件。

It just requires a little thought and patience, but there will be mistakes at first. But if you are using continuous integrationon both sides then these will get shook out early, and people will eventually get in the habit.

只是需要一点思考和耐心,但一开始会有错误。但是如果你在双方都使用持续集成,那么这些会很早就被淘汰,人们最终会养成习惯。

回答by Ghita

Not sure if it's directly related to the question, but I was looking for an answer for how to generate *.sln from cmake projects I've discovered that one can use something like this:

不确定它是否与问题直接相关,但我一直在寻找如何从 cmake 项目生成 *.sln 的答案,我发现可以使用这样的东西:

cmake -G "Visual Studio 10"

The example generates needed VS 2010 files from an input CMakeLists.txt file

该示例从输入的 CMakeLists.txt 文件生成所需的 VS 2010 文件

回答by JesperE

We moved our department's build chain to CMake, and we had a few internal roadbumps since other departments where using our project files and where accustomed to just importing them into their solutions. We also had some complaints about CMake not being fully integrated into the Visual Studio project/solution manager, so files had to be added manually to CMakeLists.txt; this was a major break in the workflow people were used to.

我们将我们部门的构建链移至 CMake,由于其他部门使用我们的项目文件并且习惯于将它们导入到他们的解决方案中,因此我们遇到了一些内部障碍。我们还抱怨 CMake 没有完全集成到 Visual Studio 项目/解决方案管理器中,因此必须手动将文件添加到 CMakeLists.txt;这是人们习惯的工作流程的重大突破。

But in general, it was a quite smooth transition. We're very happy since we don't have to deal with project files anymore.

但总的来说,这是一个相当平稳的过渡。我们很高兴,因为我们不必再处理项目文件了。

The concrete workflow for adding a new file to a project is really simple:

将新文件添加到项目的具体工作流程非常简单:

  1. Create the file, make sure it is in the correct place.
  2. Add the file to CMakeLists.txt.
  3. Build.
  1. 创建文件,确保它位于正确的位置。
  2. 将文件添加到 CMakeLists.txt。
  3. 建造。

CMake 2.6 automatically reruns itself if any CMakeLists.txt files have changed (and (semi-)automatically reloads the solution/projects).

如果任何 CMakeLists.txt 文件发生更改,CMake 2.6 会自动重新运行(并且(半)自动重新加载解决方案/项目)。

Remember that if you're doing out-of-source builds, you need to be careful not to create the source file in the build directory (since Visual Studio only knows about the build directory).

请记住,如果您正在执行源外构建,则需要注意不要在构建目录中创建源文件(因为 Visual Studio 只知道构建目录)。

回答by jalf

As Alex says, it works very well. The only tricky part is to remember to make any changes in the cmake files, rather than from within Visual Studio. So on all platforms, the workflow is similar to if you'd used plain old makefiles.

正如亚历克斯所说,它运作良好。唯一棘手的部分是记住在 cmake 文件中进行任何更改,而不是在 Visual Studio 中进行更改。因此,在所有平台上,工作流程与您使用普通旧生成文件时的工作流程类似。

But it's fairly easy to work with, and I've had no issues with cmake generating invalid files or anything like that, so I wouldn't worry too much.

但是它很容易使用,而且我对 cmake 生成无效文件或类似的东西没有任何问题,所以我不会太担心。

回答by ap-osd

CMake produces Visual Studio Projects and Solutions seamlessly. You can even produce projects/solutions for different Visual Studio versions without making any changes to the CMake files.

CMake 无缝生成 Visual Studio 项目和解决方案。您甚至可以为不同的 Visual Studio 版本生成项目/解决方案,而无需对 CMake 文件进行任何更改。

Adding and removing source files is just a matter of modifying the CMakeLists.txtwhich has the list of source files and regenerating the projects/solutions. There is even a globbing function to find all the sources in a directory (though it should be used with caution).

添加和删​​除源文件只是修改CMakeLists.txt其中包含源文件列表并重新生成项目/解决方案的问题。甚至还有一个 globbing 函数来查找目录中的所有源(尽管应该谨慎使用)。

The following link explains CMake and Visual Studio specific behavior very well.

以下链接很好地解释了 CMake 和 Visual Studio 的特定行为。

CMake and Visual Studio

CMake 和 Visual Studio

回答by Red XIII

CMake can generate really nice Visual Studio .projs/.slns, but there is always the problem with the need to modify the .cmakefiles rather than .proj/.sln. As it is now, we are dealing with it as follows:

CMake 可以生成非常好的 Visual Studio .projs/ .slns,但总是存在需要修改.cmake文件而不是.proj/ 的问题.sln。就像现在一样,我们是这样处理的:

  1. All source files go to /srcand files visible in Visual Studio are just "links" to them defined in .filter.
  2. Programmer adds/deletes files remembering to work on the defined /srcdirectory, not the default project's one.
  3. When he's done, he run a script that "refreshes" the respective .cmakefiles.
  4. He checks if the code can be built in the recreated environment.
  5. He commits the code.
  1. 所有源文件/src和 Visual Studio 中可见的文件只是.filter.
  2. 程序员添加/删除记住在定义的/src目录上工作的文件,而不是默认项目的目录。
  3. 完成后,他运行一个脚本来“刷新”相应的.cmake文件。
  4. 他检查代码是否可以在重新创建的环境中构建。
  5. 他提交了代码。

At first we were a little afraid of how it will turn out, but the workflow works really well and with nice diff visible before each commit, everyone can easily see if his changes were correctly mapped in .cmakefiles.

起初我们有点害怕结果如何,但工作流程运行得非常好,并且在每次提交之前都可以看到很好的差异,每个人都可以轻松查看他的更改是否正确映射到.cmake文件中。

One more important thing to know about is the lack of support (afaik) for "Solution Configurations" in CMake. As it stands, you have to generate twodirectories with projects/solutions - one for each build type (debug, release, etc.). There is no direct support for more sophisticated features - in other words: switching between configurations won't give you what you might expect.

需要了解的更重要的一件事是CMake 中缺乏对“解决方案配置”的支持 ( afaik)。就目前而言,您必须为项目/解决方案生成两个目录 - 每个构建类型(调试、发布等)一个。没有对更复杂功能的直接支持——换句话说:在配置之间切换不会给你你可能期望的结果。

回答by S Meaden

Lots of great answers here but they might be superseded by this CMake support in Visual Studio (Oct 5 2016)

这里有很多很棒的答案,但它们可能会被Visual Studio 中的 CMake 支持所取代(2016 年 10 月 5 日)

回答by TarmoPikaro

I've started my own project, called syncProj. Documentation / download links from here:

我已经开始了我自己的项目,称为syncProj。文档/下载链接从这里:

https://docs.google.com/document/d/1C1YrbFUVpTBXajbtrC62aXru2om6dy5rClyknBj5zHU/edit#https://sourceforge.net/projects/syncproj/

https://docs.google.com/document/d/1C1YrbFUVpTBXajbtrC62aXru2om6dy5rClyknBj5zHU/edit# https://sourceforge.net/projects/syncproj/

If you're planning to use Visual studio for development, and currently only C++ is supported.

如果您打算使用 Visual Studio 进行开发,并且目前仅支持 C++。

Main advantage compared to other make systems is that you can actually debug your script, as it's C# based.

与其他 make 系统相比,主要优势在于您可以实际调试脚本,因为它是基于 C# 的。

If you're not familiar with syncProj, you can just convert your solution / project to .cs script, and continue further development from that point on.

如果您不熟悉 syncProj,您可以将您的解决方案/项目转换为 .cs 脚本,并从那时起继续进一步开发。

In cmake you will need to write everything from scratch.

在 cmake 中,您需要从头开始编写所有内容。