首先需要写一个例子:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd = open(argv[1], O_RDONLY);
if (fd == -1)
{
printf("open %s error\n", argv[1]);
}
printf("fd = %d\n", fd);
while (1)
{
sleep(100);
}
return 0;
}
然后利用上一篇博客:Makefile简单使用及解读韦东山的通用Makefile实例中的Makefile来编译这个C文件。
如下,已经编译好并且分别用编译出来的test打开编译程序本身test和Makefile文件,打印出来的fd都是=3。
virtual-machine:~/Linux_study$ tree open_test/
open_test/
├── Makefile
├── Makefile.build
└── open_test.c
virtual-machine:~/Linux_study/open_test$ make
virtual-machine:~/Linux_study/open_test$ tree .
.
├── built-in.o
├── Makefile
├── Makefile.build
├── open_test.c
├── open_test.o
└── test
virtual-machine