我在我的程序的整个c文件中共享全局变量和3个结构的问题很大。我知道使用全局变量可能不是最好的方法,但现在看起来似乎适合我。整个程序在头文件中共享变量和结构
出现的2个通常错误是:为变量名
未定义参考
误差预期 '=', ' ' '%3B', 'ASM' 或' 属性' 前变量名
什么,我试图做的是:
在variables.h和variables.c
定义我的全局变量定义功能file1.h,file2.h等
像:
file1.h:
#ifndef FILE1_H
#define FILE1_H
void function_file1(void);
#endif
file1.c中:
#include "file1.h"
void function_file1(void) {
//do sth
}
我定义全局变量和结构在变量.h中作为extern,然后在变量中没有extern关键字。c
然而,一遍又一遍的这样做后,我继续得到2以上errors.Is有什么我失踪?
这里是什么我做的更多信息:
variables.h:
#ifndef VARIABLES_H
#define VARIABLES_H
extern int x;
extern int y;
extern int foo = 3; // i have set value to 3 variables with extern like the foo one
/*Tooltips */
struct test_struct {
/* variables
*/
} test;
extern struct test_struct test;
#endif
functions.h:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "variables.h"
void do_sth(void) {
//do sth
}
#endif
的main.c
/* including libraries before including variables.h and functions.h */
#include "variables.h"
#include "functions.h"
........
这是基本的结构e。包含全局变量的单个variables.h文件。大约有7个functions.h文件,其中包含一些用于该程序的函数。使用此结构,编译器不会显示任何错误。
现在的问题是:如何为程序中的每个.h文件创建一个.c文件? 像variables.h和variables.c和functions.h和functions.c?
你显然没有发布带有错误的代码。我敢打赌,错误信息有一个文件名和一个行号。 – bmargulies
我之所以没有发布的原因是因为它很大......多么讽刺啊? :(我仍然高度赞赏我将如何在头文件和c文件中定义我的函数,并共享全局变量和结构。 – Rrjrjtlokrthjji
您发布的内容没有任何问题,尽管您可以尝试在前端抛出'extern'再次,在错误中寻找一个特定的文件名和行号 – bmargulies