将 RxCpp 添加到 C++ Bazel 项目

Posted

技术标签:

【中文标题】将 RxCpp 添加到 C++ Bazel 项目【英文标题】:Add RxCpp to C++ Bazel project 【发布时间】:2018-03-21 01:21:09 【问题描述】:

我目前正在尝试编译一个简单的 c++ 示例项目,然后将 rxcpp 包含到我的主项目中,但是,我遇到了找不到 rxcpp 库的困难。

所有文件都在同一个目录中。

工作站规格:

Bazel 版本:0.11.1-homebrew MacO:10.13.3 Clang:Apple LLVM 版本 9.0.0 (clang-900.0.39.2) ;目标:x86_64-apple-darwin17.4.0

main.cc

#include <iostream>

#include "rxcpp/rx.hpp"

int main()

    std::cout << "Hello RxCpp!";

Bazel 工作区文件的写法

# WORKSPACE
new_http_archive(
    name="RxCpp",
    url="https://github.com/Reactive-Extensions/RxCpp/archive/v4.0.0.zip",
    build_file="BUILD.rxcpp",
    strip_prefix = "RxCpp-4.0.0"
)

BUILD.rxcpp 的写法

# BUILD.rxcpp
cc_library(
    name="rxcpp",
    hdrs=glob(["Rx/v2/src/rxcpp/*.hpp",
               "Rx/v2/src/rxcpp/**/*.hpp"]),
    visibility=["//visibility:public"]
)

而 Bazel 项目 BUILD 文件写为

# BUILD
cc_binary(
    name="some_binary_name",
    srcs=["main.cc"],
    deps=["@RxCpp//:rxcpp"]
)

跑步

bazel build -c dbg --cxxopt='-std=c++14' //:some_binary_name --verbose_failures --sandbox_debug 

返回:

