linux中编译静态库(.a)和动态库(.so)的基本方法_only you

本文详细介绍Linux环境中静态库(.a)和动态库(.so)的创建、编译与使用方法,包括使用ar命令创建静态库、ranlib创建索引、以及动态库的创建与依赖库的配置等关键步骤。

g++ BaseChar.o libjpeg.a -shared -o libBMP.so

linux中编译静态库(.a)和动态库(.so)的基本方法_only you

2012-12-27 | 阅: 1  转: 36  |   分享 
   修改
linux中编译静态库(.a)和动态库(.so)的基本方法
2007-10-24 14:19

静态库 
       在linux环境中, 使用ar命令创建静态库文件.如下是命令的选项: 
          d -----从指定的静态库文件中删除文件 
          m -----把文件移动到指定的静态库文件中 
          p -----把静态库文件中指定的文件输出到标准输出 
          q -----快速地把文件追加到静态库文件中 
          r -----把文件插入到静态库文件中 
          t -----显示静态库文件中文件的列表 
          x -----从静态库文件中提取文件 
      还有多个修饰符修改以上基本选项,详细请man ar 以下列出三个: 
          a -----把新的目标文件(*.o)添加到静态库文件中现有文件之后 
          b -----***************************************之前 
          v -----使用详细模式 
ar 命令的命令行格式如下: 
      ar [-]{dmpqrtx}[abcfilNoPsSuvV] [membername] [count] archive files... 
参数archive定义库的名称, files是库文件中包含的目标文件的清单, 用空格分隔每个文件. 
比如创建一个静态库文件的命令如下: 
      ar r libapue.a error.o errorlog.o lockreg.o 
这样就了libapue.a静态库文件, 可以用 t 选项显示包含在库中的文件 
      创建库文件之后,可以创建这个静态库文件的索引来帮助提高和库连接的其他程序的编译速度.使用ranlib程序创建库的索引,索引存放在库文件内部. 
      ranlib libapue.a 
用nm程序显示存档文件的索引,它可以显示目标文件的符号 
nm libapue.a | more 
如果是显示目标文件的符号: 
nm error.o | more 
如何使用呢?如下所示: 
gcc -o test test.c libapue.a 
这样就可以在test.c中调用在libapue.a中的函数了.


动态库 
1.创建共享库 
     gcc -shared -o libapue.so error.o errorlog.o 
这样就创建了共享库! 
2.编译共享库 
    假设共享库位于当前目录(即跟程序文件相同的目录中) 
gcc -o test -L. -lapue test.c 
这样就编译出了不包含函数代码可执行文件了,但是但你运行时会发现linux动态加载器打不到libapue.so文件. 
可以用ldd 命令查看可执行文件依赖什么共享库: 
ldd test 
如何才能让动态加载器发现库文件呢?有两种方法可以解决: 
     LD_LIBRARY_PATH 环境变量  
     /etc/ld.so.conf文件 
    1.环境变量 
       export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:." 
    2.修改/etc/ld.so.conf文件.位于/etc/ld.so.conf 
一般应用程序的库文件不与系统库文件放在同一个目录下,一般把应用程序的共享库文件放在/usr/local/lib下,新建一个属于自己的目录apue,然后把刚才libapue.so复制过去就行了 
同时在/etc/ld.so.conf中新增一行: 
/usr/local/lib/apue 

以后在编译程序时加上编译选项: 
-L/usr/local/lib/apue -lapue 
这样就可以使用这个libapue.so共享库了!! 
全文结束!! 

写此文章参考了:<<Professional Assembly Language>>. 
     这是一本好书!如果是学习AT&T汇编语法,建议阅读此书!!

