【问题标题】:How can you add warning flags using cmake cross platform?如何使用 cmake 跨平台添加警告标志?
【发布时间】:2016-07-15 07:59:09
【问题描述】:

我看到一些文章建议您检查编译器并酌情添加标志,例如。

if (CMAKE_COMPILER_IS_GNUCC) ... endif() if (MSVC) ... endif()

不过,这是一个非常不受欢迎的情况。

它依赖于您为每个项目添加对您支持的每个编译器的特定支持,一次一个。

其他内容,例如 C++11 featuresdebug flags 由 cmake 为其支持的每个编译器自动生成。

是否没有等效的解决方案可以通过 cmake 设置将等效的 -Wall / /W3 添加到编译中?

【问题讨论】:

    标签: cmake


    【解决方案1】:

    它依赖于您为每个项目添加特定的支持 >您支持的每个编译器,一次一个。

    目前你只能有compiler.cmake之类的东西,你可以在其中为每个编译器配置合适的标志,并在项目之间共享compiler.cmake

    是否没有等效的解决方案可以通过 cmake 设置将等效的 -Wall / /W3 添加到 >compile 中?

    不,现在只讨论类似的功能和可能的实现,请参阅

    https://cmake.org/pipermail/cmake-developers/2016-March/028107.html

    【讨论】:

      【解决方案2】:

      对于发现此内容的其他人...

      有一个相当健壮的实现,可以在这里找到,作为第 3 方添加:

      https://github.com/ruslo/sugar/wiki/Cross-platform-warning-suppression

      你可以这样使用它:

      ## Project
      cmake_minimum_required(VERSION 3.1)
      project(npp)
      
      # Dependencies
      include_directories(${CMAKE_CURRENT_SOURCE_DIR}/npp)
      ... whatever ...
      
      # Clone entire sugar repo to source folder and import
      include(${CMAKE_CURRENT_SOURCE_DIR}/sugar/cmake/Sugar)
      include(sugar_generate_warning_flags)
      
      # Generate flags, included excluded flags, etc.
      # see: https://github.com/ruslo/leathers/wiki/List
      sugar_generate_warning_flags(
        flags
        properties
        ENABLE ALL
        DISABLE c++98-compat padded
        TREAT_AS_ERROR ALL
        CLEAR_GLOBAL)
      
      # Library / executable if any
      file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/npp/*.cpp)
      add_library(npp STATIC ${SOURCES})
      
      # Set flags
      set_target_properties(npp PROPERTIES ${properties} COMPILE_OPTIONS "${flags}")
      
      # Local tests
      enable_testing()
      add_executable(tests "${CMAKE_CURRENT_SOURCE_DIR}/tests/tests.cpp")
      
      # Set flags
      set_target_properties(tests PROPERTIES ${properties} COMPILE_OPTIONS "${flags}")
      target_link_libraries(tests npp)
      add_test(tests tests)
      

      显然这远非理想,因为不得不克隆一组模块非常烦人,但目前还很实用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-23
        • 2012-08-25
        • 1970-01-01
        相关资源
        最近更新 更多