繁体   English   中英

制作库:标题和源文件中的冲突类型

[英]making library: conflicting types in header and source file

我无法理解c头文件和源文件。 我有:

something.c

#include <stdio.h>
#include "something.h"

typedef struct {
    int row, col;
} point;

point
whereispoint(point **matrix, int rows, int cols)
{
   ..... Does something ....
   printf("something...");
}

something.h

typedef struct point * p;

p whereispoint(p **matrix, int rows, int cols);

main.c中

#include <stdio.h>
#include "something.h"

int
main(void)
{
   int row, col;
   p **matrix=malloc(bla, bla);
   .....Something.....
   p=whereispoint(matrix, row, col);
   return 0;
}

现在,当我实际上不知道如何编译这个...我尝试了gcc -c main.c something.c但是这不起作用,我试图单独编译gcc -c main.cgcc -c something.c然后main.c工作,但something.c没有。

我实际上是试图用something.c创建一个库,但由于我甚至无法将其编译为目标代码,所以我不知道该怎么做。 我想有一些错误的结构类型,且它在something.h typedef的,但我想不出什么...

在头文件中,函数whereispoint()声明为返回struct point*typedef p ),但定义返回struct point ,而不是指针。

就个人而言,我发现typedef指针令人困惑,并认为如果*用于表示指针,代码中的代码更清晰:

/* header */
typedef struct point point_t;

point_t* whereispoint(point_t** matrix, int rows, int cols);

/* .c */
struct point {
    int row, col;
};

point_t* whereispoint(point_t** matrix, int rows, int cols)
{
   ..... Does something ....
   printf("something...");
   return ??;
}

下列:

typedef struct {
  int row, col;
} point;

typedef类型点的无名结构。

在something.h中,然后键入“struct point”(未定义的结构类型引用)到“* p”。

通常,您应该在头文件中定义所有“接口类型”,而不是尝试隐藏实现(C需要知道实现以访问任何内容)。

尝试在something.h中做这样的事情:

typedef struct point {
  int row, col;
} *p;

或者,如果您不确定typedef是如何工作的,只需执行以下操作:

 struct point {
  int row, col;
}

那声明了一个struct类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM