2012-04-04 93 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