简体   繁体   English

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

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

I have trouble understanding c header files and source files. 我无法理解c头文件和源文件。 I have: 我有:

something.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 something.h

typedef struct point * p;

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

main.c 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;
}

Now when I don't actually know how to compile this... I tried gcc -c main.c something.c but that doesn't work, I tried to compile separately gcc -c main.c and gcc -c something.c then main.c works but something.c does not. 现在,当我实际上不知道如何编译这个...我尝试了gcc -c main.c something.c但是这不起作用,我试图单独编译gcc -c main.cgcc -c something.c然后main.c工作,但something.c没有。

I am actually trying to make a library out of something.c but as I am not able even to compile it to object code I don't know what to do. 我实际上是试图用something.c创建一个库,但由于我甚至无法将其编译为目标代码,所以我不知道该怎么做。 I guess there is something wrong with the struct type and the typedef of it in something.h but I can't figure out what... 我想有一些错误的结构类型,且它在something.h typedef的,但我想不出什么...

In the header, the function whereispoint() is declare as returning a struct point* (the typedef p ) but the definition returns a struct point , not a pointer. 在头文件中,函数whereispoint()声明为返回struct point*typedef p ),但定义返回struct point ,而不是指针。

Personally, I find typedef pointers confusing and think it is clearer in the code if * is used to denote a pointer: 就个人而言,我发现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 ??;
}

The following: 下列:

typedef struct {
  int row, col;
} point;

typedefs a nameless struct to the type point. typedef类型点的无名结构。

In something.h, you then typedef "struct point" (an undefined struct type reference) to "*p". 在something.h中,然后键入“struct point”(未定义的结构类型引用)到“* p”。

Typically, you should define all your "interface types" in the header file, instead of trying to hide the implementation (C needs to know the implementation to access anything). 通常,您应该在头文件中定义所有“接口类型”,而不是尝试隐藏实现(C需要知道实现以访问任何内容)。

Try doing something like this in something.h: 尝试在something.h中做这样的事情:

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

Or, if you are unsure exactly how typedefs work, simply do: 或者,如果您不确定typedef是如何工作的,只需执行以下操作:

 struct point {
  int row, col;
}

That declares a struct type. 那声明了一个struct类型。

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

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