test@test-pc:/media/test/DATA/soft_DMG/Soft/Soft_dmg$ box64 '/media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft' [BOX64] Box64 arm64 v0.3.6 with Dynarec built on Sep 6 2025 16:26:44 [BOX64] Dynarec for ARM64, with extension: ASIMD AES CRC32 PMULL SHA1 SHA2 [BOX64] Running on Phytium,D2000/8 E8C with 8 cores, pagesize: 4096 [BOX64] Will use hardware counter measured at 48.0 MHz emulating 3.0 GHz [BOX64] Didn't detect 48bits of address space, considering it's 39bits [BOX64] Counted 61 Env var [BOX64] Library search path: [BOX64] Binary search path: ./:bin/:/usr/local/sbin/:/usr/local/bin/:/usr/sbin/:/usr/bin/:/sbin/:/bin/:/usr/games/:/usr/local/games/ [BOX64] Looking for /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Rename process to "DamageAssessSoft" [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libJW_QtDB_Lib.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libJW_QtIO_Lib.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libJW_View_Lib.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libJW_AppCommon_Lib.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libJW_3DWindow_Lib.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpython3.8.so.1.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libonnxruntime.so.1.16.3 [BOX64] Using native(wrapped) libtiff.so.5 [BOX64] Using emulated /usr/lib/box64-x86_64-linux-gnu/libstdc++.so.6 [BOX64] Using native(wrapped) libm.so.6 [BOX64] Using native(wrapped) libc.so.6 [BOX64] Using native(wrapped) ld-linux-x86-64.so.2 [BOX64] Using native(wrapped) libpthread.so.0 [BOX64] Using native(wrapped) libdl.so.2 [BOX64] Using native(wrapped) libutil.so.1 [BOX64] Using native(wrapped) librt.so.1 [BOX64] Using native(wrapped) libbsd.so.0 [BOX64] Using emulated /usr/lib/box64-x86_64-linux-gnu/libgcc_s.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_surface.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_recognition.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libboost_system.so.1.64.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libflann_cpp.so.1.9 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkRenderingCore-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libopencv_world.so.3.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libQt5Charts.so.5 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libQt5PrintSupport.so.5 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libQt5Widgets.so.5 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libQt5Gui.so.5 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libQt5Sql.so.5 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libQt5Xml.so.5 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libQt5Core.so.5 [BOX64] Using native(wrapped) libz.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libicui18n.so.56 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libicuuc.so.56 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libicudata.so.56 [BOX64] Using native(wrapped) libgthread-2.0.so.0 [BOX64] Using native(wrapped) libglib-2.0.so.0 [BOX64] Look for arc4random in loaded elfs [BOX64] Warning, function arc4random not found in lib libbsd.so.0 [BOX64] Look for arc4random in loaded elfs [BOX64] Warning, function arc4random not found in lib libbsd.so.0 [BOX64] Using native(wrapped) libGL.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libjpeg.so.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpng12.so.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonColor-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkFiltersGeometry-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkFiltersSources-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkFiltersGeneral-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkFiltersCore-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonExecutionModel-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonComputationalGeometry-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonDataModel-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonSystem-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtksys-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonTransforms-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonMath-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonCore-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libvtkCommonMisc-8.0.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libboost_filesystem.so.1.64.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_io.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_features.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_filters.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_search.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_kdtree.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_sample_consensus.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_common.so.1.8 [BOX64] Using native(wrapped) libgomp.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libboost_thread.so.1.64.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_octree.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libboost_iostreams.so.1.64.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libboost_chrono.so.1.64.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_io_ply.so.1.8 [BOX64] Using native(wrapped) libusb-1.0.so.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libOpenNI2.so.0 [BOX64] Using native(wrapped) libpcap.so.0 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libqhull.so.7 [BOX64] Using native(wrapped) libexpat.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_segmentation.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_visualization.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libflann.so.1.9 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libJW_PluginAPI_Lib.so.1 [BOX64] Using native(wrapped) libgmp.so.10 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libJW_FrameBuffer_Lib.so.1 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libQt5OpenGL.so.5 [BOX64] Using native(wrapped) libX11.so.6 [BOX64] Using native(wrapped) libxcb.so.1 [BOX64] Using native(wrapped) libXau.so.6 [BOX64] Using native(wrapped) libXdmcp.so.6 [BOX64] Using native(wrapped) libXt.so.6 [BOX64] Using native(wrapped) libXext.so.6 [BOX64] Error: Global Symbol applicationShellWidgetClass not found, cannot apply R_X86_64_GLOB_DAT @0x3f45d82880 ((nil)) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_visualization.so.1.8 [BOX64] Using emulated /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libpcl_ml.so.1.8 [BOX64] Error: Symbol TIFFSetField not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d69e8 (0xb90d6) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFOpen not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d6ad0 (0xb92a6) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFNumberOfStrips not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d6d28 (0xb9756) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFReadEncodedStrip not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d7dc8 (0xbb896) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFGetField not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d8238 (0xbc176) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFWriteScanline not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d8548 (0xbc796) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFStripSize not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d87b8 (0xbcc76) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFReadTile not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d8ff8 (0xbdcf6) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFTileSize not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d9950 (0xbefa6) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: Symbol TIFFDefaultStripSize not found, cannot apply R_X86_64_JUMP_SLOT @0x1009d99d8 (0xbf0b6) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/DamageAssessSoft [BOX64] Error: PltResolver: Symbol TIFFSetErrorHandler(ver 0: TIFFSetErrorHandler) not found, cannot apply R_X86_64_JUMP_SLOT 0x3f15ae6608 (0x3f131cd3c6) in /media/test/DATA/soft_DMG/Soft/Soft_dmg/lib/libopencv_world.so.3.1 (local_maplib=(nil), global maplib=0x543b4460, deepbind=0) QSocketNotifier: Can only be used with threads started with QThread terminate called after throwing an instance of 'CGAL::Assertion_exception' what(): CGAL ERROR: assertion violation! Expr: -CGAL_IA_MUL(-1.1, 10.1) != CGAL_IA_MUL(1.1, 10.1) File: /usr/include/CGAL/Interval_nt.h Line: 279 Explanation: Wrong rounding: did you forget the -frounding-math option if you use GCC (or -fp-model strict for Intel)? [BOX64] NativeBT: /usr/local/bin/box64() [0x34a50c7c] [BOX64] NativeBT: /usr/local/bin/box64() [0x34a8856c] [BOX64] NativeBT: linux-vdso.so.1(__kernel_rt_sigreturn+0) [0x7f9b4b05b0] [BOX64] NativeBT: /lib/aarch64-linux-gnu/libc.so.6(gsignal+0xe0) [0x7f9b325d18] [BOX64] NativeBT: /lib/aarch64-linux-gnu/libc.so.6(abort+0x110) [0x7f9b312a68] [BOX64] NativeBT: [0x7f906b177c] [BOX64] EmulatedBT: box64(abort+0) [0x30030080] [BOX64] EmulatedBT: ??? [(nil)] [BOX64] 5307|SIGABRT @0x7f9b325d18 (gsignal(/lib/aarch64-linux-gnu/libc.so.6)) (x64pc=0x30030080/"???", rsp=0x7f9afaddd8, stack=0x7f9a7af000:0x7f9afaf000 own=(nil) fp=0x1009db080), for accessing 0x3e8000014bb (code=-6/prot=0), db=(nil)((nil):(nil)/(nil):(nil)/???:clean, hash:0/0) handler=(nil) RSP-0x20:0x0000003f0b0af274 RSP-0x18:0x0000000000000167 RSP-0x10:0x00000000544348b0 RSP-0x08:0x0000000000000078 RSP+0x00:0x0000003f0b0a09db RSP+0x08:0x0000000055c45018 RSP+0x10:0x0000000055c44e10 RSP+0x18:0x0000000055c44e70 RAX:0x0000000055bfaa80 RCX:0x0000007f9b461468 RDX:0x0000000000000001 RBX:0x0000000055c45348 RSP:0x0000007f9afaddd8 RBP:0x00000001009db080 RSI:0x0000000035cd5f48 RDI:0x0000000055c44e20 R8:0x00000000ffffffff R9:0x0000007f9afad860 R10:0x0000003f0c023220 R11:0x0000007f9afad620 R12:0x0000000054b2f7e0 R13:0x0000007f9afade68 R14:0x0000000055c44e90 R15:0x0000003f0b254640 ES:0x002b CS:0x0033 SS:0x002b DS:0x002b FS:0x0043 GS:0x0053 已放弃 (核心已转储)
最新发布
09-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值