1

I would like to convert my project from a Visual Studio solution to build with CMake and compile it with Makefiles.

This is a 2-part question.

  1. Right now the CMakeLists.txt is:

    cmake_minimum_required(VERSION 3.13.0)
    
    project(Project2015 CXX)
    
    add_executable(Project Source/main.cpp)
    

When I run cmake .. out of the build directory, it generates *.vcxproj and *.sln files, but there is no Makefile. How can I change the CMakeLists file to generate a Makefile?

  1. What is the command line equivalent compiler to gcc for windows? And how do I set this compiler as the target for CMake and the generated Makefile? Reading about the build tools https://learn.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019 Do I need to target the cl.exe compiler? Would this work with CMake and Makefiles? I'm reading online that these command line flags will set the compiler, how can I add these to the CMakeLists.txt to be used automatically?

    DCMAKE_C_COMPILER=cl 
    DCMAKE_C_COMPILER_FORCED=ON 
    DCMAKE_CXX_COMPILER=cl 
    DCMAKE_CXX_COMPILER_FORCED=ON 
    DCMAKE_BUILD_TYPE=Debug 
    DCMAKE_INSTALL_PREFIX=%CFITSIO_DIR% 
    G"NMake Makefiles"
    
Kevin
  • 16,549
  • 8
  • 60
  • 74
Mich
  • 3,188
  • 4
  • 37
  • 85
  • You need to download and install MinGW or similar for Windows, and tell CMake to it instead of Visual Studio. – Kevin Apr 06 '20 at 19:09
  • Im not trying to build a project for Linux, I just want to build through command line instead of having to load visual studio. MinGW is for building native Linux apps. Unfortunately the link doesn't help. There has to be a command line compiler for windows that I should be able to target with Cmake – Mich Apr 06 '20 at 19:12
  • You ask "*How can I change CMakeLists to generate a makefile?*"... Visual Studio does not use Makefiles. Please refer to [this link](https://cmake.org/cmake/help/v3.0/manual/cmake-generators.7.html#command-line-build-tool-generators) in the CMake docs for a list of the command line CMake generators. – Kevin Apr 06 '20 at 19:15
  • FWIW, you can still use Visual Studio and perform the entire CMake configure and compilation on the command line. Is that the ultimate goal? – Kevin Apr 06 '20 at 19:18
  • No, the goal is to `not` use visual studio, and compile from command line – Mich Apr 06 '20 at 19:26
  • As stated, this is a "read the fine manual" question, pure and simple, and as such not appropriate for SO, so please go do that. If you have done that and you have problems using the information you've found there, please ask for help with _that_ instead, by showing the commands you entered and showing the results you got (cut down to the important parts) and describing why those results aren't what you want. – MadScientist Apr 06 '20 at 19:30
  • Ok, you'll have to settle on which other CMake generator to use, from the list in the link I provided, then you can tell CMake to use that generator using, for example, `cmake -G"NMake Makefiles" ..`. NMake should come with Visual Studio, which does generate Makefiles. However, I think it still uses the Visual Studio compiler, so that may not be the generator you want. – Kevin Apr 06 '20 at 19:32
  • Mad Scientist, I have added information from the manuals and various documentation I found online to the question Unfortunately, the manual doesn't have information on using cmake to build with visual studio build tools. This is a very appropriate question for SO, as I have not been able to find similar questions. – Mich Apr 06 '20 at 19:34
  • Thanks Squarekitties, I think your answer about using Nmake is what Im looking for, please feel free to add it as answer. – Mich Apr 06 '20 at 20:02
  • How can I add the `-G"NMake Makefiles"` flag so its automatically used if I type in `cmake ..` without the command line option. I tried `set(CMAKE_CXX_FLAGS -G"NMake Makefiles")` but it doesn't work – Mich Apr 06 '20 at 20:07
  • 1
    The second part of your question is actually **several questions** at once. E.g. the last one (generator) is answered there: https://stackoverflow.com/questions/11269833/cmake-selecting-a-generator-within-cmakelists-txt, the one about build type is answered here https://stackoverflow.com/questions/48832233/have-a-cmake-project-default-to-the-release-build-type (and some other places) and so on. This is why on Stack Overflow we prefer **single question per question post**. – Tsyvarev Apr 06 '20 at 21:11

2 Answers2

2

As suggested by @vre, you can run everything from the command line, while still using the Visual Studio generator. Just use CMake's command line build tools:

cmake ..
cmake --build . --config Release

This way, you don't have to open Visual Studio at all to build your libraries/executables.

Another option is to use Microsoft's nmake utility, which will generate NMake Makefiles. You can tell CMake to use this generator instead using this:

cmake -G"NMake Makefiles" ..

The full list of CMake generators you can choose from is listed here.

If you don't want to manually set the CMake generator in the command line, you can set it at the top of your CMakeLists.txt file:

set (CMAKE_GENERATOR "NMake Makefiles" CACHE INTERNAL "" FORCE)

It will be used on the second CMake configuration in this case, as the first run will use the system default generator. If you want CMake to use it on the first configuration, you can utilize the Preload.cmake procedure outlined in this answer.

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • Thanks for a simple and correct answer. Nice to see you are in the [list](https://stackoverflow.com/tags/cmake/topusers) for giving answers for CMake tag. – Guy Coder Aug 17 '22 at 20:09
  • Error: could not load cache. This is a fresh install of cmake and windows machine. – jjxtra Apr 19 '23 at 13:40
1

You should use the build tool mode of CMake for builds from the command line.

After configuring your project for a 64bit build using Visual Studio 2019 e.g. with

cmake -S <sourcedir> -B <builddir> -G "Visual Studio 16 2019" -A x64 

you would run

cmake --build <builddir> --target ALL_BUILD --config Release

For further options see here for an almost quiet build from the command line see here.

vre
  • 6,041
  • 1
  • 25
  • 39