linux中的file命令简介

        我们先来看看三个文件,

        test.h文件:

 

void print();

        test.c文件:

 

 

#include <stdio.h>
#include "test.h"

void print()
{
	printf("rainy days\n");
}

 

       main.c文件:

 

#include "test.h"

int main()
{
	print();
	return 0;
}

       下面, 我们来生成目标文件、静态链接库、动态链接库、可执行文件, 然后用file命令来玩玩, 如下:

 

 

[taoge@localhost learn_file]$ ls
main.c  test.c  test.h
[taoge@localhost learn_file]$ gcc -c test.c main.c
[taoge@localhost learn_file]$ ls
main.c  main.o  test.c  test.h  test.o
[taoge@localhost learn_file]$ gcc test.o main.o
[taoge@localhost learn_file]$ ls
a.out  main.c  main.o  test.c  test.h  test.o
[taoge@localhost learn_file]$ ./a.out 
rainy days
[taoge@localhost learn_file]$ ar rcs libtest.a test.o
[taoge@localhost learn_file]$ gcc -shared -fPIC -o libtest.so test.o
[taoge@localhost learn_file]$ ls
a.out  libtest.a  libtest.so  main.c  main.o  test.c  test.h  test.o
[taoge@localhost learn_file]$ ls -l
total 36
-rwxrwxr-x 1 taoge taoge 4703 Jul 26 00:23 a.out
-rw-rw-r-- 1 taoge taoge  990 Jul 26 00:23 libtest.a
-rwxrwxr-x 1 taoge taoge 4025 Jul 26 00:24 libtest.so
-rw-rw-r-- 1 taoge taoge   55 Jul 26 00:15 main.c
-rw-rw-r-- 1 taoge taoge  764 Jul 26 00:22 main.o
-rw-rw-r-- 1 taoge taoge   80 Jul 26 00:12 test.c
-rw-rw-r-- 1 taoge taoge   14 Jul 26 00:12 test.h
-rw-rw-r-- 1 taoge taoge  848 Jul 26 00:22 test.o
[taoge@localhost learn_file]$ ls -l
total 36
-rwxrwxr-x 1 taoge taoge 4703 Jul 26 00:23 a.out
-rw-rw-r-- 1 taoge taoge  990 Jul 26 00:23 libtest.a
-rwxrwxr-x 1 taoge taoge 4025 Jul 26 00:24 libtest.so
-rw-rw-r-- 1 taoge taoge   55 Jul 26 00:15 main.c
-rw-rw-r-- 1 taoge taoge  764 Jul 26 00:22 main.o
-rw-rw-r-- 1 taoge taoge   80 Jul 26 00:12 test.c
-rw-rw-r-- 1 taoge taoge   14 Jul 26 00:12 test.h
-rw-rw-r-- 1 taoge taoge  848 Jul 26 00:22 test.o
[taoge@localhost learn_file]$ file *
a.out:      ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped
libtest.a:  current ar archive
libtest.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (GNU/Linux), dynamically linked, not stripped
main.c:     ASCII C program text
main.o:     ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
test.c:     ASCII C program text
test.h:     ASCII text
test.o:     ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
[taoge@localhost learn_file]$ 

       我们看到, 虽然ls -l能大概看到文件的类型, 但是, 那结果太粗略了。 还是用file命令吧, 它可以比ls -l更详细地显示文件的类型信息。 我们知道, 在Windows中, 系统根据文件的后缀名来判断文件类型, 一个test.exe文件如果改名为test.xxx, 那就不能运行了。 而在linux中, 实际并不存在文件扩展名一说, 系统是根据文件本身的信息来判断文件类型的。

 

 

       test.h和test.c, main.c很简单, 我就不说了。

       test.o和main.o都是目标文件, ELF 32-bit LSB relocatable, not stripped

       a.out是可执行文件, 我们可以看到,ELF 32-bit LSB executable,  dynamically linked (uses shared libs), not stripped

       libtest.a是静态链接库, 我们可以看到, current ar archive

       libtest.so是动态链接库, 我们可以看到,ELF 32-bit LSB shared object,  dynamically linked, not stripped

 

 

       注意到, 目标文件、可执行文件、静态链接库、动态链接库都是ELF文件, 且暂时都是not stripped,  那么什么是ELF文件, 什么是not stripped呢? 后续我们会介绍。

 

       OK, 本文主要说说file命令, 很简单, 先到此为止吧。

 

 

 

 

 

      



 

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值