I am trying to use OpenCV in my cpp project with bazel.
This is the structure:
-WORKSPACE
-tutorial2
main.cpp
BUILD.bazel
-thirdparty
-opencv
-installdir
include
lib
I have this main.cpp file:
#include <iostream>
#include "tutorial2/thirdparty/opencv/installdir/include/opencv4/opencv2/core.hpp"
int main() {
std::cout << "Hello Shell" << std::endl;
cv::Mat tmp = cv::Mat::zeros(3,3, CV_32FC1);
std::cout << tmp << std::endl;
return 0;
}
This is the BUILD.bazel file:
cc_library(
name = "opencv_lib",
hdrs = glob(["thirdparty/opencv/installdir/include/opencv4/**/*h*"]),
srcs = glob(["thirdparty/opencv/installdir/lib/**/*.a"])
)
cc_binary(
name = "opencv_bin",
srcs = ["main.cpp"],
deps = [":opencv_lib"],
copts = [
"-Itutorial2/thirdparty/opencv/installdir/include/opencv4",
"-Ltutorial2/thirdparty/opencv/installdir/lib/",
"-Ltutorial2/thirdparty/opencv/installdir/lib/opencv4/3rdparty",
"-L/opt/homebrew/opt/zlib/lib",
"-lz",
],
)
But I get the linking error:
_arm64-fastbuild/bin/tutorial2/opencv_bin-2.params)
ld64.lld: error: undefined symbol: gzopen
>>> referenced by tutorial2/thirdparty/opencv/installdir/lib/libopencv_core.a(persistence.cpp.o):(symbol cv::FileStorage::Impl::open(char const*, int, char const*)+0xd7c)
ld64.lld: error: undefined symbol: gzgets
>>> referenced by tutorial2/thirdparty/opencv/installdir/lib/libopencv_core.a(persistence.cpp.o):(symbol cv::FileStorage::Impl::gets(unsigned long)+0x1ec)
>>> referenced by tutorial2/thirdparty/opencv/installdir/lib/libopencv_core.a(persistence.cpp.o):(symbol cv::FileStorage::Impl::getsFromFile(char*, int)+0x48)
I have tried rules_foreign_cc but I don't think the caching is great and it is not working in Linux. Also I want to learn other ways of adding dependencies with bazel. What is the best strategy to add pre-built opencv to a c++ bazel project?
This is on my Mac.