你的位置:首页 > 操作系统

[操作系统]splice()函数,SPLICE_F_MOVE SPLICE_F_NONBLOCK SPLICE_F_MORE undeclared


1.编译含有splice()函数的程序时出现,'SPLICE_F_MOVE'  undeclared,'SPLICE_F_NONBLOCK' ‘SPLICE_F_MORE' 也是一样undeclared!

2.使用man splice查看,发现要定义宏_GNU_SOURCE

1 #define _GNU_SOURCE     /* See feature_test_macros(7) */2 #include <fcntl.h>3 4 ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);

我们跟着后面宏的注释See feature_test_macros(7)

1 $:man feature_test_macros

man手册上面的描述是:特性测试宏允许程序编译时控制系统头文件的宏定义!

1 NAME2    feature_test_macros - feature test macros3 4 SYNOPSIS5    #include <features.h>6 7 DESCRIPTION8    Feature test macros allow the programmer to control the definitions that are exposed by system header files when a program is compiled.

 

3.我们找找看,splice()函数的这个几个flags参数宏定义在哪里。splice()函数定义在<fcntl.h>头文件里面,查看头文件

1 $:vim /usr/include/fcntl.h

然而里面并没有这几个宏,但里面有包含了<features.h>,<bits/fcntl.h>文件,我们跟进去这2个文件。我的系统是64位的kail。

bits文件夹在/usr/include/x86_64-linux-gnu/bits/

1 $:vim /usr/include/features.h2 $:vim /usr/include/x86_64-linux-gnu/bits/fcntl.h

这2个里面都也没有,<bits/fcntl.h>文件里面又包含了<bits/fcntl-linux.h>。我们继续跟进。

1 $:vim /usr/include/x86_64-linux-gnu/bits/fcntl-linux.h

这次我们找到了:

1 /* Flags for SPLICE and VMSPLICE. */2 # define SPLICE_F_MOVE   1  /* Move pages instead of copying. */3 # define SPLICE_F_NONBLOCK 2  /* Don't block on the pipe splicing4             (but we may still block on the fd5             we splice from/to). */6 # define SPLICE_F_MORE   4  /* Expect more data. */7 # define SPLICE_F_GIFT   8  /* Pages passed in are a gift. */

4.现在我们根据这几个宏定义的上下文来查看跟_GNU_SOURCE宏的联系。

这4个宏包含在#ifdef __USE_GNU里面,我回头在看看features.h

1 $:vim /usr/include/features.h

直接搜索__USE_GNU,发现里面有这个定义,跟_GNU_SOURCE关联。

#ifdef _GNU_SOURCE# define __USE_GNU 1#endif

5.如果不注重里面的包含细节,直接用grep搜索,简单粗暴!!!

$:grep -rn 'SPLICE_F' /usr/include/