【问题标题】:How to link with libm.so and libdl.so with my bazel project?如何将 libm.so 和 libdl.so 与我的 bazel 项目链接?
【发布时间】:2019-02-13 16:46:15
【问题描述】:

我正在尝试在我的 c 程序中使用一些 cos/sin 函数和 dlopen/sym 函数,并将其放在我的“BUILD”文件中:

cc_binary(
    name = "hello-dl",
    srcs = ["hello-dl.cc"],
    deps = ["dl"],
)

我的源文件是:

#include<stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(){
    void * handle = dlopen("hello-greet", RTLD_LAZY);
    if (!handle) {
        fprintf(stderr, "%s\n", dlerror());
        exit(EXIT_FAILURE);
    }
    void (*f)() = (void (*)()) dlsym(handle, "f");
    char* error = dlerror();
    if (error != NULL) {
        fprintf(stderr, "%s\n", error);
        exit(EXIT_FAILURE);
    }

    f();
    dlclose(handle);
    exit(EXIT_SUCCESS);
    return 0;
}

我的图书馆是:

# cat hello-greet.cc
#include<stdio.h>
void f(){
    printf("f function\n");
}

当我运行 bazel 时,它给出了一些错误:

bazel build hello-dl
INFO: Invocation ID: 89be69f9-5b70-4318-8b6a-43ad6feb341c
ERROR: /root/mynet/mytest/build/useBazel/cpp-project/BUILD:9:12: in deps attribute of cc_binary rule //:hello-dl: target '//:dl' does not exist
ERROR: Analysis of target '//:hello-dl' failed; build aborted: Analysis of target '//:hello-dl' failed; build aborted
INFO: Elapsed time: 0.115s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

那么我该如何解决这个问题?非常感谢。

【问题讨论】:

    标签: c++ dependencies bazel


    【解决方案1】:

    linkopts = ['-ldl', '-lm'] 放入cc_binary 规则中。

    【讨论】:

    • 它有什么作用?
    • 指示链接器查找 libmlibdl 并将它们与输出可执行文件链接。
    猜你喜欢
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多