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

Mac M2 Max环境下protobuf@24.2构建C++项目失败求助

问题描述

Mac M2 Max设备上,通过brew安装protobuf@24.2及其依赖abseil@20230802后,使用CMake构建C++项目时出现链接错误,缺失abseil log_internal相关符号。

构建错误日志

Undefined symbols for architecture arm64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::MakeCheckOpString<void const*, void const*>(void const*, void const*, char const*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::Check_NEImpl<haomo::msg::ProtoFile const*, haomo::msg::ProtoFile*>(haomo::msg::ProtoFile const* const&, haomo::msg::ProtoFile* const&, char const*) in proto_file.pb.cc.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::Check_NEImpl<haomo::msg::DataType const*, haomo::msg::DataType*>(haomo::msg::DataType const* const&, haomo::msg::DataType* const&, char const*) in proto_file.pb.cc.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::Check_NEImpl<haomo::msg::ProtoFileDes const*, haomo::msg::ProtoFileDes*>(haomo::msg::ProtoFileDes const* const&, haomo::msg::ProtoFileDes* const&, char const*) in proto_file.pb.cc.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::Check_NEImpl<google::protobuf::internal::RepeatedPtrFieldBase const*, google::protobuf::internal::RepeatedPtrFieldBase*>(google::protobuf::internal::RepeatedPtrFieldBase const* const&, google::protobuf::internal::RepeatedPtrFieldBase* const&, char const*) in proto_file.pb.cc.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::MakeCheckOpString<long long, long long>(long long, long long, char const*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::Check_GEImpl<int, int>(int const&, int const&, char const*) in proto_file.pb.cc.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::Check_GTImpl<int, int>(int const&, int const&, char const*) in proto_file.pb.cc.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::Check_LTImpl<int, int>(int const&, int const&, char const*) in proto_file.pb.cc.o
  "absl::lts_20230802::log_internal::CheckOpMessageBuilder::ForVar2()", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::MakeCheckOpString<unsigned long long, long long>(unsigned long long, long long, char const*) in proto_file.pb.cc.o
  "absl::lts_20230802::log_internal::CheckOpMessageBuilder::NewString()", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::MakeCheckOpString<unsigned long long, long long>(unsigned long long, long long, char const*) in proto_file.pb.cc.o
  "absl::lts_20230802::log_internal::CheckOpMessageBuilder::CheckOpMessageBuilder(char const*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* absl::lts_20230802::log_internal::MakeCheckOpString<unsigned long long, long long>(unsigned long long, long long, char const*) in proto_file.pb.cc.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [qtui.app/Contents/MacOS/qtui] Error 1
make[1]: *** [CMakeFiles/qtui.dir/all] Error 2
make: *** [all] Error 2

当前CMakeLists.txt片段

find_package(absl CONFIG REQUIRED)
find_package(Protobuf REQUIRED)
include_directories(${Protobuf_INCLUDE_DIRS})

message("pb include dir:${Protobuf_INCLUDE_DIRS}")

set(absl_LIBRARIES
    absl::algorithm
    absl::base
    absl::debugging
    absl::flat_hash_map
    absl::flags
    absl::memory
    absl::meta
    absl::numeric
    absl::random_random
    absl::strings
    absl::synchronization
    absl::time
    absl::utility
    absl::log
)
message("absl: ${absl_LIBRARIES}")

CMake运行日志

pb include dir:/opt/homebrew/include
absl: absl::algorithm;absl::base;absl::debugging;absl::flat_hash_map;absl::flags;absl::memory;absl::meta;absl::numeric;absl::random_random;absl::strings;absl::synchronization;absl::time;absl::utility;absl::log

解决方法

1. 补充缺失的abseil组件

错误中缺失的符号属于absl::checking模块,该模块包含abseil CHECK宏的实现代码,当前的absl库列表中未包含它。在absl_LIBRARIES中添加该组件:

set(absl_LIBRARIES
    absl::algorithm
    absl::base
    absl::debugging
    absl::flat_hash_map
    absl::flags
    absl::memory
    absl::meta
    absl::numeric
    absl::random_random
    absl::strings
    absl::synchronization
    absl::time
    absl::utility
    absl::log
    absl::checking  # 新增该行
)

2. 使用现代CMake链接方式(推荐)

避免手动维护库列表,改用CMake目标链接自动处理依赖。假设你的可执行目标是qtui,修改CMakeLists.txt为:

find_package(absl CONFIG REQUIRED)
find_package(Protobuf REQUIRED)

# 替换为你的实际源文件列表
add_executable(qtui
    your_source_files.cpp
    proto_file.pb.cc
)

# 链接Protobuf和abseil组件
target_link_libraries(qtui
    PRIVATE
        Protobuf::libprotobuf
        absl::algorithm
        absl::base
        absl::debugging
        absl::flat_hash_map
        absl::flags
        absl::memory
        absl::meta
        absl::numeric
        absl::random_random
        absl::strings
        absl::synchronization
        absl::time
        absl::utility
        absl::log
        absl::checking
)

# 替换全局include_directories为目标专属包含路径(若生成的pb.h在当前二进制目录,需添加)
target_include_directories(qtui
    PRIVATE
        ${Protobuf_INCLUDE_DIRS}
        ${CMAKE_CURRENT_BINARY_DIR}
)

3. 清理构建缓存并重新构建

旧的CMake缓存可能导致依赖解析错误,执行以下命令清理后重新构建:

rm -rf build
mkdir build && cd build
cmake ..
make

4. 确认abseil版本匹配

运行以下命令确认brew安装的abseil版本与protobuf依赖的版本一致:

brew info abseil@20230802
brew info protobuf@24.2

若存在版本冲突,可卸载多余版本后重新安装:

brew uninstall --force abseil
brew install protobuf@24.2  # 会自动安装对应的abseil依赖

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 07:37:02