我将-lrt
作为编译器的最后一个链接器标志。但仍然遇到这个错误。
arif@khost:~/sak/sak.exosip$ gcc eXo_init.c -I/opt/osip2/include -I/opt/exosip/include -L/opt/osip2/lib -L/opt/exosip/lib -leXosip2 -losipparser2 -losip2 -lrt
/opt/osip2/lib/libosip2.so: undefined reference to `clock_gettime'
collect2: ld returned 1 exit status
手册页说:
NAME
clock_getres, clock_gettime, clock_settime - clock and time functions
SYNOPSIS
#include <time.h>
int clock_getres(clockid_t clk_id, struct timespec *res);
int clock_gettime(clockid_t clk_id, struct timespec *tp);
int clock_settime(clockid_t clk_id, const struct timespec *tp);
Link with -lrt.
所以我有点困惑,我做错了。
我试图在librt.so
中读取符号但没有运气:
arif@khost:~/sak/ortp/src/tests$ nm /lib/x86_64-linux-gnu/librt-2.15.so
nm: /lib/x86_64-linux-gnu/librt-2.15.so: no symbols
更新1 我无法读取librt.so
中的符号的原因是它们被“剥离”了。我在哪里可以得到符号名称?
arif@khost:~/sak/ortp/src/tests$ file /lib/x86_64-linux-gnu/librt-2.15.so
/lib/x86_64-linux-gnu/librt-2.15.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), BuildID[sha1]=0x375b2c35c4e6503a5d1a88ab6f76f5b6e0ee81df, for GNU/Linux 2.6.24, stripped
更新2
事情变得非常混乱,因为以下测试代码编译并运行得很好:
#include <time.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv, char **arge) {
struct timespec tps, tpe;
if ((clock_gettime(CLOCK_REALTIME, &tps) != 0)
|| (clock_gettime(CLOCK_REALTIME, &tpe) != 0)) {
perror("clock_gettime");
return -1;
}
printf("%lu s, %lu ns\n", tpe.tv_sec-tps.tv_sec,tpe.tv_nsec-tps.tv_nsec);
return 0;
}
用
构建arif@khost:~/sak/sak.exosip$ gcc what.c -lrt
UPDATE3 我正在尝试编译的代码:
#include <eXosip2/eXosip.h>
#include <netinet/in.h>
#include <unistd.h>
int ex_init(int port)
{
struct eXosip_t *eXcontext;
int i;
TRACE_INITIALIZE(6, stdout);
i = eXosip_init(eXcontext);
if (i != 0)
return -1;
i = eXosip_listen_addr(eXcontext, IPPROTO_UDP, NULL, port, AF_INET, 0);
if (i != 0) {
eXosip_quit(eXcontext);
fprintf (stderr, "could not initialize transport layer\n");
return -1;
}
return 1;
}
int main(int argc, char **argv) {
if(ex_init(1000))
printf("success \n");
return 0;
}
答案 0 :(得分:17)
问题解决了如果我传递此链接器标志
-Wl,--no-as-needed
在命令行中的库列表之前。
为什么会这样,因为在我的平台中,链接器总是与-Wl,--as-needed
一起传递。
来自ld
手册:
--as-needed
--no-as-needed
This option affects ELF DT_NEEDED tags for dynamic libraries
mentioned on the command line after the --as-needed option.
Normally the linker will add a DT_NEEDED tag for each dynamic
library mentioned on the command line, regardless of whether the
library is actually needed or not. --as-needed causes a DT_NEEDED
tag to only be emitted for a library that satisfies an undefined
symbol reference from a regular object file or, if the library is
not found in the DT_NEEDED lists of other libraries linked up to
that point, an undefined symbol reference from another dynamic
library. --no-as-needed restores the default behaviour.
因此,当在库之前给出--as-needed
时,liker只链接到库中NEEDED
部分中给出的库。
例如,
-Wl,--as-needed -llibA -llibB -llibC
此处 - 按需提供libA
之前。因此,在链接期间,链接器将检查NEEDED
的{{1}}部分。如果在libA
的{{1}}部分仅列出NEEDED
,则libA
将不会被链接。
发生此特定问题是因为
libC
libB
未将arif@khost:~/sak/sak.exosip$ objdump -p /opt/osip2/lib/libosip2.so.10 | grep NEEDED
NEEDED libosipparser2.so.10
NEEDED libc.so.6
列为libosip2
。
如果我通过librt
,那么无论ELF NEEDED
部分中提供了什么,所有图书馆都会被链接。
虽然不应该这样,因为,
--no-as-needed
它有未定义的符号NEEDED
,由arif@khost:~/sak/sak.exosip$ nm --demangle /opt/osip2/lib/libosip2.so.10 | grep clock_gettime
U clock_gettime
提供。
实际上clock_gettime
开发人员librt.so
与libosip2
无法合作的错误实际上是错误的。
osip使用的链接命令:
autotools
所以它没有与--as-needed
关联,这就是为什么它没有在libtool: link: gcc -shared -fPIC -DPIC .libs/ict_fsm.o .libs/ist_fsm.o .libs/nict_fsm.o .libs/nist_fsm.o .libs/ict.o .libs/ist.o .libs/nict.o .libs/nist.o .libs/fsm_misc.o .libs/osip.o .libs/osip_transaction.o .libs/osip_event.o .libs/port_fifo.o .libs/osip_dialog.o .libs/osip_time.o .libs/port_sema.o .libs/port_thread.o .libs/port_condv.o -Wl,-rpath -Wl,/home/arif/sak/osip/src/osipparser2/.libs -Wl,-rpath -Wl,/opt/osip2-test/lib -lnsl ../osipparser2/.libs/libosipparser2.so -Wl,-soname -Wl,libosip2.so.10 -o .libs/libosip2.so.10.0.0
列表中列出librt
如果配置为:
librt
然后链接命令变为:
NEEDED
所以它与 LDFLAGS="${LDFLAGS} -lrt" ./configure --prefix=/opt/osip2-test/
的链接。它也反映在它的ELF中:
libtool: link: gcc -shared -fPIC -DPIC .libs/ict_fsm.o .libs/ist_fsm.o .libs/nict_fsm.o .libs/nist_fsm.o .libs/ict.o .libs/ist.o .libs/nict.o .libs/nist.o .libs/fsm_misc.o .libs/osip.o .libs/osip_transaction.o .libs/osip_event.o .libs/port_fifo.o .libs/osip_dialog.o .libs/osip_time.o .libs/port_sema.o .libs/port_thread.o .libs/port_condv.o -Wl,-rpath -Wl,/home/arif/sak/osip/src/osipparser2/.libs -Wl,-rpath -Wl,/opt/osip2-test/lib -lnsl ../osipparser2/.libs/libosipparser2.so -lrt -Wl,-soname -Wl,libosip2.so.10 -o .libs/libosip2.so.10.0.0
此补丁解决了这个问题:
librt
相关的usenet讨论主题: https://groups.google.com/forum/#!topic/comp.unix.programmer/VKbARy6W4AY
更新:
osip开发者回应了我的邮件。他用一个不同的补丁修复它(更普遍的解决方案,然后我的) http://git.savannah.gnu.org/cgit/osip.git/commit/?id=bd5b1ad58381e4bfce08bad9b66ad00cd28f9b65