error: unknown type name ‘Point’

error: unknown type name ‘Point’

当我尝试使用以下命令在 64 位 Linux 下编译 C 文件时:

gcc -o strain_from_faults_new strain_from_faults_new.c gen.h eqf.h

我遇到了以下错误:

eqf.h:16:3: error: unknown type name ‘Point’
   Point loc; 
   ^
eqf.h:32:3: error: unknown type name ‘Point’
   Point p[33]; 
   ^
eqf.h:44:3: error: unknown type name ‘Point’
   Point p[33]; 
   ^
eqf.h:61:3: error: unknown type name ‘Point’
   Point xy[NSEG]; 
   ^

似乎类型 'Point' 没有定义,但它确实在文件中定义了

gen.h

struct point {
  double x;
  double y;
 };
typedef struct point Point;

不知道为什么会出现这样的错误,因为这个C文件在其他电脑上是可以编译的。这是 32 位-64 位兼容问题吗? 任何评论将不胜感激!

根据我在 eqf.h 文件中 #include "gen.h" 的评论,但效果不佳。由于C文件strain_from_faults_new.c中已经包含了两个头文件gen.h eqf.h,编译器出现错误:

In file included from eqf.h:12:0,
                 from strain_from_faults_new.c:6:
gen.h:11:8: error: redefinition of ‘struct point’
 struct point {
        ^
In file included from strain_from_faults_new.c:5:0:
gen.h:11:8: note: originally defined here
 struct point {
        ^

有人有什么想法吗?

好了,下面是全部代码:

gen.h

#define MAX_NAME 50
#define MAXNAME 50
#define EARTH_RADIUS 6371.0
#define MU (3.0e11)
#define PI 3.1415927
#define D2R 0.0174532
#define SUNIT (1.0e-9)
#define KAPA 0.25
#include <stdio.h>

struct point {
  double x;
  double y;
};

typedef struct point Point;

extern int enclose(Point *, int, Point);
extern int enclose2(Point *, int, Point, Point);
extern int lenclose(Point *p, int n, Point p1);
extern int lenclose2(Point *p, int n, Point p1, Point p2);
extern double azimuth(double, double);
extern double atan2d(double, double);
extern double sind(double);
extern double cosd(double);
extern double dsign(double);
extern double dmax(double, double);
extern double dmin(double, double);
extern double diag_len(Point p[]);
extern void make_tensor(double z[][3],  double, double, double);
extern void read_a_line(FILE *);
extern void strip_a_line(FILE *);
extern char *get_a_string(FILE *f, char* str);
extern int line_to_string(FILE *f, char* str);
extern int in_region(int node[], int N, int p);
extern double convert_date_to_year(int);

eqf.h

#define MAXP 10
#define MAXN 400
#define MAXB 1000
#define MAXF 37000
#define MAXE 1000
#define TYPE_F 1
#define TYPE_E 0
#define T 149 
#define DEPTH 15.0 
#define NSEG 2074
#include <stdio.h>
#include "gen.h"


struct earthquake {
  int  date;
  Point loc; 
  double mom[3][3];
  double m0;
  int type;
};
struct merr {
  double m[3][3];
};

struct mom {
  double m0;
  double mxx;
  double myy;
  double mxy;
};
struct region {
  Point p[33]; 
  double area; 
  double t0; 
  struct earthquake event[50];
  int ne; 
  struct earthquake *e[100];
  double strainrate[9]; 
  double strain[9]; 
  double strainr[9]; 
  double strain0[9]; 
};
 struct regionf {
   Point p[33]; 
   double area; 
   int ix;
   int iy;
   int ia;
};

 struct fault {
   double strike;
   double rake;
   double dip;
   double rate;
   double ratemin;
   double ratemax;
   char name[50];
   int  date;
   int n; 
   Point xy[NSEG]; 
 };

 extern void test();
 extern void read_faults(FILE *inf, struct fault *f);
 extern void test_mom();
 extern void print_event (FILE *inf, struct earthquake e);
 extern int bydate(struct earthquake *, struct earthquake *);
 extern void sort_event(FILE *);
 extern int write_mom_tensor(struct earthquake *, int *, double);
 extern void fill_regionf(struct regionf *, int *);
 extern void fill_region(struct region *, int *);
 extern void print_mom_in_region(struct region *b, int nb);
 extern double strain_rate(double ex, double ey, double exy);
 extern double strain_rate_var(double ex, double ey, double exy, double dex, double dey, double dexy, double cxy, double cxxy, double cyxy);

strain_from_faults_new.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "gen.h"
#include "eqf.h"

void deal_with_options(int, char **);

int no_faults, n_events;
double sdepth;
double ub = 0.01;
char faultin[25];
FILE *strain1;

int main(int argc, char *argv[]) {
  Point p[MAXN], p1[10];
  struct fault f[MAXF];
  struct regionf b[MAXB];
  double  plat=0.0, plon=0.0, prat=0.0;
  int n=0, i, j, nb=0, np, num, ia, k, l, nrect=0, nfree=0, ix=0, iy=0;
  FILE *faults, *strain0;

  sdepth = DEPTH*1.0e5;
  strcpy(faultin, "fault.in");

  deal_with_options(argc, argv);

  if( (faults = fopen(faultin, "r")) == NULL ) {
     printf("Error: File %s does not exist.\n", faultin);
     exit(-1);
  }
  if( (strain0 = fopen("spline_fit.dat", "r")) == NULL) {
     printf("Error: File spline_fit.dat cannot open\n");
     exit(-1);
  }
  if( (strain1 = fopen("strain_from_faults.dat", "w")) == NULL) {
     printf("Error: File strain_from_faults.dat cannot open\n");
     exit(-1);
  }

  fscanf(strain0, "%d%d%d", &ix, &iy, &num);
  fscanf(strain0, "%d", &nfree);
  fprintf(strain1, "%10d %10d %10d\n", ix, iy, num);
  fprintf(strain1, "%10d\n", nfree);
  for(i=nfree; i<num; i++) {
    if(fscanf(strain0, "%d%lf%lf%lf",  &np, &plat, &plon, &prat) != 4) {
      printf("Error: format error in spline_fit.dat\n");
      exit(1);
     }
     fprintf(strain1, "%10d %10.3f %10.3f %12.6e\n", np, plat, plon, prat);

     }
     fscanf(strain0, "%d", &nrect);
     fprintf(strain1, "%10d\n", nrect);
     close(strain0);

     for(i = 0; i < nrect; i += MAXB) {
       nb = i;
       printf("Working on regions %d to %d ...\n", i, i+MAXB);
       fill_regionf(b, &nb);
       read_clip_faults(faults, f, b, nb);   
     }
     return 0;
 }


 int read_clip_faults(FILE *inf, struct fault *f, struct regionf *b, int nb) {
 int i, j, k, l, ib, jj, kk, ii, n, nf;
 double x, y, dx, dy, d, mt[3][3], c, theta;
 double sd, md, strainrate[9];
 double clip_seg = 0.05;
 struct mom m1;
 struct mom m0;
 struct merr m2;
 struct fault f0;
 Point p[100], p0;

 i=0;
 fseek(inf, 0, SEEK_SET);
 while( (k = fscanf(inf, "%d%lf%lf%lf%lf%lf%s",
        &n, &(f[i].rake), &(f[i].dip), &(f[i].ratemin),
         &(f[i].rate), &(f[i].ratemax), f[i].name)) == 7) {
     printf("Fault no. %d, %s\n", i, f[i].name);

   f[i].rate = f[i].rate;
   f[i].ratemin = f[i].ratemin;
   f[i].ratemax = f[i].ratemax;
   f[i].n = 0;

   for(j=0; j<n; j++) {
     if( (l = fscanf(inf, "%lf%lf", &(p[j].y), &(p[j].x))) != 2) {
       printf("Error at fault %s, line %d in fault.in\n", f[i].name, j);
       exit(1);
     }
   }

   jj=0;
  for(j=0; j<n-1; j++) {
k = 0;
while( k < nb && (l = enclose(b[k].p, 33, p[j])) == 0)
...  

根据 Mike Kinghan 的评论,我添加了

#ifndef GEN_H
#define GEN_H

#endif

分别在gen.h文件的头部和尾部。但效果不佳,这里是错误:

/tmp/ccteT25z.o: In function `read_clip_faults':
strain_from_faults_new.c:(.text+0x6a4): undefined reference to `enclose'
strain_from_faults_new.c:(.text+0x7c5): undefined reference to `enclose'
strain_from_faults_new.c:(.text+0xa14): undefined reference to `enclose'
strain_from_faults_new.c:(.text+0xa7b): undefined reference to `dsign'
strain_from_faults_new.c:(.text+0xaae): undefined reference to `dsign'
strain_from_faults_new.c:(.text+0xad9): undefined reference to `dsign'
strain_from_faults_new.c:(.text+0xb5a): undefined reference to `dsign'
...

您需要 #include "gen.h" 在 eqf.h

您需要在eqf.h中添加#include "gen.h"并使用以下命令编译您的程序:
gcc -o gen.h eqf.h strain_from_faults_new strain_from_faults_new.c;

编译器会根据您的文件输入顺序编译您的程序文件。

如果你读到这篇文章:

In file included from eqf.h:12:0,
                 from strain_from_faults_new.c:6:
gen.h:11:8: error: redefinition of ‘struct point’
 struct point {
        ^
In file included from strain_from_faults_new.c:5:0:
gen.h:11:8: note: originally defined here
 struct point {
        ^

它告诉你 struct point 在同一个翻译中被定义了两次 单位,strain_from_faults_new.c,这是一个错误。

您在第 11 行的 gen.h 中定义了 struct point

您在 strain_from_faults_new.c

的第 5 行包含了 gen.h

您还在 eqf.h 的第 12 行包含了 gen.h

最后你在 strain_from_faults_new.c 的第 6 行包含了 eqf.h, 从而再次包含 struct point 的定义。

为避免此类问题,请在您编写的每个 header 中使用 header guards a.k.a include guards, 例如

gen.h

#ifndef GEN_H
#define GEN_H

    struct point {
      double x;
      double y;
     };
    typedef struct point Point;

#endif

这使得在翻译单元中多次包含 header 是无害的。

并且,不要在编译器命令行中传递 header 文件:

gcc -o strain_from_faults_new strain_from_faults_new.c gen.h eqf.h
                                                       ^^^^^ ^^^^^

A header 文件通过在 .c 文件中被 #include 编译,这 将其复制到翻译单元中。