INFO: Analysed target //:some_binary_name (1 packages loaded).
INFO: Found 1 target...
ERROR: /Users/ddowney/workspace/cpp/rxcpp_test_build/BUILD:2:1: C++ compilation of rule '//:some_binary_name' failed (Exit 1): sandbox-exec failed: error executing command
  (cd /private/var/tmp/_bazel_ddowney/234783b10f6b175e3bb988e899ac1d0e/execroot/__main__ && \
  exec env - \
    APPLE_SDK_PLATFORM='' \
    APPLE_SDK_VERSION_OVERRIDE='' \
    DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer \
    PATH=/Users/ddowney/anaconda3/bin:/Users/ddowney/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:/usr/local/share/dotnet:/opt/X11/bin:/Users/ddowney/anaconda3/bin:/Users/ddowney/.cargo/bin:/Users/ddowney/Library/android/sdk/platform-tools/:/Users/ddowney/Library/Android/sdk/platform-tools/ \
    SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk \
    TMPDIR=/var/folders/sj/dylcfjks0hbc43fk8ylf71fw0000gn/T/ \
    XCODE_VERSION_OVERRIDE=9.2.0 \
  /usr/bin/sandbox-exec -f /private/var/tmp/_bazel_ddowney/234783b10f6b175e3bb988e899ac1d0e/bazel-sandbox/6190111679734456077/sandbox.sb /private/var/tmp/_bazel_ddowney/234783b10f6b175e3bb988e899ac1d0e/execroot/__main__/_bin/process-wrapper '--timeout=0' '--kill_delay=15' external/local_config_cc/wrapped_clang '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -g '-std=c++11' '-std=c++14' -iquote . -iquote bazel-out/darwin-dbg/genfiles -iquote external/RxCpp -iquote bazel-out/darwin-dbg/genfiles/external/RxCpp -iquote external/bazel_tools -iquote bazel-out/darwin-dbg/genfiles/external/bazel_tools -isystem external/bazel_tools/tools/cpp/gcc3 -MD -MF bazel-out/darwin-dbg/bin/_objs/some_binary_name/main.d '-frandom-seed=bazel-out/darwin-dbg/bin/_objs/some_binary_name/main.o' '-isysroot __BAZEL_XCODE_SDKROOT__' -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c main.cc -o bazel-out/darwin-dbg/bin/_objs/some_binary_name/main.o)
main.cc:1:10: fatal error: 'rx/rx.hpp' file not found
#include "rx/rx.hpp"
         ^~~~~~~~~~~
1 error generated.
Target //:some_binary_name failed to build
INFO: Elapsed time: 2.123s, Critical Path: 1.84s
FAILED: Build did NOT complete successfully

我希望在当前目录下的 bazel-out/darwin-dbg/bin/external/RxCpp 下找到 rxcpp 库,但事实并非如此。

[编辑]

构建目录的输出如下:

ls -la /private/var/tmp/_bazel_ddowney/234783b10f6b175e3bb988e899ac1d0e/execroot/__main__/external/RxCpp/Rx/v2/src/rxcpp/
total 616
drwxr-xr-x  26 ddowney  wheel    832 21 Mar 13:51 .
drwxr-xr-x   3 ddowney  wheel     96 21 Mar 13:51 ..
drwxr-xr-x  61 ddowney  wheel   1952 21 Mar 13:51 operators
-rwxr-xr-x   1 ddowney  wheel   6408 22 Mar  2017 rx-connectable_observable.hpp
-rwxr-xr-x   1 ddowney  wheel   9340 22 Mar  2017 rx-coordination.hpp
-rwxr-xr-x   1 ddowney  wheel   5675 22 Mar  2017 rx-coroutine.hpp
-rwxr-xr-x   1 ddowney  wheel   5538 22 Mar  2017 rx-grouped_observable.hpp
-rwxr-xr-x   1 ddowney  wheel   6171 22 Mar  2017 rx-includes.hpp
-rwxr-xr-x   1 ddowney  wheel    251 22 Mar  2017 rx-lite.hpp
-rwxr-xr-x   1 ddowney  wheel   8282 22 Mar  2017 rx-notification.hpp
-rwxr-xr-x   1 ddowney  wheel  63252 22 Mar  2017 rx-observable.hpp
-rwxr-xr-x   1 ddowney  wheel  21989 22 Mar  2017 rx-observer.hpp
-rwxr-xr-x   1 ddowney  wheel  14552 22 Mar  2017 rx-operators.hpp
-rwxr-xr-x   1 ddowney  wheel   9371 22 Mar  2017 rx-predef.hpp
-rwxr-xr-x   1 ddowney  wheel  28270 22 Mar  2017 rx-scheduler.hpp
-rwxr-xr-x   1 ddowney  wheel   1045 22 Mar  2017 rx-sources.hpp
-rwxr-xr-x   1 ddowney  wheel    495 22 Mar  2017 rx-subjects.hpp
-rwxr-xr-x   1 ddowney  wheel  41330 22 Mar  2017 rx-subscriber.hpp
-rwxr-xr-x   1 ddowney  wheel  14088 22 Mar  2017 rx-subscription.hpp
-rwxr-xr-x   1 ddowney  wheel   3551 22 Mar  2017 rx-test.hpp
-rwxr-xr-x   1 ddowney  wheel   3917 22 Mar  2017 rx-trace.hpp
-rwxr-xr-x   1 ddowney  wheel  23700 22 Mar  2017 rx-util.hpp
-rwxr-xr-x   1 ddowney  wheel    232 22 Mar  2017 rx.hpp
drwxr-xr-x  10 ddowney  wheel    320 21 Mar 13:51 schedulers
drwxr-xr-x  12 ddowney  wheel    384 21 Mar 13:51 sources
drwxr-xr-x   6 ddowney  wheel    192 21 Mar 13:51 subjects

【问题讨论】:

【参考方案1】:

默认情况下,Bazel 仅将 -iquote 添加到外部存储库的根目录中。因此main.cc 中的包含路径应​​该是:

#include "Rx/v2/src/rxcpp/rx.hpp"

或者,您可能更喜欢将includes 属性添加到BUILD.rxcpp

cc_library(
    name="rxcpp",
    hdrs=glob(["Rx/v2/src/rxcpp/*.hpp",
               "Rx/v2/src/rxcpp/**/*.hpp"]),
    visibility=["//visibility:public"],
    includes=["Rx/v2/src/"],
)

【讨论】:

以上是关于将 RxCpp 添加到 C++ Bazel 项目的主要内容,如果未能解决你的问题,请参考以下文章

Bazel + C++ 项目:如何为“运行”命令指定工作目录

添加 bazel 角度 lint 规则

一文学会使用Bazel构建C++项目

一文学会使用Bazel构建C++项目

一文学会使用Bazel构建C++项目

一文学会使用Bazel构建C++项目