如何给bazel项目添加外部动态链接库依赖
我一直在尝试将基本的 .dll 链接到 bazel 项目,但遇到了一些链接器问题。我的 .dll 非常简单,它是 Microsoft 提供的示例:
#pragma once
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
// The Fibonacci recurrence relation describes a sequence F
// where F(n) is { n = 0, a
// { n = 1, b
// { n > 1, F(n-2) + F(n-1)
// for some initial integral values a and b.
// If the sequence is initialized F(0) = 1, F(1) = 1,
// then this relation produces the well-known Fibonacci
// sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
// Initialize a Fibonacci relation sequence
// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
extern "C" MATHLIBRARY_API void fibonacci_init(
const unsigned long long a, const unsigned long long b);
// Produce the next value in the sequence.
// Returns true on success and updates current value and index;
// false on overflow, leaves current value and index unchanged.
extern "C" MATHLIBRARY_API bool fibonacci_next();
// Get the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();
// Get the position of the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned fibonacci_index();
我的 BUILD 文件如下所示:
load("@rules_cc//cc:defs.bzl", "cc_binary")
#cc_import(
# name = "MathLibrary",
# hdrs = ["MathLibrary.h"],
# shared_library = "libs/MathLibrary.dll",
# interface_library = "libs/MathLibrary.lib",
#)
cc_library(
name = "MathLibrary",
srcs = ["libs/MathLibrary.dll"],
hdrs = ["MathLibrary.h"],
)
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [":MathLibrary"],
)
hello-world.cc 包含以下内容:
fibonacci_init(1, 1);
// Write out the sequence values until overflow.
do {
std::cout << fibonacci_index() << ": "
<< fibonacci_current() << std::endl;
} while (fibonacci_next());
// Report count of values written before overflow.
std::cout << fibonacci_index() + 1 <<
" Fibonacci sequence values fit in an " <<
"unsigned 64-bit integer." << std::endl;
我一直在尝试使用 cc_import 和 cc_library,但都不起作用。对于 cc_library 我收到以下错误: bazel-out\x64_windows-fastbuild\bin\main\hello-world.exe : fatal error LNK1120: 4 unresolved externals
对于 cc_import 我可以成功构建,但当我尝试运行时,什么也没有发生。
I've been trying to link a basic .dll to bazel project and I've been into some linker issues. The .dll I have is pretty simple, it's the example Microsoft provides:
#pragma once
#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif
// The Fibonacci recurrence relation describes a sequence F
// where F(n) is { n = 0, a
// { n = 1, b
// { n > 1, F(n-2) + F(n-1)
// for some initial integral values a and b.
// If the sequence is initialized F(0) = 1, F(1) = 1,
// then this relation produces the well-known Fibonacci
// sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
// Initialize a Fibonacci relation sequence
// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
extern "C" MATHLIBRARY_API void fibonacci_init(
const unsigned long long a, const unsigned long long b);
// Produce the next value in the sequence.
// Returns true on success and updates current value and index;
// false on overflow, leaves current value and index unchanged.
extern "C" MATHLIBRARY_API bool fibonacci_next();
// Get the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();
// Get the position of the current value in the sequence.
extern "C" MATHLIBRARY_API unsigned fibonacci_index();
And my BUILD file looks like:
load("@rules_cc//cc:defs.bzl", "cc_binary")
#cc_import(
# name = "MathLibrary",
# hdrs = ["MathLibrary.h"],
# shared_library = "libs/MathLibrary.dll",
# interface_library = "libs/MathLibrary.lib",
#)
cc_library(
name = "MathLibrary",
srcs = ["libs/MathLibrary.dll"],
hdrs = ["MathLibrary.h"],
)
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [":MathLibrary"],
)
The hello-world.cc contains the following:
fibonacci_init(1, 1);
// Write out the sequence values until overflow.
do {
std::cout << fibonacci_index() << ": "
<< fibonacci_current() << std::endl;
} while (fibonacci_next());
// Report count of values written before overflow.
std::cout << fibonacci_index() + 1 <<
" Fibonacci sequence values fit in an " <<
"unsigned 64-bit integer." << std::endl;
I've been trying to use both cc_import and cc_library but neither work. For cc_library I get the following error:bazel-out\x64_windows-fastbuild\bin\main\hello-world.exe : fatal error LNK1120: 4 unresolved externals
And for cc_import I can build successfully but when I try to run, nothing happens.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论