2
我有几个源代码文件,如hashtable.c等。主要的问题是,当我写我的main.c这样:在yacc中声明全局变量
#include "tokens.h"
#include <stdio.h>
void yyerror(char *errorMsg)
{
fprintf(stderr, "%s\n", errorMsg);
}
main()
{
yyparse();
hsh = createHashtable();
}
而且在我的yacc文件(parser.y)的顶部,我想declear哈希表这样:
%{
#include <stdio.h>
#include "tokens.h"
#include "ast.c"
struct hashtable *hsh;
.............................
..............................
但是我得到这个错误。
main.c: In function ‘main’:
main.c:24: error: ‘hsh’ undeclared (first use in this function)
main.c:24: error: (Each undeclared identifier is reported only once
main.c:24: error: for each function it appears in.)
make: *** [main.o] Error 1
我比较幼稚,而涉及到C语言编程,任何援助将不胜感激
你能成为一个更具体一点,我很困惑 – iva123 2009-12-27 14:28:03
您需要在main.c中声明符号'hsh'是在其他编译单元中定义的。这是用'extern'修饰符完成的。请参阅http://wiki.answers.com/Q/What_is_the_use_of_extern_in_C – mrkj 2009-12-27 14:40:18
如果用户将extern声明放在头文件中,并将该头文件包含在语法和主程序中,会更好。这两个文件中的一个实际上应该定义变量 - 在语法中这样做很好。 – 2009-12-27 17:09:18