使用 Bazel 构建 OpenCV 代码 [英] Building OpenCV code using Bazel
问题描述
使用 Bazel 构建使用 OpenCV 库的 C++ 代码的最佳方法是什么?即,BUILD 规则是什么样的?
What is the best way to build C++ code that uses the OpenCV library using Bazel? I.e., what would the BUILD rules look like?
Bazel.io 有 外部依赖的文档,但不是很清楚.
Bazel.io has docs for external dependencies but it's not very clear.
推荐答案
有几个选项.最简单的方法可能是按照 OpenCV 站点推荐的方式在本地安装:
There are a couple of options. The easiest way is probably to install locally in the way the OpenCV site recommends:
git clone https://github.com/Itseez/opencv.git
cd opencv/
mkdir build install
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/path/to/opencv/install ..
make install
然后将以下内容添加到您的 WORKSPACE 文件中:
Then add the following to your WORKSPACE file:
new_local_repository(
name = "opencv",
path = "/path/to/opencv/install",
build_file = "opencv.BUILD",
)
在与 WORKSPACE 相同的目录中使用以下内容创建 opencv.BUILD
:
Create opencv.BUILD
in the same directory as WORKSPACE with the following:
cc_library(
name = "opencv",
srcs = glob(["lib/*.so*"]),
hdrs = glob(["include/**/*.hpp"]),
includes = ["include"],
visibility = ["//visibility:public"],
linkstatic = 1,
)
那么你的代码就可以依赖@opencv//:opencv
来链接lib/下的.so,并引用include/下的头文件.
Then your code can depend on @opencv//:opencv
to link in the .so's under lib/ and reference the headers under include/.
然而,这不是很便携.如果您想要一个可移植的解决方案(并且您感觉雄心勃勃),您可以将 OpenCV git repo 添加到您的工作区并下载 &建立它.类似的东西:
However, this isn't very portable. If you want a portable solution (and you're feeling ambitious), you could add the OpenCV git repo to your workspace and download & build it. Something like:
# WORKSPACE
new_git_repository(
name = "opencv",
remote = "https://github.com/Itseez/opencv.git",
build_file = "opencv.BUILD",
tag = "3.1.0",
)
并使 opencv.BUILD 类似于:
And make opencv.BUILD something like:
cc_library(
name = "core",
visibility = ["//visibility:public"],
srcs = glob(["modules/core/src/**/*.cpp"]),
hdrs = glob([
"modules/core/src/**/*.hpp",
"modules/core/include/**/*.hpp"]
) + [":module-includes"],
)
genrule(
name = "module-includes",
cmd = "echo '#define HAVE_OPENCV_CORE' > $@",
outs = ["opencv2/opencv_modules.hpp"],
)
...
那么你的代码可以依赖于更具体的目标,例如,@opencv//:core
.
Then your code could depend on more specific targets, e.g., @opencv//:core
.
作为第三种选择,您在 WORKSPACE 文件中同时声明 cmake 和 OpenCV,并使用 genrule 从 Bazel 中在 OpenCV 上运行 cmake.
As a third option, you declare both cmake and OpenCV in your WORKSPACE file and use a genrule to run cmake on OpenCV from within Bazel.
这篇关于使用 Bazel 构建 OpenCV 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!