Skip to content

Instantly share code, notes, and snippets.

@kopp
Created November 12, 2024 13:17
Show Gist options
  • Save kopp/a98a803ff2eff5738815f6cfa8e88f91 to your computer and use it in GitHub Desktop.
Save kopp/a98a803ff2eff5738815f6cfa8e88f91 to your computer and use it in GitHub Desktop.
cmake: Selecting a generator within CMakeLists.txt (SO https://stackoverflow.com/q/11269833)
cmake_minimum_required(VERSION 3.31.0)
project(build_with_ninja CXX)
set(CMAKE_GENERATOR "Ninja" CACHE INTERNAL "" FORCE)
message("generator is set to ${CMAKE_GENERATOR}")
mkdir source
mv CMakeLists.txt source/
mkdir build
cmake -S source -B build
ls build/Makefile build/build.ninja
@J-Siu
Copy link

J-Siu commented Nov 12, 2024

Following is what I did:

cmake_minimum_required(VERSION 3.31)

set(CMAKE_GENERATOR "Ninja")
message("generator is set to ${CMAKE_GENERATOR}")

set(CMAKE_CXX_STANDARD 20)

project(<PROJ NAME>)

Command line:

cd <PROJ FOLDER>
rm -rf build
cmake -S . -B build
cmake --build build

@kopp
Copy link
Author

kopp commented Nov 12, 2024

Interestingly I still get a Makefile in this case @J-Siu ...

So to confirm, if you do not set the generator, you get a Makefile (system default) and when you run the code above you get a ninja file?

I'm running on Arch Linux with cmake

cmake version 3.31.0

CMake suite maintained and supported by Kitware (kitware.com/cmake).

@J-Siu
Copy link

J-Siu commented Nov 12, 2024

I am on MacOS. I ran into this as VSCode seems to work better with ninja(as I don't know how to configure vscode task yet). I used the above to force a switch from makefile to ninja.

@kopp
Copy link
Author

kopp commented Nov 12, 2024

ok, interesting. So I cannot confirm, but glad that it works for you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment