C的头文件和链接库疑问。
C中的不少内置函数,如printf()和scanf(),虽然在<stdio.h>中有声明原型(接口),但是具体的定义(实现)是在链接库文件中么?如果是,具体是哪个库文件呢?如果不是,又是在哪儿呢?请大佬指点一下,谢谢。 顶一下(我也有这个疑问) 但是具体的定义(实现)是在链接库文件中么?是的
printf
scanf
这些函数在 /lib/libc.so.6 这个文件里面
sh-5.1$ nm -D /lib/libc.so.6 | grep 'scanf\|printf'
000000000004ecc0 T __asprintf@@GLIBC_2.2.5
000000000004ecc0 W asprintf@@GLIBC_2.2.5
00000000001174d0 T __asprintf_chk@@GLIBC_2.8
U _dl_fatal_printf@GLIBC_PRIVATE
000000000004ee60 T dprintf@@GLIBC_2.2.5
00000000001175c0 T __dprintf_chk@@GLIBC_2.8
000000000004ef80 T fprintf@@GLIBC_2.2.5
0000000000116050 T __fprintf_chk@@GLIBC_2.3.4
000000000004f040 T fscanf@@GLIBC_2.2.5
00000000000765b0 W fwprintf@@GLIBC_2.2.5
0000000000116fb0 T __fwprintf_chk@@GLIBC_2.4
00000000000768f0 T fwscanf@@GLIBC_2.2.5
000000000004ef80 W _IO_fprintf@@GLIBC_2.2.5
000000000004f770 T _IO_printf@@GLIBC_2.2.5
0000000000055fd0 T _IO_sprintf@@GLIBC_2.2.5
00000000000560a0 T _IO_sscanf@@GLIBC_2.2.5
0000000000056960 T _IO_vfprintf@@GLIBC_2.2.5
0000000000150ce0 T _IO_vfscanf@GLIBC_2.2.5
0000000000075490 T _IO_vsprintf@@GLIBC_2.2.5
000000000004f240 T __isoc99_fscanf@@GLIBC_2.7
00000000000bc1c0 T __isoc99_fwscanf@@GLIBC_2.7
000000000004f300 T __isoc99_scanf@@GLIBC_2.7
000000000004f3d0 T __isoc99_sscanf@@GLIBC_2.7
00000000000bc290 T __isoc99_swscanf@@GLIBC_2.7
000000000004f510 T __isoc99_vfscanf@@GLIBC_2.7
00000000000bc280 T __isoc99_vfwscanf@@GLIBC_2.7
000000000004f520 T __isoc99_vscanf@@GLIBC_2.7
000000000004f550 T __isoc99_vsscanf@@GLIBC_2.7
00000000000bc3d0 T __isoc99_vswscanf@@GLIBC_2.7
00000000000bc190 T __isoc99_vwscanf@@GLIBC_2.7
00000000000bc0c0 T __isoc99_wscanf@@GLIBC_2.7
000000000007be50 W obstack_printf@@GLIBC_2.2.5
00000000001176b0 T __obstack_printf_chk@@GLIBC_2.8
000000000007be40 W obstack_vprintf@@GLIBC_2.2.5
0000000000117780 T __obstack_vprintf_chk@@GLIBC_2.8
000000000004f840 T parse_printf_format@@GLIBC_2.2.5
000000000004f770 T printf@@GLIBC_2.2.5
0000000000115f80 T __printf_chk@@GLIBC_2.3.4
0000000000052a40 T __printf_fp@@GLIBC_2.2.5
0000000000054760 T printf_size@@GLIBC_2.2.5
00000000000551e0 T printf_size_info@@GLIBC_2.2.5
0000000000055c60 W register_printf_function@@GLIBC_2.2.5
0000000000055880 W register_printf_modifier@@GLIBC_2.10
0000000000055b90 W register_printf_specifier@@GLIBC_2.10
0000000000055c70 W register_printf_type@@GLIBC_2.10
0000000000055e40 T scanf@@GLIBC_2.2.5
0000000000055f10 T __snprintf@@GLIBC_PRIVATE
0000000000055f10 W snprintf@@GLIBC_2.2.5
0000000000115e70 T __snprintf_chk@@GLIBC_2.3.4
0000000000055fd0 T sprintf@@GLIBC_2.2.5
0000000000115d60 T __sprintf_chk@@GLIBC_2.3.4
00000000000560a0 T sscanf@@GLIBC_2.2.5
0000000000076670 T swprintf@@GLIBC_2.2.5
0000000000116dd0 T __swprintf_chk@@GLIBC_2.4
0000000000076c10 T swscanf@@GLIBC_2.2.5
000000000007b7b0 W vasprintf@@GLIBC_2.2.5
00000000001175a0 T __vasprintf_chk@@GLIBC_2.8
000000000007b940 W vdprintf@@GLIBC_2.2.5
0000000000117690 T __vdprintf_chk@@GLIBC_2.8
0000000000056960 T vfprintf@@GLIBC_2.2.5
0000000000116140 T __vfprintf_chk@@GLIBC_2.3.4
000000000005bf50 T __vfscanf@@GLIBC_2.2.5
000000000005bf50 W vfscanf@@GLIBC_2.2.5
0000000000064a90 W vfwprintf@@GLIBC_2.2.5
00000000001170a0 T __vfwprintf_chk@@GLIBC_2.4
000000000006a350 W vfwscanf@@GLIBC_2.2.5
0000000000071560 T vprintf@@GLIBC_2.2.5
0000000000116120 T __vprintf_chk@@GLIBC_2.3.4
000000000007b950 W vscanf@@GLIBC_2.2.5
000000000007bb00 W __vsnprintf@@GLIBC_2.2.5
000000000007bb00 W vsnprintf@@GLIBC_2.2.5
0000000000115f40 T __vsnprintf_chk@@GLIBC_2.3.4
0000000000075490 W vsprintf@@GLIBC_2.2.5
0000000000115e40 T __vsprintf_chk@@GLIBC_2.3.4
0000000000075550 W __vsscanf@@GLIBC_2.2.5
0000000000075550 W vsscanf@@GLIBC_2.2.5
0000000000076b50 W vswprintf@@GLIBC_2.2.5
0000000000116ea0 T __vswprintf_chk@@GLIBC_2.4
0000000000076b60 W vswscanf@@GLIBC_2.2.5
0000000000076730 T vwprintf@@GLIBC_2.2.5
0000000000117080 T __vwprintf_chk@@GLIBC_2.4
00000000000769b0 T vwscanf@@GLIBC_2.2.5
0000000000076750 T wprintf@@GLIBC_2.2.5
0000000000116ee0 T __wprintf_chk@@GLIBC_2.4
0000000000076820 T wscanf@@GLIBC_2.2.5
sh-5.1$
看看这个有没有一点帮助
https://man.archlinux.org/man/scanf.3#LIBRARY 人造人 发表于 2023-1-3 13:34
但是具体的定义(实现)是在链接库文件中么?
是的
谢谢大佬 人造人 发表于 2023-1-3 13:34
但是具体的定义(实现)是在链接库文件中么?
是的
大佬,我在追问一下,编译器在编译源代码的时候,遇到这些内置函数,是自动连接到这些链接库文件么?不需要想像链接自己做的链接文件一样,选项上加上 -L路径,如gcc -L/home/ues_name/lib abc.c这样的形式吧? gchzp1993 发表于 2023-1-3 15:05
大佬,我在追问一下,编译器在编译源代码的时候,遇到这些内置函数,是自动连接到这些链接库文件么?不需 ...
这个不需要你手动加,gcc这个命令在内部自己加的
--verbose选项可以查看详细的编译步骤
sh-5.1$ gcc --verbose -o main main.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/cc1 -quiet -v main.c -quiet -dumpbase main.c -dumpbase-ext .c -mtune=generic -march=x86-64 -version -o /tmp/cc53u5Ip.s
GNU C17 (GCC) version 12.2.0 (x86_64-pc-linux-gnu)
compiled by GNU C version 12.2.0, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.25-GMP
warning: MPFR header version 4.1.0-p13 differs from library version 4.1.1-p1.
warning: MPC header version 1.2.1 differs from library version 1.3.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/include
/usr/local/include
/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/include-fixed
/usr/include
End of search list.
GNU C17 (GCC) version 12.2.0 (x86_64-pc-linux-gnu)
compiled by GNU C version 12.2.0, GMP version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version isl-0.25-GMP
warning: MPFR header version 4.1.0-p13 differs from library version 4.1.1-p1.
warning: MPC header version 1.2.1 differs from library version 1.3.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 4b798a352742e8322eaa4a166b8f6299
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64'
as -v --64 -o /tmp/ccnRHJhA.o /tmp/cc53u5Ip.s
GNU assembler version 2.39.0 (x86_64-pc-linux-gnu) using BFD version (GNU Binutils) 2.39.0
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64' '-dumpdir' 'main.'
/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/collect2 -plugin /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccby7A03.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -o main /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib/Scrt1.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/crtbeginS.o -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0 -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/../../.. /tmp/ccnRHJhA.o -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/crtendS.o /usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib/crtn.o
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64' '-dumpdir' 'main.'
sh-5.1$
人造人 发表于 2023-1-3 15:42
这个不需要你手动加,gcc这个命令在内部自己加的
--verbose选项可以查看详细的编译步骤
多谢大佬
页:
[1]