Appending to CMAKE_C_FLAGS

后端 未结 3 1928
谎友^
谎友^ 2021-02-18 22:22

I\'m using CMake for a project that comes in two versions, one of which requires -lglapi and the other does not.

So far the lines we used look like that:



        
相关标签:
3条回答
  • 2021-02-18 22:35

    Since CMake 3.4 you do:

    string(APPEND CMAKE_CXX_FLAGS " -lglapi")
    

    This very handy when you want to set the flags only for one language (C++ in the example above), but if you want to set the same flags for all languages, you can simply do:

    add_compile_options(-lglapi)
    

    Both commands change the flags for the whole directory, if you want to set the flags for only one target, do:

    target_compile_options(my_lib PUBLIC -lglapi)
    

    Flags on a target can either be PUBLIC, PRIVATE or INTERFACE, allowing to transitively forward the flags from one target to the other.

    0 讨论(0)
  • 2021-02-18 22:55

    Just for the particular case where you want to add compiler and linker options (like for --coverage option), the following syntax add the flags to both the linker and compiler:

    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
    

    (above commands add --coverage option to both compiler and flags options for C and C++)

    Otherwise, if you prefer to use new CMake commands (add_compile_options and add_link_options), don't forget to add to the linker option too:

    add_compile_options(--coverage)
    add_link_options(--coverage)
    

    (above commands add --coverage option to both compiler and flags options for C and C++)

    0 讨论(0)
  • 2021-02-18 22:57

    Try to do this instead:

    if(SINGLE_MODE)
        SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lglapi")
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lglapi")
    endif(SINGLE_MODE)
    

    Then, you are sure you append -lglapi to the existing ${CMAKE_CXX_FLAGS} string. Else, looks like something like a CMake list is being created.

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