使用 openCV 构建 Bazel,错误:架构 x86_64 的未定义符号

Posted

技术标签:

【中文标题】使用 openCV 构建 Bazel,错误:架构 x86_64 的未定义符号【英文标题】:Build Bazel with openCV, error: Undefined symbols for architecture x86_64 【发布时间】:2021-10-17 07:06:33 【问题描述】:

使用 openCV 构建 ios 应用时出现此错误:

INFO: Build options --apple_platform_type, --compilation_mode, --cpu, and 3 more have changed, discarding analysis cache.
INFO: Analyzed target //ios-app:ios-app (49 packages loaded, 2428 targets configured).
INFO: Found 1 target...
ERROR: /Volumes/0906411561/My_project/bazel_iOS_sample/examples/tutorial/ios-app/BUILD:36:16: Linking ios-app/ios-app_bin failed: (Aborted): wrapped_clang_pp failed: error executing command external/local_config_cc/wrapped_clang_pp @bazel-out/ios-x86_64-min10.0-applebin_ios-ios_x86_64-fastbuild-ST-7bf874b56ea0/bin/ios-app/ios-app_bin-2.params

Use --sandbox_debug to see verbose messages from the sandbox
Undefined symbols for architecture x86_64:
  "cv::Mat::Mat()", referenced from:
      ___cxx_global_var_init in libwrapper.a(wrapper.o)
      ___cxx_global_var_init.1 in libwrapper.a(wrapper.o)
      ___cxx_global_var_init.2 in libwrapper.a(wrapper.o)
      ___cxx_global_var_init.3 in libwrapper.a(wrapper.o)
  "cv::Mat::~Mat()", referenced from:
      ___cxx_global_var_init in libwrapper.a(wrapper.o)
      ___cxx_global_var_init.1 in libwrapper.a(wrapper.o)
      ___cxx_global_var_init.2 in libwrapper.a(wrapper.o)
      ___cxx_global_var_init.3 in libwrapper.a(wrapper.o)
ld: symbol(s) not found for architecture x86_64
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
Error in child process '/usr/bin/xcrun'. 1
Target //ios-app:ios-app failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 24.334s, Critical Path: 0.83s
INFO: 10 processes: 10 internal.
FAILED: Build did NOT complete successfully

在 wrapper.cpp 文件中,我只有这个源代码:

#include "wrapper.hpp"
#include <iostream>
#include "opencv2.framework/Versions/A/Headers/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

cv::Mat imgGray,imgBlur,imgCanny,imgDil;
void Wrapper::testEmptyFunc()

    std::cout << "testEmptyFunc";

包装器.hpp

#include <stdio.h>
class Wrapper

    public:
    void testEmptyFunc();
;

我像这样从视图控制器中包含:

#import "wrapper.hpp"

调用函数:

Wrapper* wrapperObj = new Wrapper();
wrapperObj->testEmptyFunc();

我的工作空间是:

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

http_archive(
    name = "build_bazel_rules_apple",
    sha256 = "c84962b64d9ae4472adfb01ec2cf1aa73cb2ee8308242add55fa7cc38602d882",
    url = "https://github.com/bazelbuild/rules_apple/releases/download/0.31.2/rules_apple.0.31.2.tar.gz", )

load(
    "@build_bazel_rules_apple//apple:repositories.bzl",
    "apple_rules_dependencies", )

apple_rules_dependencies()

load(
    "@build_bazel_apple_support//lib:repositories.bzl",
    "apple_support_dependencies", )

apple_support_dependencies()

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

http_archive(
    name = "build_bazel_rules_swift",
    sha256 = "f872c0388808c3f8de67e0c6d39b0beac4a65d7e07eff3ced123d0b102046fb6",
    url = "https://github.com/bazelbuild/rules_swift/releases/download/0.23.0/rules_swift.0.23.0.tar.gz", )

load(
    "@build_bazel_rules_swift//swift:repositories.bzl",
    "swift_rules_dependencies", )

swift_rules_dependencies()

load(
    "@build_bazel_rules_swift//swift:extras.bzl",
    "swift_rules_extra_dependencies", )

swift_rules_extra_dependencies()

git_repository(
    name = "build_bazel_apple_support",
    remote = "https://github.com/bazelbuild/apple_support.git",
    tag = "0.11.0", )

git_repository(
    name = "bazel_skylib",
    remote = "https://github.com/bazelbuild/bazel-skylib.git",
    tag = "1.0.3", )

http_archive(
    name = "ios_opencv",
    sha256 = "b85c23953e66f202a5e4b83484f90556ad4ea9df6fcb7934044d5d4decf2898f",
    type = "zip",
    build_file = "@//third_party:opencv_ios.BUILD",
    url = "https://github.com/opencv/opencv/releases/download/4.5.3/opencv-4.5.3-ios-framework.zip", )

# Tesseract

http_archive(    name = "ios_tesseract",    url = "https://github.com/kang298/Tesseract-builds-for-iOS/archive/refs/tags/tesseract-ios-4.1.1.zip", type = "zip",    build_file = "@//third_party:tesseract.BUILD", )

