1.編譯含有splice()函數的程式時出現,'SPLICE_F_MOVE' undeclared,'SPLICE_F_NONBLOCK' ‘SPLICE_F_MORE' 也是一樣undeclared!2.使用man splice查看,發現要定義巨集_GNU_SOURCE1 #define _GNU_...
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 NAME
2 feature_test_macros - feature test macros
3
4 SYNOPSIS
5 #include <features.h>
6
7 DESCRIPTION
8 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.h
2 $: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 splicing
4 (but we may still block on the fd
5 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/