Michael
Michael

Reputation: 591

How can I import a CLion C++ project into Visual Studio?

It seems like this should be easy and I've spent hours trying to find this answer online but haven't had any luck.

I can open the CLion folder path in Visual Studio, but that option doesn't give me the usual options to build and start the project. To do that, I need to open CLion as a Project/Solution, but I can't seem to do this.

My professor requires that my C++ code be executable in Visual Studio, but I prefer CLion. So I've done all my work in CLion and want to test that it runs in Visual Studio. How can I import my CLion project?

Thanks!

Upvotes: 2

Views: 6094

Answers (1)

Kevin
Kevin

Reputation: 18243

The link provided describes Visual Studio's CMake integration, which (similar to CLion) will install a version of CMake that Visual Studio will use. These instructions are pretty thorough and should provide everything needed to get your CMake project working in Visual Studio.

Now, you probably have two versions of CMake installed on your machine, one that came with CLion and one that came with Visual Studio. I would recommend installing the latest version of CMake on your machine separately, and configuring both Visual Studio and CLion to use that version instead. However, this is probably getting outside the scope of your immediate problem.


As you follow the Microsoft's instructions for "CMake projects in Visual Studio", you mentioned receiving the error:

1> [CMake] CMake Error at C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.17/Modules/CMakeDetermineSystem.cmake:173 (file): 
1> [CMake] file failed to open for writing (No such file or directory):

This looks like a permissions issue, specifically while running CMake within Visual Studio, so be sure you have read/write access to all the files in your project, and the CMake packages in your Visual Studio installation. Hopefully, this doesn't require you re-install Visual Studio in another location on your machine, or run Visual Studio with elevated privileges, but perhaps that is necessary.


If you decide to install CMake separately, the instructions would be the following:

  1. Install the latest CMake on your machine (somewhere you have adequate permissions), and ensure it is available in your Path environment variable. You can verify this by running cmake -version from the command line to see it is the version you just installed.

  2. Using Windows command prompt, navigate to your CMake project directory (containing the top-level CMakeLists.txt file), and run the following:

    mkdir build
    cd build
    cmake -G "Visual Studio 16 2019" -A x64 ..
    

    You may run -A Win32 with the cmake command instead if your project is a 32-bit build.

  3. Open the .sln Visual Studio Solution file that was generated in the build directory. Once, the Solution is loaded in Visual Studio, you can build the project (CTRL + SHIFT + B).

Upvotes: 3

Related Questions