lex.yy.c 未与外部函数链接

问题描述 投票:0回答:1

我正在编写一个 SQL 解析器。

SQLSelectParser.l
是lex文件,使用lex编译器成功生成
lex.yy.c
文件。在
SQLSelectParser.l
文件中,我调用
Ast.h/c
文件中定义的外部函数。我在
SQLSelectParser.l
的 %{ %} 部分中 #include Ast.h。

我单独编译src文件没有任何问题。

lex SQLSelectParser.l
gcc -g -c lex.yy.c -o lex.yy.o
gcc -g -c Ast.c -o Ast.o 
gcc -g lex.yy.o Ast.o -o exe -lfl   << but get linker error.

函数

ast_add_child
在Ast.h中声明并在Ast.c中定义。我无法理解为什么会出现链接器错误。 这是链接 Flex 生成的 src 文件时常见的问题吗?

/usr/bin/ld: lex.yy.o: in function `create_q_parse_columns':
/home/vm/RDBMSImplementation/Parsers/SQLSelectParser.l:405: undefined reference to `ast_add_child'
/usr/bin/ld: /home/vm/RDBMSImplementation/Parsers/SQLSelectParser.l:417: undefined reference to `ast_add_child'
collect2: error: ld returned 1 exit status

这里是使用 ast_add_child() 的 SQLSelectParser.l 文件的链接。

Ast.h在这里

Ast.cpp在这里

c linker lex
1个回答
0
投票

gcc -g -c Ast.c -o Ast.o

规则 1:显示实际命令而不是虚构命令。

您没有

Ast.c
,您有
Ast.cpp
,因此您显示的命令一定不是您实际运行的命令。

当您使用

gcc -c Ast.cpp
编译代码时,它会被编译为
C++
。如果您运行
nm Ast.o | grep ast_add_child
,您会看到类似
_Z13ast_add_childv
的内容。

规则2:学会钓鱼。当您收到未定义符号错误时,请使用

nm
(如上所述)来确认您缺少的符号事实上已在您认为的位置定义。

© www.soinside.com 2019 - 2024. All rights reserved.