2014-10-27 178 views
1

我想在c中編譯這段代碼。首先,我有此結構在一個單獨的源文件使用它像一個「類」(dir.h)傳遞結構指針作爲參數

//Structure 
typedef struct s_dirobject { 
    int noteid; 
    char title[20]; 
    int bytes; 
    char head[20]; 
    bool is_dir; 
    struct s_dirobject* next; 
} dirobject; 

//Ops 
void add_dirobject(dirobject* myDirobject,int num_dirobject, char title[20], int is_dir,  int bytes, char head[20]); 
int get_dirobject_noteid(dirobject* myDirobject,int num_note); 
char* get_dirobject_title(dirobject* myDirobject,int num_note); 
int get_dirobject_bytes(dirobject* myDirobject,int num_note); 
char* get_dirobject_head(dirobject* myDirobject,int num_note); 
bool isdir(dirobject* myDirobject, int num_note); 
int get_dirobjects_len(dirobject* myDirobject); 
void clear_dir(dirobject* myDirobject); 
void init_dir(dirobject* myDirobject); 

在第二位的我有通訊科源從遠程檢索目錄的內容文件系統,並且填充對象(comms.c)

#include "notebook.h" 
#include "dir.h" 

dirobject* temporaldirobjects; 

... 

init_dir(temporaldirobjects); 
while(cond) { 
    //Add retrieved item to the directory 
    add_dirobject(temporaldirobjects, index, title, is_dir, bytes, ""); 
} 
//When done retrieving the contents from the source i do instantiate the notebook_window 
notebook_init(source, path, temporaldirobjects); 

最後,我的筆記本窗口界面看起來是這樣的。 (notebook.h)

#include "dir.h" 
void notebook_init(char* source, char* path, dirobject* contents); 
void notebook_deinit(); 

及其履行情況(notebook.c)

void notebook_init(char* source, char* path, dirobject* contents) { 

    // Fill the vars 
    this_window_source=source; 
    this_window_path=path; 
    this_window_dirobjects=contents; 

    ... 
} 

當我按原樣編譯這段代碼,我得到的錯誤,指出

../src/dir.h:13:16: error: redefinition of 'struct s_dirobject' 
In file included from ../src/notebook.h:12:0, 
       from ../src/comms.c:25: 
../src/dir.h:13:16: note: originally defined here 
In file included from ../src/comms.c:27:0: 
../src/dir.h:20:3: error: conflicting types for 'dirobject' 
In file included from ../src/notebook.h:12:0, 
       from ../src/comms.c:25: 
../src/dir.h:20:3: note: previous declaration of 'dirobject' was here 
In file included from ../src/comms.c:27:0: 
../src/dir.h:23:6: error: conflicting types for 'add_dirobject' 
In file included from ../src/notebook.h:12:0, 
       from ../src/comms.c:25: 
../src/dir.h:23:6: note: previous declaration of 'add_dirobject' was here 
... 

通信庫包括dir.h和notebook.h,而notebook.h也是如此。 如果我刪除包括notebook.h我得到這個其他錯誤:

In file included from ../src/comms.c:25:0: 
../src/notebook.h:14:46: error: unknown type name 'dirobject' 

我怎麼能達致這?我想保留它作爲乾淨的代碼,因爲我可以。

回答

2

您包括在文件comms.c

#include "notebook.h" 
#include "dir.h" 

notebook.h反過來兩個頭部包括頭dir.h

#include "dir.h" 
void notebook_init(char* source, char* path, dirobject* contents); 
void notebook_deinit(); 

作爲產生的結構以相同的編譯單元中定義的兩倍。您必須規定每個編譯單元中只包含一次每個標題。要做到這一點,你必須提供標題的方塊。例如

#ifndef DIR_H 
#define DIR_H 

//Structure 
typedef struct s_dirobject { 
    int noteid; 
    char title[20]; 
    int bytes; 
    char head[20]; 
    bool is_dir; 
    struct s_dirobject* next; 
} dirobject; 

//... 

#endif 

或者,如果編譯器支持#pragme一次,那麼你可以使用它。

1

通常情況下,多個聲明在c中很好但是多個定義不是。在您的代碼中,您包含多次導致重新定義struct s_dirobjectdir.h。閱讀關於「標題警衛」或「包括警衛」的內容。 here。希望通過重新定義解決您的主要問題。