15

I find some libraries are linked to librt.so, while others are not. I am wondering in what circumstances it is linked.

I have never used this library directly. Is there any demo showing the direct usage of it? Is there a header file related to it?

jinge
  • 785
  • 1
  • 7
  • 20

2 Answers2

8

The functions are for Real Time Solaris (Real Time means guaranteed response in a time boundary), which has since been integrated into mainline Solaris and is part of libc. Librt is present only for historical, backward compatibility reasons and you do not need to reference it.

Dan Anderson
  • 2,265
  • 1
  • 9
  • 20
2
$ nm /usr/lib32/librt.a | grep ' T ' | grep -v ' __'
00000000 T aio_cancel
00000000 T aio_error
00000000 T aio_fsync
00000000 T aio_read
00000000 T aio_read64
00000000 T aio_return
00000130 T aio_suspend
00000000 T aio_write
00000000 T aio_write64
00000000 T timer_create
00000000 T timer_delete
00000000 T timer_getoverrun
00000000 T timer_gettime
00000000 T timer_settime
00000000 T shm_open
00000000 T shm_unlink
00000000 T mq_open
00000000 T mq_close
00000000 T mq_unlink
00000000 T mq_getattr
00000000 T mq_setattr
00000310 T mq_notify
00000000 T mq_send
00000000 T mq_receive

And indeed if we look e.g. at man shm_open, we see

Link with -lrt.

so the list of functions provided seems to be correct.

There is also this page from Solaris https://docs.oracle.com/cd/E86824_01/html/E54772/librt-3lib.html which explains that the librt library reexports some symbols actually implemented in libc and is there for historical reasons as it was specified in some versions of the Posix standard.

  • There is no `/usr/lib32/librt.a` in my computer. Only a `/lib/x86_64-linux-gnu/librt.so.1` exists. How can I get the symbol list for the shared library? – jinge May 15 '19 at 04:12
  • If I invoke `shm_open` function, what header file should I include? – jinge May 15 '19 at 04:14
  • @jinge for headers see https://linux.die.net/man/3/shm_open, librt.so exports no symbols, but objdump -T shows a very similar list to my answer, so possibly this explanation from Solaris applies: https://docs.oracle.com/cd/E86824_01/html/E54772/librt-3lib.html (librt reexports some libc symbols for historical reasons) –  May 15 '19 at 08:51
  • I have checked https://linux.die.net/man/3/shm_open, it shows the headers `shm_open` needed. But I still don't know which header include `shm_open` function. – jinge May 16 '19 at 13:36
  • @jinge how would you go about it if you wanted to find out yourself? The man page suggests three candidates. Where would you look for the underlying files? Or perhaps you could try to make a source file that uses the function and only includes one of them, see which one makes it compile? –  May 16 '19 at 13:45
  • I have looked into `sys/mman.h`, `sys/stat.h` and `fcntl.h` on my computer. None of them include the declaration of `shm_open`. I doubt there is no such header file on my computer. – jinge May 17 '19 at 02:15
  • @jinge Try `nm -D /lib/x86_64-linux-gnu/librt.so.1`. Many of the builtin libraries use dynamic symbols, which are normally stripped by `nm` unless you add the `-D` option. – nonrectangular Jun 04 '21 at 07:16