简体   繁体   中英

bazel c++ create and link with shared library

I will base my question on stage 2 from the bazel tutorial for c++ .

Normally this example will create hello-world linked statically with libhello-greet.a . However I would like to create hello-world linked dynamically with libhello-greet.so .

Therefore I found some kind of workaround by using this BUILD file:

cc_binary(
    name = "libhello-greet.so",
    srcs = ["hello-greet.cc", "hello-greet.h"],
    linkshared = 1,
)

cc_import(
    name = "libhello-greet",
    shared_library = "libhello-greet.so",
    hdrs = ["hello-greet.h"],
)   

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [
        ":libhello-greet",
    ],
)

but this doesn't feel like the best solution. Is there a better way to create and link with a shared library?

If you specify the linkstatic -flag in the binary, it will link all libraries either as static or as shared libraries. But I do not know how to link only certain libraries as shared libraries.

cc_library(
    name = "hello-greet",
    srcs = ["hello_greet.cc"],
    hdrs = ["hello_greet.h"],
)

cc_binary(
    name = "hello-world",
    srcs = ["main.cc"],
    deps = [
        ":hello-greet",
    ],
    linkstatic=False,
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM