c - 制作库: conflicting types in header and source file

标签 c compilation

我无法理解 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);

ma​​in.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.cgcc -c Something.c 那么 ma​​in.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/

相关文章:

C 函数刷新所有包含数组的缓存行

c++ - COM 端口读取 - 线程在超时发生后保持事件状态

c - 如何将 16 位数字存储在两个字节的 char 数组中?

android - ASSIMP - 安卓 NDK 工具链

visual-studio - 在没有 Visual Studio 的情况下编译 .vbproj 或 .csproj 项目文件

c# - 加快 MSBuild 构建过程

c - 我的代码有什么问题吗?解析输入文本文件 (C)

c - leetcode 38 count and say confuse me的c解法

c - 我的 pid 标识符不是预期的

perl - 如何安全地为 Perl 5.8.9 编译 Perl 5.12 模块?