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

Mac OS X 13.3.1用Bazel编译Homebrew版LibTorch 2.0.1遇链接错误

MacOS下Bazel编译LibTorch示例的两个问题

问题1:Homebrew安装的LibTorch出现符号缺失错误

在Mac OS X 13.3.1系统中,使用Homebrew安装LibTorch 2.0.1版本,通过Bazel 6.2.1构建系统编译最小示例时,运行阶段出现以下错误:

dyld[21509]: symbol not found in flat namespace '_PyBaseObject_Type'

测试命令为:

bazel run -- //examples:hello_torch

最小示例文件结构

  • 根目录:WORKSPACE文件
  • /examples子目录:BUILD文件、hello_torch.cc文件

WORKSPACE内容

new_local_repository(
  name = "libtorch_osx_homebrew",
  path = "/usr/local/Cellar/pytorch/2.0.1",
  build_file_content =
"""
cc_library(
    name = "libtorch",
    srcs = glob(["lib/*.dylib"]),
    hdrs = glob(["include/**/*.h", "include/torch/csrc/api/include/torch/*.h"]),
    includes = ["include", "include/torch/csrc/api/include/"],
    copts=["-O3"],
    visibility = ["//visibility:public"],
    linkstatic = 1,
)
"""
)

BUILD内容

cc_binary(
  name = "hello_torch",
  srcs = ["hello_torch.cc"],
  deps = ["@libtorch_osx_homebrew//:libtorch"],
  copts=["-std=c++17"],
)

hello_torch.cc内容

#include <iostream>
#include "torch/script.h"
#include "torch/torch.h"

int main() {
  torch::DeviceType device = torch::kCPU;
  torch::Tensor tensor = torch::rand({2, 3}).to(device);
  std::cout << tensor << std::endl;
}

排查情况

推测WORKSPACE中LibTorch的定义缺失了包含_PyBaseObject_Type符号的依赖库,但无法确定该依赖的来源。尝试使用lldb、dtruss、otool等工具分析,未找到有效线索。此类错误通常会显示符号的引用来源,但本次无相关输出,导致定位困难。该问题与GitHub Issue #51084类似。


问题2:官方LibTorch包的开发者验证问题

尝试使用官方LibTorch包替代Homebrew版本后,出现新问题:运行时频繁弹出对话框,提示每个dylib“无法打开,因为无法验证开发者”。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 02:58:21