我无法理解 C 头文件和源文件。我有:
某事.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...");
}
某事.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 some.c
但这不起作用,我尝试单独编译 gcc -c main.c
和 gcc -c Something.c
那么 main.c 可以工作,但 something.c 则不能。
我实际上正在尝试用 Something.c 创建一个库,但由于我什至无法将其编译为目标代码,所以我不知道该怎么做。我猜 something.h 中的结构类型和 typedef 有问题,但我不知道是什么......
最佳答案
在 header 中,函数 whereispoint()
被声明为返回一个 struct point*
(typedef p
),但定义返回一个结构点
,而不是一个指针。
就我个人而言,我发现 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 ??;
}
关于c - 制作库: conflicting types in header and source file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182604/