linux下ar命令实战

  • linux下ar命令实战已关闭评论
  • 370 views
  • A+
所属分类:linux linux命令

实例一

打包文件

我在这里创建了两个文件max.c和add.c,如下:

[root@localhost xubo]# ls
add.c max.c
[root@localhost xubo]# more add.c
#include <stdio.h>

int add(int num1, int num2)
{
 /* 局部变量声明 */
 int result;

result = num1+num2;

return result;
}
[root@localhost xubo]# more max.c
#include <stdio.h>

int max(int num1, int num2)
{
 /* 局部变量声明 */
 int result;

if (num1 > num2)
 result = num1;
 else
 result = num2;

return result;
}

分别编译两个文件,如下:

[root@localhost xubo]# gcc -c max.c 
[root@localhost xubo]# gcc -c add.c 
[root@localhost xubo]# ls
add.c add.o max.c max.o

打包,使用命令,如下:

 ar rv num.a add.o max.o

或者你想把目录下所有的.o文件打包,如下:

 ar rv num.a add.o *.o

结果如下:

[root@localhost xubo]# ar rv num.a add.o max.o
ar: 正在创建 num.a
a - add.o
a - max.o

下来我们验证一下效果

我们先调用一下max方法,创建main.c,内容如下:

[root@localhost xubo]# more main.c
#include <stdio.h>
int main()
{
 int a = 100;
 int b = 200;
 int ret;
 ret = max(a, b);
 return ret;
}

编译main.c

[root@localhost xubo]# gcc -c main.c
[root@localhost xubo]# ls 
add.c add.o main.c main.o max.c max.o num.a

使用ld链接main.o和num.a,命令如下:

 ld main.o num.a /lib64/libc.so.6 -o main

结果如下:

[root@localhost xubo]# ld main.o num.a /lib64/libc.so.6 -o main
ld: 警告: 无法找到项目符号 _start; 缺省为 000000000040020b

注意,这个警告你想消除的话,就把main.c的main方法变成_start方法就行

编译main.o生成可执行命令,命令如下:

gcc -v -o main main.o num.a

结果如下:

[root@localhost xubo]# gcc -v -o main main.o num.a
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/4.8.5/collect2 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. main.o num.a -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crtn.o

然后执行./main看看效果,结果如下:

[root@localhost xubo]# ./main
[root@localhost xubo]# echo $?
200

如果你想调用add方法的话,就把main函数修改一下就行。

实例二

查看打包内容

使用t参数就行,如下:

[root@localhost xubo]# ar t num.a 
add.o
max.o

实例三

删除包中的某个文件

使用d参数,如下:

[root@localhost xubo]# ar t num.a 
add.o
max.o
[root@localhost xubo]# ar d num.a max.o
[root@localhost xubo]# ar t num.a 
add.o

我们再试试编译执行main.c看看效果,如下:

[root@localhost xubo]# rm -rf main
[root@localhost xubo]# gcc -v -o main main.o num.a
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/4.8.5/collect2 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. main.o num.a -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crtn.o
main.o:在函数‘main’中:
main.c:(.text+0x26):对‘max’未定义的引用
collect2: 错误:ld 返回 1

找不到max函数了。

好就到这里

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar