我正试图在cpp上用位于/usr/lib64/libfmt.so中的动态库构建一个项目。lib模块构建时没有出现错误,但由于某些原因,main失败并出现错误:
INFO: Analyzed target //main:main (1 packages loaded, 7 targets configured).
INFO: Found 1 target...
ERROR: /home/nim/Prg/Cpp/bazel_learn/examples/cpp-tutorial/my_stage/main/BUILD:1:11: SolibSymlink _solib_k8/_U_S_Smain_Cfmt___Umain/libfmt.so failed: missing input file '//main:libfmt.so'
Target //main:main failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /home/nim/Prg/Cpp/bazel_learn/examples/cpp-tutorial/my_stage/main/BUILD:1:11 SolibSymlink _solib_k8/_U_S_Smain_Cfmt___Umain/libfmt.so failed: 1 input file(s) do not exist
INFO: Elapsed time: 0.248s, Critical Path: 0.04s
INFO: 3 processes: 3 internal.
FAILED: Build did NOT complete successfully
如何正确收集项目?这是BUILD文件。
cc_import (
name = "fmt",
# hdrs = ["include/fmt/core.h"],
shared_library = "libfmt.so",
visibility = ["//visibility:public"],
)
cc_library (
name = "lib",
hdrs = ["lib.hpp"],
srcs = ["lib.cc"],
deps = [":fmt"],
visibility = ["//visibility:public"],
linkstatic = 0,
)
cc_binary (
name = "main",
srcs = ["main.cc"],
deps = [":lib"],
linkopts = ["-lfmt"],
linkstatic = 0,
)
项目结构
├── main
│ ├── BUILD
│ ├── lib.cc
│ ├── lib.hpp
│ └── main.cc
└── WORKSPACE
代码文件:
main.cc
#include "lib.hpp"
int main(int argc, char *argv[]) {
if ( argc > 1) {
lib::hello(argv[1]);
}
else {
lib::hello();
}
return EXIT_SUCCESS;
}
lib.cc
#include "lib.hpp"
#include <fmt/core.h>
namespace lib {
void hello(std::string const& name) {
fmt::print("Hello, {}", name);
}
}
lib.hpp
#pragma once
#include <string>
namespace lib {
void hello(std::string const& name = "World");
}
{fmt}附带Bazel支持-您可以这样使用它:
WORKSPACE.bazel
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "fmt",
branch = "master",
patch_cmds = [
"mv support/bazel/.bazelrc .bazelrc",
"mv support/bazel/.bazelversion .bazelversion",
"mv support/bazel/BUILD.bazel BUILD.bazel",
"mv support/bazel/WORKSPACE.bazel WORKSPACE.bazel",
],
# Windows related patch commands are only needed in the case MSYS2 is not installed
patch_cmds_win = [
"Move-Item -Path support/bazel/.bazelrc -Destination .bazelrc",
"Move-Item -Path support/bazel/.bazelversion -Destination .bazelversion",
"Move-Item -Path support/bazel/BUILD.bazel -Destination BUILD.bazel",
"Move-Item -Path support/bazel/WORKSPACE.bazel -Destination WORKSPACE.bazel",
],
remote = "https://github.com/fmtlib/fmt",
)
BUILD.bazel
cc_binary(
name = "Demo",
srcs = ["main.cpp"],
deps = ["@fmt"],
)
main.cpp
#include "fmt/core.h"
int main() {
fmt::print("The answer is {}.n", 42);
}
另请参见此处。