Cmake: generator selection in CMakeLists.txt

I would like to force CMake to use the "Unix Makefile" generator from CMakeLists.txt.

This is the command I'm using now.

cmake -G "Unix Makefiles" . 

I wish it were.

 cmake . 

When working on Windows with VC and a custom toolchain installed.

I expect that I can install the generator in the CMakeLists.txt file.

Perhaps something like this.

 set(CMAKE_GENERATOR "Unix Makefiles") 
+16
cmake
Jun 29 '12 at 22:41
source share
3 answers

It seems to me that the CMAKE_GENERATOR variable CMAKE_GENERATOR set too late if it is set in 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 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 

Thus, the variable is set only at the very end of the generation procedure. If you use something like

 set(CMAKE_GENERATOR "Ninja" CACHE INTERNAL "" FORCE) 

in CMakeLists.txt , then the very first run of cmake ../source (without -G ) uses the default generator. However, the CMAKE_GENERATOR variable CMAKE_GENERATOR stored in the cache. Therefore, if you re-run cmake ../source after this, it will use the generator as specified in the CMAKE_GENERATOR variable in the cache.

This, of course, is not the most elegant solution, though ;-) Perhaps use a batch file that actually runs cmake -G generator for the user ...

+7
Apr 23 '15 at 9:34
source share

Here's what worked for me - create a PreLoad.cmake file in the project file containing this:

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

+5
Jul 21 '17 at 22:47
source share

This is not what I get when I run the same command, cmake will look for the gcc compiler / make utility. If PATH is not configured correctly, it will not work with something like:

 D: \ Development \ build> cmake -G "Unix Makefiles" .. \ source
 CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles".  CMAKE_MAKE_PROGRAM is not set.
   You probably need to select a different build tool.
 CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
 Missing variable is:
 CMAKE_C_COMPILER_ENV_VAR
 CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
 Missing variable is:
 CMAKE_C_COMPILER
 CMake Error: Could not find cmake module file: D: /Development/build/CMakeFiles/CMakeCCompiler.cmake
 CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
 Missing variable is:
 CMAKE_CXX_COMPILER_ENV_VAR
 CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
 Missing variable is:
 CMAKE_CXX_COMPILER
 CMake Error: Could not find cmake module file: D: /Development/build/CMakeFiles/CMakeCXXCompiler.cmake
 CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
 CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
 - Configuring incomplete, errors occurred!

when gcc / mingw is on the way, everything works fine. So can you provide more information about your PATH variable or version of CMAKE?

+2
Jul 02 2018-12-12T00:
source share



All Articles