如何在Bazel中配置动态加载库依赖及cc_test传库路径参数
Great question! Let's tackle both parts of your problem step by step.
1. Setting Up Dynamic Library Dependencies in Bazel
By default, Bazel builds cc_library targets as static libraries. To create a dynamically linked library (shared object), you need to add the linkshared = True attribute to your cc_library definition. This tells Bazel to generate a platform-specific dynamic library (.so on Linux, .dylib on macOS, .dll on Windows).
If you have other targets that need to dynamically link against this library (not just load it via dlopen), you can set linkstatic = False on those dependent targets to force dynamic linking instead of static. For your dlopen use case though, you don't need to link the library directly—instead, you'll treat it as a runtime dependency (more on that next).
2. Configuring the cc_test to Pass the Dynamic Library Path
Absolutely! You can set up your cc_test to pass the path of library a as an argument, and Bazel handles platform-specific suffixes automatically. Here's the complete setup:
Step 1: Update the cc_library to Generate a Dynamic Library
First, modify your existing a library to produce a dynamic build:
cc_library( name = "a", srcs = ["a.cpp"], visibility = ["//visibility:public"], linkshared = True, # Critical: Tells Bazel to build a dynamic library )
Step 2: Define the cc_test with Runtime Dependency
Now set up your test target to include the dynamic library as data, pass its path as an argument, and handle platform-specific linking for dlopen:
cc_test( name = "dynamic_load_test", srcs = ["test.cpp"], # Replace with your actual test file name args = [ "$(location :a)", # Auto-expands to the platform-specific library path ], data = [":a"], # Ensures the dynamic library is available at test runtime # Link against libdl on Linux (required for dlopen/dlsym functions) linkopts = select({ "@bazel_tools//src/conditions:linux": ["-ldl"], "@bazel_tools//src/conditions:darwin": [], # macOS doesn't need explicit linking "@bazel_tools//src/conditions:windows": [], # Windows uses LoadLibrary instead }), )
How This Works
linkshared = True: Bazel builds the appropriate dynamic library for your OS (e.g.,liba.soon Linux,a.dllon Windows) instead of a static archive.data = [":a"]: This ensures the dynamic library is copied to the test's execution directory, so it's accessible when the test runs.$(location :a): This Bazel make variable expands to the relative path of the dynamic library from the test's working directory—no need to hardcode suffixes, Bazel handles cross-platform paths automatically.linkopts: Linux requires linking againstlibdlfordlopenand related functions; macOS and Windows don't need this extra step.
Testing It Out
When you run bazel test //:dynamic_load_test, Bazel will:
- Build the dynamic library
a - Compile your test binary
- Execute the test with the dynamic library path pre-filled as the first argument
Your test code can then use argv[1] directly in dlopen without worrying about OS-specific paths or suffixes.
内容的提问来源于stack exchange,提问作者Ryan Burn

