I would like to force CMake to use the "Unix Makefiles" generator from within CMakeLists.txt.
This is the command I use now.
cmake -G "Unix Makefiles" .
I would like it to be this.
cmake .
When running on windows with VC installed and a custom tool-chain.
I would expect to be-able to set the generator in the CMakeLists.txt
file.
Maybe something like this.
set(CMAKE_GENERATOR "Unix Makefiles")
Use its -G option to specify the generator for a new build tree. The cmake-gui(1) offers interactive selection of a generator when creating a new build tree.
For those seeking the CMake GUI answer. Go to File->Delete Cache and then click Configure again. A scenario where changing the generator is needed is that you are keeping the CMake GUI open and reusing the same directory (source and CMakeList.
When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory. To open a project, you can point CLion to the top-level CMakeLists. txt and choose Open as Project.
If you create the cache variable in the CMakeLists. txt file and then pass the argument via calling cmake, won't the CMakeList. txt file keep overwriting the argument value? No, the cache is populated on the first run with either the default value, or the value supplied on the command line if it is provided.
Here is what worked for me - create a file called PreLoad.cmake in your project dir containing this:
set (CMAKE_GENERATOR "Unix Makefiles" CACHE INTERNAL "" FORCE)
It seems to me that the variable CMAKE_GENERATOR
is set too late if set in the CMakeLists.txt
. If you use (even at the beginning of CMakeLists.txt
)
set(CMAKE_GENERATOR "Ninja") message("generator is set to ${CMAKE_GENERATOR}")
you can see in the output something like
% cmake ../source -- The C compiler identification is GNU 4.9.2 ... -- Detecting CXX compile features - done generator is set to Ninja -- Configuring done -- Generating done -- Build files have been written to: /tmp/build
So the variable is only set at the very end of the generation procedure. If you use something like
set(CMAKE_GENERATOR "Ninja" CACHE INTERNAL "" FORCE)
in CMakeLists.txt
, then in the very first run of cmake ../source
(without -G
) the default generator is used. The variable CMAKE_GENERATOR
is stored in the cache, though. So if you rerun cmake ../source
afterwards, it will use the generator as specified in the CMAKE_GENERATOR
variable in the cache.
This is surely not the most elegant solution, though ;-) Maybe use a batch file that will actually execute the cmake -G generator
for the user...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With