Thien Pham
Thien Pham

Reputation: 23

Build Bazel with openCV, error: Undefined symbols for architecture x86_64

I got this error when build iOS app with openCV:

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

In the file wrapper.cpp , I only have this source code:

#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";
}

wrapper.hpp

#include <stdio.h>
class Wrapper
{
    public:
    void testEmptyFunc();
};

I include from the viewcontroller like this:

#import "wrapper.hpp"

Call the function:

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

My WORKSPACE is:

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", )

Please help me to solve this issue.

Upvotes: 2

Views: 814

Answers (1)

Vertexwahn
Vertexwahn

Reputation: 8172

This is how OpenCV works for me under 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://stackoverflow.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",
    ],
)

If you change the download URL to opencv-4.5.3-ios-framework.zip it might work

Upvotes: 0

Related Questions