Ali Mirzaei
Ali Mirzaei

Reputation: 1552

Open Cmake Project in Visual Studio like Qt Creator

I used to program at Qt Creator in linux But now I am working on Windows and Visual Studio (I am forced to it). When I was programming in Qt Creator these features was so great and useful for me:

  1. Opening several cmake project at the same time and easy switching between them.
  2. There was no need to run cmake command in terminal before opening the project and Qt Creator was handling this matter itself. Therefore, if there was any error in CMakeLists.txt I could correct it from inside of Qt Creator

Now, In Windows you should run cmake command in cmd before you open the project in Visual Studio.

Are there any Extensions or Add-on's for Visual Studio to have the mentioned features of Qt Creator for opening a cmake project?

Do you have any other suggestion for working on a cmake project in Visual Studio?

Upvotes: 3

Views: 3118

Answers (3)

VonC
VonC

Reputation: 1323065

Now (2015), in Windows you should run cmake command in cmd before you open the project in Visual Studio.

Not anymore, with recent version of Visual Studio 19 (2020): See "Build systems and projects"

Open a folder that contains a CMakeLists.txt file.
CMake support is integrated into Visual Studio. You can use the IDE to edit, test and debug without modifying the CMake files in any way. This enables you to work in the same CMake project as others who might be using different editors.
CMake is the recommended approach for cross-platform development. For more information, see CMake projects.

No need to generate any files first.

That is why a project like Git, for instance, will ignore said generated files: they are not needed anymore to open a CMake project:

With Git 2.29 (Q4 2020), using the CMake support added some time ago for real with Visual Studio build revealed there were lot of usability improvements possible, which have been carried out.

See commit 0ad621f (30 Sep 2020) by Junio C Hamano (gitster).
See commit f2f1250, commit b490283, commit 2d9eb4e, commit 8c35e82, commit f1bd737, commit 8f45138 (30 Sep 2020), commit e18ae4e, commit 72b6eeb (28 Sep 2020), and commit 3eccc7b (25 Sep 2020) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 8250ab0, 05 Oct 2020)

cmake: ignore files generated by CMake as run in Visual Studio

Helped-by: Đoàn Trần Công Danh
Signed-off-by: Johannes Schindelin

As of recent Visual Studio versions, CMake support is built-in: https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=vs-2019

All that needs to be done is to open the worktree as a folder, and Visual Studio will find the CMakeLists.txt file and automatically generate the project files.

Let's ignore the entirety of those generated files.

That has practical consequence on a project on-boarding.
If we take the example of a project like Git, again:

cmake (Windows): initialize vcpkg/build dependencies automatically

Signed-off-by: Johannes Schindelin

The idea of having CMake support in Git's source tree is to enable contributors on Windows to start contributing with little effort. To that end, we just added some sensible defaults that will let users open the worktree in Visual Studio and start building.

This expects the dependencies (such as zlib) to be available already, though. If they are not available, we expect the user to run compat/vcbuild/vcpkg_install.bat.

Rather than requiring this step to be manual, detect the situation and run it as part of the CMake configuration step.

Note that this obviously only applies to the scenario when we want to compile in Visual Studio (i.e. with MS Visual C), not with GCC. Therefore, we guard this new code block behind the MSVC conditional.

This concludes our journey to make it as effortless as possible to start developing Git in Visual Studio: all the developer needs to do is to clone Git's repository, open the worktree via File>Open>Folder... and wait for CMake to finish configuring.

Upvotes: 2

Phil
Phil

Reputation: 6162

No. QtCreator plays better with cmake than Visual Studio. With QtCreator you can open the CMakeLists.txt as a "project" and run the cmake config step directly. With Visual Studio, you must run the initial cmake config (with the generator option) first to generate the projects and solution. With QtCreator you can also open multiple projects (corresponding to multiple top-level CMakeLists.txt files) together. With Visual Studio, you can only open one solution at a time, and each top-level CMakeLists.txt corresponds to a solution.

While QtCreator works better with cmake than Visual Studio, cmake and Visual Studio are still an excellent combination (and as a whole my preferred working toolset)--just one for which you must run the inital cmake configure step before being able to open the solution. Note that it is just the initial cmake config step that is required. Once you have generated the solution and project files and are using the VS IDE, subsequent changes to CMakeLists.txt files or any input files to the configure_file command will cause cmake to reconfigure before VS builds the solution.

Also Visual Studio 2013 works better than 2010 because when you do trigger a cmake reconfigure it will ask you if you want to reload all the projects. VS 2010 will prompt you to reload each one, which is a pain when you have a lot of projects. (I typically have 20-100 projects in a medium to large codebase.) And sometimes VS 2010 will crash with cmake reconfigures. (Nothing is lost--it is just a pain to have the IDE crash and have to re-open it.)

As for other suggestions:

  • my comment above about automatic reconfigures, is based on not setting CMAKE_SUPPRESS_REGENERATION to ON as suggested in a comment above by drescherjm. (Based on your workflow, this may be a fine way to proceed, it just conflicts with the way I have used cmake/VS and will prevent reconfigures as I described.)

  • For building you do not have to use the vcvarsall.bat script as described by DevSolar because that is all sorted out by cmake with the -G argument. For a 64-bit build with VS 2013, I use -G "Visual Studio 12 2013 Win64". (Note VS 2010 is vc10, VS 2012 is vc11, and VS 2013 is vc12.) It is still, however, helpful to have a correct runtime environment, which I get using

    call "%VS120COMNTOOLS%....\VC\vcvarsall.bat" amd64

  • You can get a command line build by cd'ing to the build directory (the one containing the generated solution file) and executing

    cmake --build . --config Release

  • You can also specify which project to build by adding --target MyProject

Upvotes: 2

e3oroush
e3oroush

Reputation: 3125

For your first question, Visual studio provide you a complete solution and you can add and switch between projects easily inside a solution. (see differences between projects and solutions in this post)

But for your second question, I don't know any official extension to do that. But once you created a solution with Cmake, you can easily change the CmakeList in VS.

If you insist in doesn't running Cmake commands, you can write an extension for visual studio see this for more information.

Upvotes: 2

Related Questions