cmake: Selecting a generator within CMakeLists.txt

前端 未结 4 689
有刺的猬
有刺的猬 2020-11-30 03:39

I would like to force CMake to use the \"Unix Makefiles\" generator from within CMakeLists.txt.

This is the command I use n

相关标签:
4条回答
  • 2020-11-30 04:20

    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...

    0 讨论(0)
  • 2020-11-30 04:22

    This is not what I get, when I run the same command, cmake will look for a gcc compiler / make utility. If the PATH is not set up correctly it will fail 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 in the path then everything works fine. So could you provide more information as to your PATH variable or CMAKE version?

    0 讨论(0)
  • 2020-11-30 04:27

    You need to set the generator at the Generate stage so it is written into the cache. You only need to run this command once for the first configuration

    cmake .. -DCMAKE_GENERATOR:INTERNAL=Ninja

    This will configure Ninja as the default generator.

    Later you can simply run

    cmake ..

    And it would use the Ninja generator as default

    You can read more about it under Running CMake from the command line

    When running cmake from the command line, it is possible to specify command line options to cmake that will set values in the cache. This is done with a -DVARIABLE:TYPE=VALUE syntax on the command line. This is useful for non-interactive nightly test builds.

    0 讨论(0)
  • 2020-11-30 04:35

    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)

    0 讨论(0)
提交回复
热议问题