我正在尝试构建动态库,然后构建可执行文件,然后使用 Bazel 将它们链接在一起。该工具链是自定义的,并在 Windows 11 Pro 下使用 MinGW ucrt64 GCC 工具。
我的
BUILD
文件:
DLL_HDRS = ["math_import_defs.h", "math_dll_interface.h"]
load("//main:dll_library.bzl", "dll_library")
cc_binary(
name = "sum_numbers_mingw",
srcs = ["main.cpp"] + DLL_HDRS,
dynamic_deps = [":math_d_shared"]
)
dll_library(
name = "math_d",
srcs = ["math_dll_interface.cpp"],
hdrs = DLL_HDRS,
deps = [":math"],
defines = ["MATH_DLL"]
)
cc_library(
name = "math",
srcs = ["math.cpp"],
hdrs = ["math.h"],
copts = ["-std=c++17"]
)
具有 .bzl
定义的
dll_library
文件:
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_cc//examples:experimental_cc_shared_library.bzl", "cc_shared_library")
def dll_library(
name,
srcs = [],
deps = [],
hdrs = [],
visibility = None,
**kwargs):
static_library_name = name + "_static"
dll_lib_name = name + "_shared"
cc_library(
name = static_library_name,
srcs = srcs,
hdrs = hdrs,
deps = deps,
**kwargs
)
cc_shared_library(
name = dll_lib_name,
deps = [":" + static_library_name]
)
一切顺利,直到 ld 收到 -lmath_d_shared 选项:
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmath_d_shared: No such file or directory
这是因为
cc_shared_library
生成 libmath_d_shared.so
库,但 ld
在 Windows 下不查找 .so
。将库重命名为 libmath_d_shared.dll
并再次运行构建而不删除以前生成的目标可以解决问题。 ld
找到 libmath_d_shared.dll
并且一切都按预期进行。
尝试通过额外调整我的工具链:
artifact_name_pattern(
category_name = "dynamic_library",
prefix = "lib",
extension = ".dll",
)
在
artifact_name_patterns
字段中。
它强制 Bazel 将扩展名
.dll
添加到所有生成的动态库中。但是,由于某种原因,Bazel 在将 .dll
选项传递给 -l
时不会从库名称中删除后缀 ld
。所以它通过了 -lmath_d_shared.dll
选项。然后 ld
找不到该库,因为文件 libmath_d_shared.dll.dll
不存在。
正在寻找此案例的解决方法。我需要在将
-l
选项传递给 ld
之前修复库名称剥离过程,或者在与可执行文件链接之前自动将我的库从 .so
重命名为 .dll
。
感谢任何帮助。谢谢。