转载引用: Linux中的动态库和静态库(.a/.la/.so/.o) - findumars - 博客园
工具:libtools
使用brew update && brew install binutils
,然后用greadelf
和gobjdump
- binutils is keg-only, which means it was not symlinked into /usr/local,
- because because Apple provides the same tools and binutils is poorly supported on macOS.
-
- If you need to have binutils first in your PATH run:
- echo 'export PATH="/usr/local/opt/binutils/bin:$PATH"' >> ~/.bash_profile
-
- For compilers to find binutils you may need to set:
- export LDFLAGS="-L/usr/local/opt/binutils/lib"
- export CPPFLAGS="-I/usr/local/opt/binutils/include"
>>readelf -h atoi.o
readelf: Error: Not an ELF file - it has the wrong magic bytes at the start
使用 otool 命令查看:
>> otool -l atoi.o
结果:
- atoi.o:
- Load command 0
- cmd LC_SEGMENT_64
- cmdsize 392
- segname
- vmaddr 0x0000000000000000
- vmsize 0x00000000000000a0
- fileoff 552
- filesize 160
- maxprot 0x00000007
- initprot 0x00000007
- nsects 4
- flags 0x0
- Section
- sectname __text
- segname __TEXT
- addr 0x0000000000000000
- size 0x000000000000003d
- offset 552
>> file atoi.o
atoi.o: Mach-O 64-bit object x86_64
>> otool -L main
main:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)
>> otool -L atoi.so.1
atoi.so.1:
atoi.so.1 (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)
>> libtool --mode=compile gcc -c atoi.c
error: /Library/Developer/CommandLineTools/usr/bin/libtool: unknown option character `-' in: --mode=compile
原因:This is because OS X has its own libtool
, quite different from GNU's libtool。
解决方案
:安装 gnubin 工具,直接brew install libtool* 即可。
>> brew install libtool*
>> libtool --tag=CC --mode=compile gcc -c atoi.c
libtool: compile: gcc -c atoi.c -fno-common -DPIC -o .libs/atoi.o
libtool: compile: gcc -c atoi.c -o atoi.o >/dev/null 2>&1