8

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?

Neal Kruis
  • 2,055
  • 3
  • 26
  • 49

1 Answers1

4

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

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • 3
    How to "create a Configuration Type / Utility project " ? In VS2019 under File > New > Project, there are no results for searching "Configuration" or "Utility" – M.M Mar 29 '21 at 00:33