请帮我解决这个问题。

【问题讨论】:

您的 WORSPACE 和 BUILD 文件是什么样的? 我更新了WORKSAPCE的新信息 【参考方案1】:

这就是 OpenCV 在 Windows 10 下为我工作的方式:

WORKSPACE.bazel:

workspace(name = "OpenCVDemo")

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

# OpenCV
http_archive(
    name = "opencv",
    build_file = "//ThirdParty:opencv.BUILD",
    strip_prefix = "opencv/build",
    # Debug
    #url = "http://vertexwahn.de/artifacts/opencv-4.3.0-dldt-2020.2-vc16-avx2-debug.zip",
    #sha256 = "9bcd2dda258e67ad2ddef7768f1c9b2afcc68cd4b9d9f5c9b841ea3ee47f9d4c",
    # Release
    url = "https://github.com/opencv/opencv/releases/download/4.3.0/opencv-4.3.0-dldt-2020.2-vc16-avx2.zip",
)

opencv.BUILD:

package(default_visibility = ["//visibility:public"])

MAIN_MODULES = [
    "core",
    "imgproc",
    "imgcodecs",
    "videoio",
    "highgui",
    "video",
    "calib3d",
    "features2d",
    "objdetect",
    "dnn",
    "ml",
    "flann",
    "photo",
    "stitching",
    "gapi",
]

# https://***.com/questions/56108940/how-to-specify-the-compiler-flags-to-be-used-in-opt-compilation-mode-by-my-own
config_setting(
    name = "fastbuild_mode",
    values = "compilation_mode": "fastbuild",
)

config_setting(
    name = "dbg_mode",
    values = "compilation_mode": "dbg",
)

cc_import(
    name = "tbb",
    shared_library = select(
        ":fastbuild_mode": "bin/tbb.dll",
        ":dbg_mode": "bin/tbb_debug.dll",
        "//conditions:default": "bin/tbb.dll",
    ),
)

[
    (
        cc_import(
            name = module,
            interface_library = select(
                ":fastbuild_mode": "lib/opencv_430.lib".format(module),
                ":dbg_mode": "lib/opencv_430d.lib".format(module),
                "//conditions:default": "lib/opencv_430.lib".format(module),
            ),
            shared_library = select(
                ":fastbuild_mode": "bin/opencv_430.dll".format(module),
                ":dbg_mode": "bin/opencv_430d.dll".format(module),
                "//conditions:default": "bin/opencv_430.dll".format(module),
            ),
        )
    )
    for module in MAIN_MODULES
]

cc_library(
    name = "opencv",
    hdrs = [
        "include/opencv2/calib3d.hpp",
        "include/opencv2/calib3d/calib3d.hpp",
        "include/opencv2/calib3d/calib3d_c.h",
        "include/opencv2/core.hpp",
        "include/opencv2/core/hal/interface.h",
        "include/opencv2/cvconfig.h",
        "include/opencv2/dnn.hpp",
        "include/opencv2/features2d.hpp",
        "include/opencv2/flann.hpp",
        "include/opencv2/flann/config.h",
        "include/opencv2/flann/defines.h",
        "include/opencv2/flann/miniflann.hpp",
        "include/opencv2/highgui.hpp",
        "include/opencv2/highgui/highgui.hpp",
        "include/opencv2/highgui/highgui_c.h",
        "include/opencv2/imgcodecs.hpp",
        "include/opencv2/imgproc.hpp",
        "include/opencv2/ml.hpp",
        "include/opencv2/ml/ml.inl.hpp",
        "include/opencv2/objdetect.hpp",
        "include/opencv2/opencv.hpp",
        "include/opencv2/opencv_modules.hpp",
        "include/opencv2/photo.hpp",
        "include/opencv2/stitching.hpp",
        "include/opencv2/video.hpp",
        "include/opencv2/video/background_segm.hpp",
        "include/opencv2/video/tracking.hpp",
        "include/opencv2/videoio.hpp",
        "include/opencv2/videoio/videoio_c.h",
    ],
    includes = ["include"],
    deps = MAIN_MODULES + [
        "tbb",
    ],
)

如果您将下载 URL 更改为 opencv-4.5.3-ios-framework.zip 它可能会起作用

【讨论】:

谢谢。但它不起作用。问题是当我想在objective-c文件中包含openCV(c++)时,它遇到了链接器问题。如果我在 c++ 文件中包含 openCV,它会起作用。

以上是关于使用 openCV 构建 Bazel,错误:架构 x86_64 的未定义符号的主要内容,如果未能解决你的问题,请参考以下文章

Mediapipe 跟踪 AAR 构建失败:按架构过滤 AAR 本机库失败

Tensorflow 构建量化工具 - bazel 构建错误

nvcc 错误:使用 bazel 构建 tensorflow 时,'cicc' 以状态 0xC0000005 (ACCESS_VIOLATION) 死亡

初探 Bazel

Bazel 构建 Retrainer 失败

在IBM lsf.conf上构建Bazel