官术网_书友最值得收藏!

Looking at the components of the C library

The C library is not a single library file. It is composed of four main parts that together implement the POSIX functions API:

  • libc: The main C library that contains the well-known POSIX functions such as printf, open, close, read, write, and so on
  • libm: Maths functions such as cos, exp, and log
  • libpthread: All the POSIX thread functions with names beginning with pthread_
  • librt: The real-time extensions to POSIX, including shared memory and asynchronous I/O

The first one, libc, is always linked in but the others have to be explicitly linked with the -l option. The parameter to -l is the library name with lib stripped off. So, for example, a program that calculates a sine function by calling sin() would be linked with libm using -lm:

arm-cortex_a8-linux-gnueabihf-gcc myprog.c -o myprog -lm

You can verify which libraries have been linked in this or any other program by using the readelf command:

$ arm-cortex_a8-linux-gnueabihf-readelf -a myprog | grep "Shared library"
0x00000001 (NEEDED) Shared library: [libm.so.6]
0x00000001 (NEEDED) Shared library: [libc.so.6]

Shared libraries need a run-time linker, which you can expose using:

$ arm-cortex_a8-linux-gnueabihf-readelf -a myprog | grep "program interpreter"
 [Requesting program interpreter: /lib/ld-linux-armhf.so.3]

This is so useful that I have a script file with these commands into a shell script:

#!/bin/sh
${CROSS_COMPILE}readelf -a $1 | grep "program interpreter"
${CROSS_COMPILE}readelf -a $1 | grep "Shared library"
主站蜘蛛池模板: 临夏市| 南宁市| 丘北县| 木兰县| 旬邑县| 永州市| 邳州市| 皋兰县| 淮滨县| 梅河口市| 望奎县| 沁水县| 金堂县| 景德镇市| 二连浩特市| 托克托县| 会泽县| 阿克苏市| 无为县| 苍山县| 祁阳县| 晴隆县| 文安县| 南安市| 南投市| 淮阳县| 台江县| 新余市| 永仁县| 宣恩县| 巴林左旗| 博客| 建阳市| 伊通| 隆昌县| 龙游县| 满洲里市| 科技| 六枝特区| 万州区| 枣阳市|