Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a CMake project to a Visual Studio solution

I am working on a Visual Studio 2013 project and would like to link it to a library that uses CMake to generate a build configuration. For example:

project
|-> src
    |-> project.sln
|-> dep
    |-> library
        |-> src
            |-> CMakeLists.txt

Is there a way to configure, build, and link my library to my project when I build the project in Visual Studio?

I would like to eventually make the whole project a CMake project and generate a comprehensive Visual Studio solution, but it is currently quite large and complicated. With limited time, I'm wondering what my best option is. Is there a clean way to do this with VS Custom Build commands?

like image 258
Neal Kruis Avatar asked Feb 21 '17 19:02

Neal Kruis


People also ask

How do I add CMake to PATH in Visual Studio?

To run an explicitely installed CMake you should download a CMake suitable for your platform from https://cmake.org/download install it and add the bin folder of the installation directory to your PATH variable, e.g. set PATH=C:\Program Files\CMake\bin;%PATH% on Windows.

How do I open CMake-GUI in Visual Studio?

Running CMake for Windows / Microsoft Visual C++ (MSVC)Run cmake-gui.exe, which should be in your Start menu under Program Files, there may also be a shortcut on your desktop, or if you built from source, it will be in the build directory.

Does Visual Studio code support CMake?

The latest version of CMake tools is now available for download in the VS Code Marketplace or using the . vsix file.


1 Answers

Here is most simplified version of CMake's configuration and build steps.

Just create a Configuration Type / Utility project in the same folder as your CMakeLists.txt and add:

  • Pre-Build Event / Command Line

    IF NOT EXIST "bin\*.sln" ( cmake -H"." -B"bin" )
    

    or for newer versions of CMake

    IF NOT EXIST "bin\*.sln" ( 
        cmake -H"." -B"bin" -DCMAKE_MAKE_PROGRAM:PATH="$(DevEnvDir)\devenv.exe"
    )
    

    Just because I personally don't like it to use msbuild.exe (which would be the default).

  • Post-Build Event / Command Line

    cmake --build "bin" --config "$(Configuration)"
    

Alternative

You can also create a root CMakeLists.txt importing existing .vcproj files via include_external_msproject() commands:

cmake_minimum_required(VERSION 2.6)

project(project)

include_external_msproject(${PROJECT_NAME} src/${PROJECT_NAME}.vcxproj)
...
add_subdirectory(dep/library/src library)

Reference

  • How to know whether the cmake project generation ended with success
  • MSBuild.exe has stopped cmake error
like image 59
Florian Avatar answered Sep 28 '22 22:09

Florian