如何在原生 Node 插件中成功链接 Flex、Bison 和 Node.js

How can I successfully link Flex, Bison, and Node.js in a native Node addon?

本文关键字:Node Flex Bison js 链接 原生 插件 成功      更新时间:2023-10-16

我正在尝试使用Flex/Bison编写本机Node.js解析器。插件有三个部分:词法分析器、解析器和 node/v8 接口。词法分析器和解析器在开始时运行良好(我使用 g++ 编译词法分析器/解析器,并包含必要的词法分析器函数,如下所示:http://tldp.org/HOWTO/Lex-YACC-HOWTO-5.html)。当我将 node/v8 部分添加到混合中时,我最终得到了词法分析器代码中定义的函数的缺少符号错误。

以下是文件结构的概述:

File A: Lexer file, automatically generated by flex from the lexer rules I wrote.
File B: The parser file, includes the extern "C" { [lexer functions] } and 
        a function called parse(std::string). parse() calls some yy_functions, 
        which are defined in the lexer, and returns a c++ data structure 
        representing the parsed data.
File C: This is the node/v8 part. There is a header file with the prototype for 
        B's parse() function. When JS calls a certain function, the data is 
        converted to a c++ string and passed to the parse() function in File B.

当我运行一个简单的测试来解析JS字符串时,我收到一个错误,说

node: symbol lookup error: /home/monty/projects/flex_bison/GetObj_Request_Parser/build/default/GetObjParser.node: undefined symbol: yy_scan_string

注意:我正在使用node-waf来构建插件,并且我已经将flex和bison生成的文件从[FILE].c重命名为[FILE].cc,以便构建脚本拾取它们。

如果你想看到一些代码,你可以在这里找到它:https://github.com/IDX-io/GetObj_Request_Parser(在src/buildstuffs目录中查看)。

File A = src/buildstuffs/lex.yy.c[c]
File B = src/buildstuffs/geto.tab.c[c]
File C = src/buildstuffs/GetObjParser.cc
Test = test.js

谢谢!

[编辑] 与其使用两个不同的编译器并弄乱 node-waf 构建脚本(我的解决方案),不如使用 flex++ 和 bison++ 来生成C++。

文件重命名为 .cc 通常会导致默认的 make 规则使用 C++ 而不是 C 构建。这可能意味着lex.yy.c文件中的函数链接错误。我认为您需要找出另一种方法,使节点构建系统识别您的 .c 文件并使用 c,而不是 c++ 正确构建它们

删除geto.y文件顶部的extern "C"。 弹性函数未通过 C 链接导出。