簡體   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