使用Bazel构建OpenCV代码 [英] Building OpenCV code using Bazel

查看:701
本文介绍了使用Bazel构建OpenCV代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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,并使用了一种规则从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