2012-04-04 68 views
1

我想寫一個包裝MPI的框架庫。結構在頭編譯錯誤

我有一個框架調用afw.h的頭文件和一個名爲afw.c的框架的實現文件。

我希望能夠通過在應用程序代碼中執行#include "afw.h"來編寫使用框架的應用程序代碼。

afw.h的摘錄:

#ifndef AFW_H 
#define AFW_H 

#include <mpi.h> 

struct ReqStruct 
{ 
    MPI_Request req; 
}; 

ReqStruct RecvAsynch(float *recvbuf, FILE *fp); 
int RecvTest(ReqStruct areq); 

我在afw.c(在這種情況下的MPI編譯包裝器使用PGC的下面)提供了RecvAsynch一個實現其#includes afw.h

當我編譯使用mpicc

mpicc -c afw.c -o afw.o 

我得到:

PGC-S-0040-Illegal use of symbol, ReqStruct (./afw.h: 69) 
PGC-W-0156-Type not specified, 'int' assumed (./afw.h: 69) 
PGC-S-0040-Illegal use of symbol, ReqStruct (./afw.h: 71) 
PGC-W-0156-Type not specified, 'int' assumed (./afw.h: 71) 

和類似的錯誤,無論ReqStructafw.c

任何想法,我做錯了使用?

回答

5

您定義了一個struct ReqStruct而不是ReqStruct,這些不是一回事。

無論是功能更改爲

struct ReqStruct RecvAsynch(float *recvbuf, FILE *fp); 

或使用的typedef:

typedef struct ReqStruct ReqStruct; 
+0

+1打我一毫秒 – Anonymous 2012-04-04 14:40:01

+0

是的,當然,謝謝你。我以爲在寫作之前我已經打了折扣,但顯然不是! – 2012-04-04 14:46:53

4

在C++中,該序列:

struct ReqStruct 
{ 
    MPI_Request req; 
}; 

定義類型ReqStruct,你可以在使用函數聲明。

在C中,它沒有(它定義了一個你可以使用的類型struct ReqStruct);你需要添加一個typedef如:

typedef struct ReqStruct 
{ 
    MPI_Request req; 
} ReqStruct; 

是的,struct標籤可以是相同typedef名。或者您可以在任何地方使用struct ReqStruct來代替ReqStruct;我會優先使用typedef

+0

感謝關於C/C++差異的有用評論,這裏有人先學習C++的經典案例... – 2012-04-04 14:48:02