如何在我的bazel项目中与libm.so和libdl.so链接?

时间:2019-02-13 03:05:53

标签: c++ dependencies bazel

我试图在我的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)

那我该如何解决呢?非常感谢。

1 个答案:

答案 0 :(得分:2)

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