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

如何修复macOS M1上Bazel编译时的ld链接警告?

解决Bazel在macOS上的链接器警告:-undefined dynamic_lookup may not work with chained fixups

问题描述

执行命令 bazel clean && bazel test --config=macos //... 时,始终收到以下链接器警告:

ld: warning: -undefined dynamic_lookup may not work with chained fixups

Windows(Visual Studio环境)和Linux(GCC/Clang环境)下无此问题。我的环境配置:Bazel 6.0,macOS Ventura 13.1(Apple M1 Pro芯片)。

项目相关文件内容如下:

WORKSPACE.bazel

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "googletest",
    sha256 = "1d5e983023e43a695d3f49ceaed4e0010baaeb4591d88532b779669dc8f6a68e",
    strip_prefix = "googletest-a652ae05755c5d95e9b11ef0c4bc0b715e4332d4",
    urls = [
        "https://github.com/google/googletest/archive/a652ae05755c5d95e9b11ef0c4bc0b715e4332d4.tar.gz",
    ],
)

BUILD.bazel

cc_test(
    name = "tests",
    srcs = ["test.cpp"],
    deps = [
        "@googletest//:gtest_main",
    ],
)

test.cpp

#include <iostream>
using namespace std;

#include "gtest/gtest.h"

TEST(sample_test_case, sample_test) {
    EXPECT_EQ(1, 1);
}

.bazel.rc

build:macos --cxxopt=-std=c++2a
build:macos --cxxopt=-Wall

修复方案

这个警告的根源是:macOS链接器的-undefined dynamic_lookup选项与链式修复(chained fixups)机制存在兼容性冲突,而Bazel默认会给macOS的测试目标添加该链接选项。以下是三种有效解决方式:

方法1:全局配置禁用链式修复

在.bazel.rc的build:macos配置块中添加链接器选项,全局关闭链式修复:

build:macos --cxxopt=-std=c++2a
build:macos --cxxopt=-Wall
build:macos --linkopt=-Wl,-no_chained_fixups

方法2:针对单个测试目标修改

如果不想全局调整,可以直接在BUILD.bazel的cc_test目标中添加linkopts:

cc_test(
    name = "tests",
    srcs = ["test.cpp"],
    deps = [
        "@googletest//:gtest_main",
    ],
    linkopts = ["-Wl,-no_chained_fixups"],
)

方法3:升级Bazel版本(可选)

Bazel 6.x后续版本或7.x已针对该默认行为做了修复,升级到较新版本可能自动消除该警告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 14:10:33