c++windowscmakebuildsln-file

Building .SLN files on Windows without Visual Studio?


I have recently been trying to set up my CMake environment and some 'hello world' code in C++. I added a CMakeLists.txt and added my configurations, but when I ran cmake . in the command line, something was different from all of the tutorials.

The people on the tutorials were using a Unix based system, so the command cmake . was producing a 'makefile'. They then built the makefile using the command make.

Since I'm on windows, it generated a msvc .sln file instead of a makefile. My question is - how can I build the .sln file, similar to how they did it on Linux? I want to do it without Visual Studio 2019 and preferably in the command prompt.

I have tried searching for this question, but haven't found what I'm looking for. Thank you in advance.


Solution

  • First of all, you should never do an in-tree build with cmake .. It invites problems in the form of name clashes and makes it nearly impossible to get a clean rebuild.

    If you're using a recent version of CMake (which you should be), the standard way to build a project varies on whether the backend generator is single-config or multi-config.

    If it's single-config (like Make or Ninja), then the commands are:

    $ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S /path/to/sources -B /path/to/build
    $ cmake --build /path/to/build
    

    The directory /path/to/build doesn't need to exist when you invoke CMake. If you wanted a Debug build, rather than Release, you would just replace that in the first line. You should never run a single-config generator without setting CMAKE_BUILD_TYPE.

    If it's multi-config, like Visual Studio, then the commands are:

    $ cmake -G "Visual Studio 16 2019" -A x64 -Thost=x64 -S /path/to/sources -B /path/to/build
    $ cmake --build /path/to/build --config Release
    

    The major difference here is that the config is specified in the second (build) command, rather than the first (configure).