109

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.

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?

1
  • 2
    For those in GNU+Linux trying to generate project files for Visual Studio, which is Windows-specific, keep this answer in mind. TL;DR: Generators are platform-specific and you must be in Windows to do that.
    – code_dredd
    Mar 3, 2019 at 2:24

7 Answers 7

64

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.

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.

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

4
  • 12
    If this was true once, it isn't any longer. Any changes to the CMakeLists.txt will cause a regeneration of the build system (project files for visual studio, makefiles, etc). The workflow within Visual Studio is kind of annoying since Visual Studio doesn't regenerate the project files when it detects that things have changed but instead waits for you to do a build which causes a dialog to appear since the build needs to be aborted to re-load the project file.
    – Vitali
    Aug 20, 2011 at 7:16
  • 1
    The caveat to this is that you don't have to run the Visual Studio compiler through the IDE. You can run it on the command line: C:\...> MSBuild ALL_BUILD.vcxproj
    – PfunnyGuy
    Nov 14, 2017 at 18:53
  • 2
    @PfunnyGuy See also cmake --build ..
    – detly
    Jul 3, 2018 at 22:46
  • 2
    @detly Thanks! I actually found that, and use cmake --build . --config Debug -- /nologo /verbosity:minimal /m. I'll insert after . target run_tests to run my googletest unit test, and "Debug" can be replaced with "Release". (Adding the config option for Debug is optional, as it is the default, but I included it to show how it can be swapped with "Release")
    – PfunnyGuy
    Jul 25, 2018 at 14:46
58

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 -G "Visual Studio 10"

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

2
  • 1
    At stackoverflow.com/questions/11269833/… @Ivan points out that this can be put into a file PreLoad.cmake in the same folder as your top-level CMakeLists.txt and then you can just do cmake.
    – PfunnyGuy
    Nov 14, 2017 at 19:14
  • 2
    And for VS 2019, the option is: "Visual Studio 16 2019".
    – Sabuncu
    Jun 2, 2022 at 21:53
27

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.

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.

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

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).

13

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.

Adding and removing source files is just a matter of modifying the CMakeLists.txt which 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).

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

CMake and Visual Studio

1
  • 2
    That link and its sample project are very, very useful, thanks for that! I would recommend incorporating some of the source code from the github repo into your answer, for example mentioning that a CMake project is the equivalent of a solution, and that something like add_executable is used to add the equivalent of a visual studio project.
    – jrh
    Jan 10, 2020 at 20:23
10

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.

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.

6

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

  1. All source files go to /src and files visible in Visual Studio are just "links" to them defined in .filter.
  2. Programmer adds/deletes files remembering to work on the defined /src directory, not the default project's one.
  3. When he's done, he run a script that "refreshes" the respective .cmake files.
  4. He checks if the code can be built in the recreated environment.
  5. He commits the code.

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 .cmake files.

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 two directories 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.

1
  • 5
    As of CMake 2.8.10, generated solutions have the 4 customary build configurations, and there is support for defining additional custom ones.
    – John
    Jan 29, 2014 at 17:46
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.