


- #include
- #include
-
- int main()
- {
- const char *msg0="hello printf\n";
- const char *msg1="hello fwrite\n";
- const char *msg2="hello write\n";
-
- printf("%s", msg0);
- fwrite(msg1, strlen(msg0), 1, stdout);
- write(1, msg2, strlen(msg2));
-
- fork();
- return 0;
- }
运行出结果:
- hello printf
- hello fwrite
- hello write
- hello write
- hello printf
- hello fwrite
- hello printf
- hello fwrite
- [root@localhost linux]# ls -l
- 总用量 12
- -rwxr-xr-x. 1 root root 7438 "9月 13 14:56" a.out
- -rw-r--r--. 1 root root 654 "9月 13 14:56" test.c
ls -l读取存储在磁盘上的文件信息,然后显示出来

其实这个信息除了通过这种方式来读取,还有一个stat命令能够看到更多信息。
- [root@localhost linux]# stat test.c
- File: "test.c"
- Size: 654 Blocks: 8 IO Block: 4096 普通文件
- Device: 802h/2050d Inode: 263715 Links: 1
- Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
- Access: 2017-09-13 14:56:57.059012947 +0800
- Modify: 2017-09-13 14:56:40.067012944 +0800
- Change: 2017-09-13 14:56:40.069012948 +0800

- [root@localhost linux]# touch abc
- [root@localhost linux]# ls -i abc
- 263466 abc
创建一个新文件主要有一下4
个操作:
- 263563 -rw-r--r--. 2 root root 0 9月 15 17:45 abc
- 261678 lrwxrwxrwx. 1 root root 3 9月 15 17:53 abc.s -> abc
- 263563 -rw-r--r--. 2 root root 0 9月 15 17:45 def

下面解释一下文件的三个时间 :
- 测试程序
- /add.h/
- #ifndef __ADD_H__
- #define __ADD_H__
- int add(int a, int b);
- #endif // __ADD_H__
- /add.c/
- #include "add.h"
- int add(int a, int b)
- {
- return a + b;
- }
-
- /sub.h/
- #ifndef __SUB_H__
- #define __SUB_H__
- int sub(int a, int b);
- #endif // __SUB_H__
- /add.c/
- #include "add.h"
- int sub(int a, int b)
- {
- return a - b;
- }
-
- ///main.c
- #include
- #include "add.h"
- #include "sub.h"
-
- int main( void )
- {
- int a = 10;
- int b = 20;
- printf("add(10, 20)=%d\n", a, b, add(a, b));
- a = 100;
- b = 20;
- printf("sub(%d,%d)=%d\n", a, b, sub(a, b));
- }
- [root@localhost linux]# ls
- add.c add.h main.c sub.c sub.h
- [root@localhost linux]# gcc -c add.c -o add.o
- [root@localhost linux]# gcc -c sub.c -o sub.o
-
- 生成静态库
- [root@localhost linux]# ar -rc libmymath.a add.o sub.o
- ar是gnu归档工具,rc表示(replace and create)
-
- 查看静态库中的目录列表
- [root@localhost linux]# ar -tv libmymath.a
- rw-r--r-- 0/0 1240 Sep 15 16:53 2017 add.o
- rw-r--r-- 0/0 1240 Sep 15 16:53 2017 sub.o
- t:列出静态库中的文件
- v:verbose 详细信息
-
- [root@localhost linux]# gcc main.c -L. -lmymath
-
- -L 指定库路径
- -l 指定库名
- 测试目标文件生成后,静态库删掉,程序照样可以运行
示例: gcc main.o -o main –L. -lhello
-
- [root@localhost linux]# export LD_LIBRARY_PATH=.
- [root@localhost linux]# gcc main.c -lmymath
- [root@localhost linux]# ./a.out
- add(10, 20)=30
- sub(100, 20)=80
-
-
- [root@localhost linux]# cat /etc/ld.so.conf.d/bit.conf
- /root/tools/linux
- [root@localhost linux]# ldconfig