我知道有类似的帖子,但我很难掌握它的诀窍:我有一个.c和一个.h文件:
graph.c
#ifndef graph
#define graph
/*1. standard headers*/
#include <stdbool.h>
/*2. user-def. headers*/
//#include ".."
/*3. global macros*/
//#define GLOB_MACRO …
/*4. global type decls.*/
struct graph;
typedef struct graph graph_t;
graph_t* graph_create(int n);
void graph_free(graph_t *g);
void graph_insert_edge(graph_t *g, int source, int target, double weight);
void graph_remove_edge(graph_t *g, int source, int target);
bool graph_has_edge(graph_t *g, int source, int target);
double graph_get_weight(graph_t *g, int source, int target);
void graph_print(graph_t *g);
/*5. glob. var. decls.*/
//extern … glob_var;
/*6. glob. func. decls*/
//[extern] … glob_func(…);
#endifgraph_m.c
// Driver Code
int main()
{
printf("test");
return 0;
}但是我得到了这些编译器错误:
gcc -MM graph_m.c > .depend
gcc -pedantic -Wall -Wextra -Wvla -std=c11 -ggdb3 -c -o graph_m.o graph_m.c
In file included from graph_m.c:14:
graph.h:15:13: error: expected '{' before ';' token
15 | struct graph;
| ^
graph.h:16:22: warning: useless storage class specifier in empty declaration
16 | typedef struct graph graph_t;
| ^~~~~~~
graph.h:18:1: error: unknown type name 'graph_t'; use 'struct' keyword to refer to the type
18 | graph_t* graph_create(int n);
| ^~~~~~~
| struct
graph.h:19:17: error: unknown type name 'graph_t'; did you mean 'graph'?
19 | void graph_free(graph_t *g);
| ^~~~~~~
| graph
graph.h:21:24: error: unknown type name 'graph_t'; did you mean 'graph'?
21 | void graph_insert_edge(graph_t *g, int source, int target, double weight);
| ^~~~~~~
| graph
graph.h:22:24: error: unknown type name 'graph_t'; did you mean 'graph'?
22 | void graph_remove_edge(graph_t *g, int source, int target);
| ^~~~~~~
| graph
graph.h:24:21: error: unknown type name 'graph_t'; did you mean 'graph'?
24 | bool graph_has_edge(graph_t *g, int source, int target);
| ^~~~~~~
| graph
graph.h:25:25: error: unknown type name 'graph_t'; did you mean 'graph'?
25 | double graph_get_weight(graph_t *g, int source, int target);
| graph
graph_m.c:24:1: warning: unnamed struct/union that defines no instances
24 | } graph;
| ^
make: *** [<builtin>: graph_m.o] Error 1
root@8c9bf05a5390:/home/swo3/src/graph# In file included from graph_m.c:14:
graph.h:15:13: error: expected '{' before ';' token
15 | struct graph;
| ^
graph.h:16:22: warning: useless storage class specifier in empty declaration
16 | typedef struct graph graph_t;
| ^~~~~~~
graph.h:18:1: error: unknown type name 'graph_t'; use 'struct' keyword to refer to the type
18 | graph_t* graph_create(int n);
| ^~~~~~~
| struct
make: *** [<builtin>: graph_m.o] Error 1union that defines no instances?e weight);我是不是弄错命令了?我在运行中有ty胡枝子和struct,对于头文件的实际实现有不同的文件名,其中我想在后台为graph.h、graph_m.c和另一个数据结构做两个数据结构。
发布于 2022-10-14 12:16:55
宏名与其他名称发生冲突。
你先从这个开始:
#define graph然后你就有了这个
struct graph;由于宏做了直接的令牌替换,在预处理后,上面的行变成:
struct ;这是无效的。
您需要更改宏名,这样就不会发生冲突。按照惯例,宏通常是大写的,头保护宏通常以_H结尾。因此,将前两行更改为:
#ifndef GRAPH_H
#define GRAPH_H发布于 2022-10-14 12:19:05
在#define graph之后,宏处理将struct graph;替换为struct ;。
为预处理器保护使用不同的标识符,如graph_h。
https://stackoverflow.com/questions/74069027
复制相似问题