-shared; Create a shared library /* 创建共享库时需要添加的编译选项 */
因为共享库的代码都是位置无关的,所以通常还需要添加编译选项-fPIC。
-fPIC; Generate position-independent code if possible (large mode)
-fpic; Generate position-independent code if possible (small mode)
编译共享库时使用;
-fPIE; Generate position-independent code for executables if possible (large mode)
-fpie; Generate position-independent code for executables if possible (small mode)
编译可执行程序时使用
还需要在ld时增加-pie选项才能产生这种代码。即gcc -fpie -pie来编译程序。单独使用哪一个都无法达到效果。
-pie; Create a position independent executable
================================================================================================
关于large mode和small mode
“mcmodel=” Use given x86-64 code model [仅适用于x86_64环境]
-mcmodel=small
Generate code for the small code model: the program and its symbols must be linked in the lower 2 GB of the address space. Pointers are 64 bits. Programs can be statically or dynamically linked. This is the default code model.
[默认值]程序和它的符号必须位于2GB以下的地址空间。指针仍然是64位。程序可以静态连接也可以动态连接。
-mcmodel=kernel
Generate code for the kernel code model. The kernel runs in the negative 2 GB of the address space. This model has to be used for Linux kernel code.
内核运行于2GB地址空间之外。在编译linux内核时必须使用该选项!
-mcmodel=medium
Generate code for the medium model: The program is linked in the lower 2 GB of the address space. Small symbols are also placed there. Symbols with sizes larger than ‘-mlarge-data-threshold’ are put into large data or bss sections and can be located above 2GB. Programs can be statically or dynamically linked.
程序必须位于2GB以下的地址空间,但是它的符号可以位于任何地址空间。程序可以静态连接也可以动态连接。
注意:共享库不能使用这个选项编译!
-mcmodel=large
Generate code for the large model: This model makes no assumptions about addresses and sizes of sections.
对地址空间没有任何限制。