简体   繁体   English

C静态库无法与librt链接

[英]C static library cannot link with librt

I have to create a static library (it is not an option to create a dynamic one), and I have a function in this static library which uses timer_create from time.h eg something like this: 我必须创建一个静态库(创建动态库不是一种选择),并且我在此静态库中有一个函数,该函数使用来自time.h timer_create ,例如:

somelib.h: somelib.h:

#include <time.h>

int do_something(void);

somelib.c: somelib.c:

int do_something(void){
        timer_t timer;
        struct sigevent sevp;
        sevp.sigev_notify = SIGEV_SIGNAL;
        sevp.sigev_signo = SIGRTMIN;
        sevp.sigev_value.sival_ptr = NULL;
        int ret = timer_create(CLOCK_MONOTONIC, &sevp, &timer);
        timer_delete(timer);
        return 0;
}

The code is actually meaningless and just there to make is necessary to link against librt to illustrate my prolem, which is as follows: 该代码实际上是毫无意义的,仅此而已就需要链接到librt以说明我的问题,如下所示:

After I compile somelib.c: 在我编译somelib.c之后:

gcc -c -o somelib.o somelib.c -lrt

and make the static library: 并制作静态库:

ar rcs somelib.a somelib.o

I get the following error when linking against it: 链接时出现以下错误:

gcc -o someexec someexec.c -lrt ./somelib.a

returns: 返回:

somelib.c:(.text+0x30): undefined reference to `timer_create'
somelib.c:(.text+0x44): undefined reference to `timer_destroy'

This is a minimum example for my problem. 这是解决我的问题的最小示例。 I am not sure though if this can be fixed at all, because my understanding was, that the static lib would have to know the location of librt at the time of its creation and since it is dynamic it is not possible without linking against a static version of librt. 我不确定这是否可以全部解决,因为据我了解,静态库在创建时必须知道librt的位置,并且由于它是动态的,因此如果不链接静态库就不可能版本的librt。 Still I don't use static libraries very often, so I would like to know if there is a way to do something like this. 仍然我不经常使用静态库,所以我想知道是否有一种方法可以做这样的事情。

Compiler version: gcc 4.8.1 No other flags set. 编译器版本:gcc 4.8.1未设置其他标志。

gcc -c -o somelib.o somelib.c -lrt

That is compilation only, the -lrt is irrelevant here. 那只是编译, -lrt在这里无关紧要。

gcc -o someexec someexec.c -lrt ./somelib.a

The order in which objects/libraries are given matters. 对象/库的分配顺序很重要。

Try to push -lrt to the end, then it should work. 尝试将-lrt推到末尾,然后它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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