0

I have a pretty large software library using CMake to be compiled. We use Linux mostly, but now a new colleague wants to use Visual Studio.

Is there any way to create a new VS 2017 project from the existing source codes with CMake structure?

I know, it's possible to do it with CLion, but I have no idea about VS, as I have a very little experience with it.

Other questions seem to focus on creating an empty project, which will use CMake, but not on creating a project from already existing source files.

Eenoku
  • 2,741
  • 4
  • 32
  • 64
  • 1
    You don't mean `cmake -G "Visual Studio 15 2017"` to generate a project do you? Because you say you already are using cmake on Linux, why wouldn't you just use the same 'CMakeLists.txt` and everything to generate the project? – fdk1342 Dec 29 '18 at 02:53
  • Yes, there is a way to do it. CMake is very good at this. The C in CMake stands for Cross-Platform, in fact! You open the CMake project in Visual Studio, try to compile it, if you run into platform specific problems, you modify CMakeLists.txt to compensate with platform specific branching. – Cinder Biscuits Dec 29 '18 at 14:28
  • @CinderBiscuits I know about cross-platform compilation. Would you mind to write a short answer about this specific IDE? – Eenoku Dec 29 '18 at 15:35

1 Answers1

0

I'm not sure why you asked for details but...

Assuming you are using cmake 3.13 then you can do the following in a command shell:

cmake -G "Visual Studio 15 2017" -S path_to_source -B path_to_build

This will then create a solution file. Actually it creates a solution file for every project() command that is issued in CMakeLists.txt.

You can then open the solution file in Visual Studio, and build the project as usual.

You don't even need to do this in the Visual Studio GUI. After creating the initial project you can also issue the command:

cmake --build path_to_build

Which will kick off the build at the command line.

Now if your CMakeLists.txt in path_to_source is using Linux specific libraries or gcc specific compiler settings then the CMakeLists.txt will have to get updated to the Windows equivalent.

The alternative is to start Visual Studio and then use File->Open->CMake and open the CMakeLists.txt file in path_to_source. It'll then start to generate the project. But I prefer using the command line method.

fdk1342
  • 3,274
  • 1
  • 16
  • 17