You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何添加CMAKE_COMPILE_WARNING_AS_ERROR OFF后仍触发警告转错误?

编译LLVM 18.1.3时"警告视为错误"的编译失败问题

问题场景

编译LLVM 18.1.3时触发"所有警告被视为错误"规则,导致编译中断,错误集中在libc组件的bsearch.cpp文件,具体错误信息如下:

/home/pi/llvm-project-llvmorg-18.1.3/libc/src/stdlib/bsearch.cpp:16:28: error: ‘void* __llvm_libc_18_1_3_::bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*))’ aliased to external symbol ‘bsearch’
   16 | LLVM_LIBC_FUNCTION(void *, bsearch,
      |                            ^~~~~~~
/home/pi/llvm-project-llvmorg-18.1.3/libc/src/__support/common.h:28:34: note: in definition of macro ‘LLVM_LIBC_FUNCTION_IMPL’
   28 |   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
      |                                  ^~~~
/home/pi/llvm-project-llvmorg-18.1.3/libc/src/stdlib/bsearch.cpp:16:1: note: in expansion of macro ‘LLVM_LIBC_FUNCTION’
   16 | LLVM_LIBC_FUNCTION(void *, bsearch,
      | ^~~~~~~~~~~~~~~~~~
/home/pi/llvm-project-llvmorg-18.1.3/libc/src/stdlib/bsearch.cpp:16:28: error: ‘void* __llvm_libc_18_1_3_::bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*))’ specifies less restrictive attribute than its target ‘void* bsearch(const void*, const void*, size_t, size_t, __compar_fn_t)’: ‘nonnull’ [-Werror=missing-attributes]
   16 | LLVM_LIBC_FUNCTION(void *, bsearch,
      |                            ^~~~~~~
/home/pi/llvm-project-llvmorg-18.1.3/libc/src/__support/common.h:28:34: note: in definition of macro ‘LLVM_LIBC_FUNCTION_IMPL’
   28 |   decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]];                   \
      |                                  ^~~~
/home/pi/llvm-project-llvmorg-18.1.3/libc/src/stdlib/bsearch.cpp:16:1: note: in expansion of macro ‘LLVM_LIBC_FUNCTION’
   16 | LLVM_LIBC_FUNCTION(void *, bsearch,
      | ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdlib.h:846,
                 from /usr/include/c++/12/cstdlib:75,
                 from /usr/include/c++/12/stdlib.h:36,
                 from /home/pi/llvm-project-llvmorg-18.1.3/libc/src/stdlib/bsearch.h:12,
                 from /home/pi/llvm-project-llvmorg-18.1.3/libc/src/stdlib/bsearch.cpp:9:
/usr/include/arm-linux-gnueabihf/bits/stdlib-bsearch.h:20:1: note: ‘void* __llvm_libc_18_1_3_::bsearch(const void*, const void*, size_t, size_t, int (*)(const void*, const void*))’ target declared here
   20 | bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
      | ^~~~~~~
cc1plus: all warnings being treated as errors

尝试的无效修复

在libc/src/stdlib/CMakeLists.txt末尾添加set(CMAKE_COMPILE_WARNING_AS_ERROR OFF),修改后的文件内容:

…
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
  add_entrypoint_object(
    malloc
    ALIAS
    DEPENDS
      .${LIBC_TARGET_OS}.malloc
  )

  add_entrypoint_object(
    free
    ALIAS
    DEPENDS
      .${LIBC_TARGET_OS}.free
  )
endif()

set(CMAKE_COMPILE_WARNING_AS_ERROR OFF)

重新编译后错误依然存在。

修复操作的问题分析

  1. CMake变量作用域限制:在子目录的CMakeLists.txt中设置CMAKE_COMPILE_WARNING_AS_ERROR,仅会影响该语句之后定义的编译目标,而bsearch的目标在该语句之前就已定义,因此设置不生效。
  2. LLVM项目专属控制逻辑:LLVM构建系统不依赖通用的CMAKE_COMPILE_WARNING_AS_ERROR变量,而是使用自身的LLVM_ENABLE_WARNINGS_AS_ERRORS变量全局控制警告转错误规则,libc模块还有独立的警告配置。
  3. 命令行选项被覆盖:cmake命令中的--compile-no-warning-as-error选项,会被LLVM自身的CMake脚本强制开启的警告转错误规则覆盖。

可行解决方案

方案1:全局关闭LLVM的警告转错误

在cmake命令中添加-DLLVM_ENABLE_WARNINGS_AS_ERRORS=OFF,替换或保留原命令中的--compile-no-warning-as-error,修改后的完整命令:

cmake -S llvm -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=/usr/local -DLLVM_TARGETS_TO_BUILD="ARM;X86;AArch64" -DLLVM_USE_LINKER=gold -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly;pstl;bolt;compiler-rt" -DBUILD_SHARED_LIBS=ON -DLLVM_USE_SPLIT_DWARF=ON -DLLVM_ENABLE_RUNTIMES='libcxx;libcxxabi;libunwind' -DCMAKE_SHARED_LINKER_FLAGS='-latomic' -DLLVM_ENABLE_WARNINGS_AS_ERRORS=OFF

方案2:针对bsearch目标单独关闭特定错误

在libc/src/stdlib/CMakeLists.txt中找到bsearch的目标定义代码块,添加编译选项关闭触发错误的规则:

找到原代码:

add_entrypoint_object(
  bsearch
  SRCS
    bsearch.cpp
  HDRS
    bsearch.h
)

修改为:

add_entrypoint_object(
  bsearch
  SRCS
    bsearch.cpp
  HDRS
    bsearch.h
  COMPILE_OPTIONS
    -Wno-error=missing-attributes
    -Wno-error=alias
)

该方式仅针对触发问题的目标处理,不会全局关闭警告转错误规则,更精准。

方案3:针对整个libc模块关闭警告转错误

在libc/CMakeLists.txt中添加如下配置,对整个libc组件生效:

set(LLVM_ENABLE_WARNINGS_AS_ERRORS OFF)

或者仅关闭触发问题的错误类型:

add_compile_options(-Wno-error=missing-attributes -Wno-error=alias)

内容的提问来源于stack exchange,提问作者Ueda Takeyuki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 05:50:00