如何使用Bazel包含已构建的静态库(.a文件)?

时间:2018-10-03 21:21:05

标签: bazel

我正在尝试使用Bazel包含一个小型静态库,但是无论尝试哪种方法,我似乎都遇到错误。

静态库是由我构建的,非常简单。在另一个.cc文件中,我只是从该库中调用这三个函数。虽然我可以使用Makefile生成可执行文件,但无法通过bazel获得相同的输出。

我的构建文件:

cc_library(
name="mylib",
srcs = ["lib/mylib.a"],
)

cc_binary(
name="main",
srcs = ["main.cc", "include/add.h", "include/answer.h", "lib/mylib.a"],
copts = [                                                                                                                                                                                                                                
    "-Iinclude/",
    "-Llib/",
    ],
linkstatic=1,
)

cc_binary(
name = "run_main",
srcs = ["lib/mylib.a"],
copts = [
    "-I.",                                                                                                                                                                                                   
    "-Llib/",
    ],
deps = [":main_lib", ":mylib"],
)

cc_library(
name = "main_lib",
srcs = ["main.cc"],
hdrs = ["include/add.h",
     "include/answer.h"
     ],
copts = [
    "-Iinclude/",  
    ],
linkstatic=1,
)

两个cc_binary目标均失败,如下所示:

  [andrei@dell static_test_bazel]$ bazel build --subcommands --sandbox_debug --verbose_failures   //:main
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
INFO: Analysed target //:main (7 packages loaded).
INFO: Found 1 target...
SUBCOMMAND: # //:main [action 'Compiling main.cc']
(cd /home/andrei/.cache/bazel/_bazel_andrei/870f0aacf9816a8089f2d66bef6251e2/execroot/__main__ && \
  exec env - \
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/opt/android-sdk/platform-tools:/opt/android-sdk/tools:/opt/android-sdk/tools/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin:/opt/android-sdk/platform-tools \
    PWD=/proc/self/cwd \
  /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF bazel-out/k8-fastbuild/bin/_objs/main/main.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/_objs/main/main.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/genfiles -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/genfiles/external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -Iinclude/ -Llib/ -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c main.cc -o bazel-out/k8-fastbuild/bin/_objs/main/main.pic.o)
SUBCOMMAND: # //:main [action 'Linking main']
(cd /home/andrei/.cache/bazel/_bazel_andrei/870f0aacf9816a8089f2d66bef6251e2/execroot/__main__ && \
  exec env - \
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/opt/android-sdk/platform-tools:/opt/android-sdk/tools:/opt/android-sdk/tools/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin:/opt/android-sdk/platform-tools \
    PWD=/proc/self/cwd \
  /usr/bin/gcc -o bazel-out/k8-fastbuild/bin/main '-fuse-ld=gold' -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/usr/bin -B/usr/bin -pass-exit-codes -Wl,-S -Wl,@bazel-out/k8-fastbuild/bin/main-2.params)
ERROR: /home/andrei/embedded-nn/static_test_bazel/BUILD:8:1: Linking of rule '//:main' failed (Exit 1): process-wrapper failed: error executing command 
  (cd /home/andrei/.cache/bazel/_bazel_andrei/870f0aacf9816a8089f2d66bef6251e2/execroot/__main__ && \
  exec env - \
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/opt/android-sdk/platform-tools:/opt/android-sdk/tools:/opt/android-sdk/tools/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin:/opt/android-sdk/platform-tools \
    PWD=/proc/self/cwd \
    TMPDIR=/tmp \
  /home/andrei/.cache/bazel/_bazel_andrei/870f0aacf9816a8089f2d66bef6251e2/execroot/__main__/_bin/process-wrapper '--timeout=0' '--kill_delay=15' /usr/bin/gcc -o bazel-out/k8-fastbuild/bin/main '-fuse-ld=gold' -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/usr/bin -B/usr/bin -pass-exit-codes -Wl,-S -Wl,@bazel-out/k8-fastbuild/bin/main-2.params)
bazel-out/k8-fastbuild/bin/_objs/main/main.pic.o:main.cc:function main: error: undefined reference to 'setSummand(int)'
bazel-out/k8-fastbuild/bin/_objs/main/main.pic.o:main.cc:function main: error: undefined reference to 'add(int)'
bazel-out/k8-fastbuild/bin/_objs/main/main.pic.o:main.cc:function main: error: undefined reference to 'answer()'
collect2: error: ld returned 1 exit status
Target //:main failed to build
INFO: Elapsed time: 3.026s, Critical Path: 0.08s
INFO: 1 process: 1 processwrapper-sandbox.
FAILED: Build did NOT complete successfully

项目的结构:

WORKSPACE
BUILD
main.cc
lib/
--- mylib.a
include/
--- add.h
--- answer.h

我已关注Bazel failed to include a external static library .a中的讨论,但不确定自己是否遇到相同的问题。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

cc_library(
  name = "mylib",
  srcs = ["lib/mylib.a"],
  hdrs = ["mylib.h"]
)

cc_binary(
  name = "main",
  srcs = ["main.cc"],
  hrds = ["include/add.h", "include/answer.h"]
  deps = [":mylib"]
  copts = [
    "-Iinclude/",
    "-Llib/",
  ],
  linkstatic=1,
)

注意:您必须在mylib规则中具体化头文件,以便它们可以包含在main.cc