我目前正在尝试在将rxcpp包含到我的主项目之前编译一个普通的c++示例项目,但是,在找不到rxcpp库的过程中遇到了很多困难。
所有文件都在同一目录中。
工作站规格:

  • Bazel版本:0.11.1-自制
  • MacOs: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
    

    最佳答案

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

    #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/"],
    )
    

    关于c++ - 将RxCpp添加到C++ Bazel项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49396689/

    10-11 17:36