2015-06-20 76 views
2

以下爲Linux open_by_handle_at()的文檔:爲什麼我必須聲明一個不相關的struct file_handle變量才能使用該類型?

http://man7.org/linux/man-pages/man2/open_by_handle_at.2.html

我寫這篇文章的C文件:

#define _GNU_SOURCE 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 

typedef void (*foobar) (struct file_handle *); 

,但它與一個不祥的警告編譯:

>gcc -c foobar.c 
warning: ‘struct file_handle’ declared inside parameter list 

如果我在兩者之間添加不相關的聲明:

#define _GNU_SOURCE 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 

struct file_handle *junk; 

typedef void (*foobar) (struct file_handle *); 

然後編譯沒有警告。爲什麼警告?

+0

您可能希望有一個看看[奇怪的編譯器警告C:警告:在參數列表中聲明'結構'](http://stackoverflow.com/questions/16831605/strange-compiler-warning-c-warning-struct-declared-inside-parameter-list #16831856)。 – halfbit

+0

@halfbit根據我引用的文檔,頭文件應該有聲明 –

+1

就像一個註釋:我無法在運行openSUSE 13.2的Linux機器上重現您的警告。該定義似乎通過'#include '包括'/ usr/src/include/bits/fcntl.h'包含'/ usr/src/include/bits/fcntl-linux.h',後者包含第311行的結構定義(在'_ifdef __USE_GNU'中應該由'_GNU_SOURCE'產生)。 – halfbit

回答

0

您尚未提前在任何地方聲明struct file_handle。編譯器首次在上述函數定義中看到struct file_handle。在每種情況下,struct file_handle的聲明都具有塊範圍,即它對於相應的功能是本地的。

在你的函數之前添加下面的結構。

struct file_handle { 
        unsigned int handle_bytes; /* Size of f_handle [in, out] */ 
        int   handle_type; /* Handle type [out] */ 
        unsigned char f_handle[0]; /* File identifier (sized by 
                caller) [out] */ 
       }; 

高級語言可以讓你把你的聲明/定義幾乎任何地方(如C++或Python),但不幸的是,C是由編譯頂部至底部

+1

那麼,根據我引用的文檔,這些頭文件應該有他們的聲明 –

+0

據我記得,即使你有一個頭文件包括你仍然必須做一個'typedef結構structname;'後面跟'structname實例'在使用之前 –

+1

你絕對不必這樣做。 –

相關問題