cmake: Selecting a generator within CMakeLists.txt

前端 未结 4 708
有刺的猬
有刺的猬 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...

提交回复
热议问题