[root@db-172-16-3-150 zzz]# cat a.c#include <stdio.h>#include "a.h"void display() {fprintf(stdout, "this is display function in a.c\n");}
[root@db-172-16-3-150 zzz]# cat a.hvoid display();
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g -c ./a.c -o a.o
DESCRIPTIONWhen you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options"allow you to stop this process at an intermediate stage. For example, the -c option says not to run thelinker. Then the output consists of object files output by the assembler.
[root@db-172-16-3-150 zzz]# ar -rcs liba.a ./a.o
[root@db-172-16-3-150 zzz]# ar -t liba.aa.o
[root@db-172-16-3-150 zzz]# nm liba.aa.o:0000000000000000 T display // 这个是display函数U fwriteU stdout
-fpicGenerate position-independent code (PIC) suitable for use in a shared library, if supported for the targetmachine. Such code accesses all constant addresses through a global offset table (GOT). The dynamicloader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is partof the operating system). If the GOT size for the linked executable exceeds a machine-specific maximumsize, you get an error message from the linker indicating that -fpic does not work; in that case, recompilewith -fPIC instead. (These maximums are 8k on the SPARC and 32k on the m68k and RS/6000. The 386 has nosuch limit.)Position-independent code requires special support, and therefore works only on certain machines. For the386, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is alwaysposition-independent.-fPICIf supported for the target machine, emit position-independent code, suitable for dynamic linking andavoiding any limit on the size of the global offset table. This option makes a difference on the m68k,PowerPC and SPARC.Position-independent code requires special support, and therefore works only on certain machines.
[root@db-172-16-3-150 zzz]# cat a.c#include <stdio.h>#include "a.h"void display() {fprintf(stdout, "this is display function in a.c\n");}
[root@db-172-16-3-150 zzz]# cat a.hvoid display();
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g -fPIC -c ./a.c -o a.o
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./b.c a.o -o b[root@db-172-16-3-150 zzz]# ./bthis is display function in a.c
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g -shared a.o -o liba.so/usr/bin/ld: a.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPICa.o: could not read symbols: Bad valuecollect2: ld returned 1 exit status所以必须加上-fPIC :[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g -fPIC -c ./a.c -o a.o[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g -shared a.o -o liba.so
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g b.c -L. -la -o b
-rw-r--r-- 1 root root 6168 Aug 15 10:26 liba.a-rwxr-xr-x 1 root root 8465 Aug 15 10:56 liba.so
[root@db-172-16-3-150 zzz]# lltotal 40-rw-r--r-- 1 root root 110 Aug 15 09:58 a.c-rw-r--r-- 1 root root 16 Aug 15 09:58 a.h-rw-r--r-- 1 root root 6064 Aug 15 11:09 a.o-rw-r--r-- 1 root root 56 Aug 15 09:58 b.c-rw-r--r-- 1 root root 6208 Aug 15 11:09 liba.a-rwxr-xr-x 1 root root 8449 Aug 15 11:10 liba.so[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g b.c -L. -la -o b[root@db-172-16-3-150 zzz]# ./b./b: error while loading shared libraries: liba.so: cannot open shared object file: No such file or directory[root@db-172-16-3-150 zzz]# export LD_LIBRARY_PATH=.[root@db-172-16-3-150 zzz]# ll
total 48-rw-r--r-- 1 root root 110 Aug 15 09:58 a.c-rw-r--r-- 1 root root 16 Aug 15 09:58 a.h-rw-r--r-- 1 root root 6064 Aug 15 11:09 a.o-rwxr-xr-x 1 root root 8185 Aug 15 11:14 b-rw-r--r-- 1 root root 56 Aug 15 09:58 b.c-rw-r--r-- 1 root root 6208 Aug 15 11:09 liba.a-rwxr-xr-x 1 root root 8449 Aug 15 11:10 liba.so[root@db-172-16-3-150 zzz]# ./bthis is display function in a.c
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g -static b.c -L. -la -o b[root@db-172-16-3-150 zzz]# lltotal 640-rw-r--r-- 1 root root 110 Aug 15 09:58 a.c-rw-r--r-- 1 root root 16 Aug 15 09:58 a.h-rw-r--r-- 1 root root 6064 Aug 15 11:09 a.o-rwxr-xr-x 1 root root 609720 Aug 15 11:14 b-rw-r--r-- 1 root root 56 Aug 15 09:58 b.c-rw-r--r-- 1 root root 6208 Aug 15 11:09 liba.a-rwxr-xr-x 1 root root 8449 Aug 15 11:10 liba.so[root@db-172-16-3-150 zzz]# ./bthis is display function in a.c// 修改LD_LIBRARY_PATH后依旧可用使用, 说明却是已经不是动态状态了. 但是文件硕大(609720 bytes). 不建议使用.[root@db-172-16-3-150 zzz]# export LD_LIBRARY_PATH=/usr/lib[root@db-172-16-3-150 zzz]# ./bthis is display function in a.c说明 :-staticOn systems that support dynamic linking, this prevents linking with the shared libraries. On other sys-tems, this option has no effect.
[root@db-172-16-3-150 zzz]# mv liba.so libb.so[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g b.c -L. -la -o b[root@db-172-16-3-150 zzz]# ll -atotal 64drwxr-xr-x 2 root root 4096 Aug 15 11:18 .drwxr-x--- 16 root root 4096 Aug 9 11:22 ..-rw-r--r-- 1 root root 110 Aug 15 09:58 a.c-rw-r--r-- 1 root root 16 Aug 15 09:58 a.h-rw-r--r-- 1 root root 6064 Aug 15 11:09 a.o-rwxr-xr-x 1 root root 9973 Aug 15 11:18 b-rw-r--r-- 1 root root 56 Aug 15 09:58 b.c-rw-r--r-- 1 root root 6208 Aug 15 11:09 liba.a-rwxr-xr-x 1 root root 8449 Aug 15 11:10 libb.so
[root@db-172-16-3-150 zzz]# mv liba.so libb.so[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g b.c -L. -lb -o b[root@db-172-16-3-150 zzz]# ./b./b: error while loading shared libraries: libb.so: cannot open shared object file: No such file or directory[root@db-172-16-3-150 zzz]# export LD_LIBRARY_PATH=.[root@db-172-16-3-150 zzz]# ./bthis is display function in a.c
ar :r Insert the files member... into archive (with replacement). This operation differs from q in that any pre-viously existing members are deleted if their names match those being added.If one of the files named in member... does not exist, ar displays an error message, and leaves undisturbedany existing members of the archive matching that name.By default, new members are added at the end of the file; but you may use one of the modifiers a, b, or ito request placement relative to some existing member.The modifier v used with this operation elicits a line of output for each file inserted, along with one ofthe letters a or r to indicate whether the file was appended (no old member deleted) or replaced.c Create the archive. The specified archive is always created if it did not exist, when you request anupdate. But a warning is issued unless you specify in advance that you expect to create it, by using thismodifier.s Write an object-file index into the archive, or update an existing one, even if no other change is made tothe archive. You may use this modifier flag either with any operation, or alone. Running ar s on anarchive is equivalent to running ranlib on it.t Display a table listing the contents of archive, or those of the files listed in member... that are presentin the archive. Normally only the member name is shown; if you also want to see the modes (permissions),timestamp, owner, group, and size, you can request that by also specifying the v modifier.If you do not specify a member, all files in the archive are listed.If there is more than one file with the same name (say, fie) in an archive (say b.a), ar t b.a fie listsonly the first instance; to see them all, you must ask for a complete listing---in our example, ar t b.a.
[root@db-172-16-3-150 lib]# cd /usr/lib[root@db-172-16-3-150 lib]# ar -t libz.aadler32.ocompress.ocrc32.ogzio.ouncompr.odeflate.otrees.ozutil.oinflate.oinfback.oinftrees.oinffast.o
[root@db-172-16-3-150 lib]# nm ./libz.aadler32.o:0000000000000000 T adler320000000000000390 T adler32_combinecompress.o:0000000000000000 r .LC0U _GLOBAL_OFFSET_TABLE_0000000000000000 T __i686.get_pc_thunk.bx00000000000000f0 T compress0000000000000020 T compress20000000000000000 T compressBoundU deflateU deflateEndU deflateInit_.......这里标记的意思可参考man nm[root@db-172-16-3-150 zzz]# nm liba.so0000000000200660 a _DYNAMIC0000000000200800 a _GLOBAL_OFFSET_TABLE_w _Jv_RegisterClasses0000000000200638 d __CTOR_END__0000000000200630 d __CTOR_LIST__0000000000200648 d __DTOR_END__0000000000200640 d __DTOR_LIST__0000000000000628 r __FRAME_END__0000000000200650 d __JCR_END__0000000000200650 d __JCR_LIST__0000000000200828 A __bss_startw __cxa_finalize@@GLIBC_2.2.50000000000000570 t __do_global_ctors_aux00000000000004a0 t __do_global_dtors_aux0000000000200658 d __dso_handlew __gmon_start__0000000000200828 A _edata0000000000200838 A _end00000000000005a8 T _fini0000000000000438 T _init0000000000000480 t call_gmon_start0000000000200830 b completed.61450000000000000550 T display0000000000200828 b dtor_idx.61470000000000000520 t frame_dummyU fwrite@@GLIBC_2.2.5U stdout@@GLIBC_2.2.5
联系客服