编译库是添加-fPIC选项的方式及验证

编译库是添加-fPIC选项的方式及验证,第1张

编译库是添加-fPIC选项的方式及验证

1.CMakeLists.txt中的合适位置添加:

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

合适的位置可以理解为不影响其他组件编译的位置,或者用cmake宏可以控制的位置;


2.Makefile中, 查找DEBUG_CFLAGS、RELEASE_CFLAGS或CFLAGS、CXXFLAGS(CPPFLAGS),在其后添加**-fPIC**;


3.configure中,先运行指令**./configure --help**,然后查看C compiler flags对应的名称,先正确使用configure生成makefile,然后在makefile中修改,如下:
(1)运行./configure --help进行查找得到:
(2)生成makefile后,进行关键字(CFLAGS和CXXFLAGS)查找,添加**-fPIC**即可:

第一步:
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L if you have libraries in a
              nonstandard directory 
  LIBS        libraries to pass to the linker, e.g. -l
  CPPFLAGS    C/C++/Objective C preprocessor flags, e.g. -I if
              you have headers in a nonstandard directory 
  CXX         C++ compiler command
  CXXFLAGS    C++ compiler flags
第二步:
AWK = gawk
CC = gcc
CCDEPMODE = depmode=gcc3
CFLAGS = -g -fPIC -O2 -Wall -fno-strict-aliasing -pthread
CPP = gcc -E
CPPFLAGS = -fPIC
CYGPATH_W = echo
DEFS = -DHAVE_CONFIG_H
DEPDIR = .deps
DLLTOOL = false

4.编译出静态库后验证-fPIC选项是否添加成功:

readelf --relocs 静态库路径 | egrep '(GOT|PLT|JU?MP_SLOT)'

eg:

相对路径:readelf --relocs libtinyxml.a | egrep '(GOT|PLT|JU?MP_SLOT)'
绝对路径:readelf --relocs /chenbin/3th_library/jsoncpp-1.8.4/build/install/lib64/libjsoncpp_static.a| egrep '(GOT|PLT|JU?MP_SLOT)'

如果运行指令后控制台输出不为空,则表示添加选项成功,如下所示:

[root@localhost build]# readelf --relocs /chenbin/3th_library/jsoncpp-1.8.4/build/install/lib64/libjsoncpp_static.a| egrep '(GOT|PLT|JU?MP_SLOT)'
000000000000  00a600000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTVN4Json17CharReader + 0
000000001aec  00a600000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTVN4Json17CharReader + 0
000000003130  011400000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTVN4Json13OurCharRea + 0
000000003448  012900000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTVSt9basic_iosIcSt11 + 0
000000003460  012b00000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTTNSt7__cxx1119basic + 0
0000000034b4  00ec00000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTVSt15basic_streambu + 0
0000000034bc  012d00000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTVNSt7__cxx1119basic + 0
0000000034cc  00eb00000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTVNSt7__cxx1115basic + 0
000000003694  012b00000137 R_AARCH64_ADR_GOT 0000000000000000 _ZTTNSt7__cxx1119basic + 0
0000000038d8  013500000137 R_AARCH64_ADR_GOT 0000000000000000 _ZN4Json5Value14maxLar + 0
00000000396c  013600000137 R_AARCH64_ADR_GOT 0000000000000000 _ZN4Json5Value6maxIntE + 0
0000000039f8  013800000137 R_AARCH64_ADR_GOT 0000000000000000 _ZN4Json5Value13maxLar + 0
000000003a3c  013900000137 R_AARCH64_ADR_GOT 0000000000000000 _ZN4Json5Value13minLar + 0
...
...
...

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5651032.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存