您的位置:首页 > 运维架构 > Linux

linux中的file命令简介

2015-07-26 20:22 585 查看
        我们先来看看三个文件,

        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命令, 很简单, 先到此为止吧。

      
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: