【问题标题】:How to set compile flags for external INTERFACE_SOURCES in CMake?如何在 CMake 中为外部 INTERFACE_SOURCES 设置编译标志?
【发布时间】:2016-05-04 19:45:44
【问题描述】:

我在 cmake 中使用了一个使用 INTERFACE_SOURCES 的外部包。这意味着当我将导入的库链接到我的目标时,接口源文件会自动添加到我的目标中。当我编译我的目标时,那些外部文件也会被编译。

这对我来说是个问题,因为外部文件会导致编译警告。我想通过在编译外部文件时设置较低的警告级别来删除警告。但我不知道该怎么做。

这是我目前得到的。

# reduce the warning level for some files over which we have no control.
macro( remove_warning_flags_for_some_external_files myTarget )

    # blacklist of files that throw warnings
    set( blackListedExternalFiles 
        static_qt_plugins.cpp
    )

    get_target_property( linkedLibraries ${myTarget} LINK_LIBRARIES )

    foreach(library ${linkedLibraries})

        get_property( sources TARGET ${library} PROPERTY INTERFACE_SOURCES )

        foreach(source ${sources})

            get_filename_component(shortName ${source} NAME)

            if( ${shortName} IN_LIST blackListedExternalFiles)

                # everything works until here
                # does not work
                get_source_file_property( flags1 ${source} COMPILE_FLAGS)     
                # does not work
                get_property(flags2 SOURCE ${source} PROPERTY COMPILE_FLAGS) 

                # exchange flags in list, this I can do

                # set flags to source file, do not know how to

            endif()

        endforeach()
    endforeach()
endmacro()

这是应该做的

  • 浏览所有链接库并获取外部 INTERFACE_SOURCES 源文件。
  • 检查每个外部源文件是否出现在黑名单中。如果是这样,请将其编译标志更改为较低级别。

我遇到的问题是获取和设置那些 INTERFACE_SOURCES 的编译标志。 get_source_file_property() 和 get_property() 调用不返回任何内容。

如何获取和设置这些似乎不属于我的目标但同时编译的文件的标志?

【问题讨论】:

    标签: cmake compiler-flags


    【解决方案1】:

    get_source_file_property() 和 get_property() 调用不返回任何内容

    COMPILE_FLAGS 源文件的属性不会被target_compile_options 等“目标”命令修改。最后一组标志是全局+目标特定+源特定的混合。据我了解,没有办法关闭全局或目标标志的“继承”。

    作为一种解决方法,您可以通过添加 -w 选项来禁用警告:

    get_source_file_property(flags "${external_source}" COMPILE_FLAGS)
    if(NOT flags)
      set(flags "") # convert "NOTFOUND" to "" if needed
    endif()
    set_source_files_properties(
        "${external_source}"
        PROPERTIES
        COMPILE_FLAGS "-w ${flags}"
    )
    

    仅供参考:How to set warning level in CMake?

    备案:最初来自issue #408

    【讨论】:

    • 感谢您的帮助。所以该文件似乎没有任何标志。我认为这是一个错误。只需设置标志即可。
    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    相关资源
    最近更新 更多