节点中的未定义符号.js C++Linux下的插件,为什么

undefined symbol in node.js C++ addon under Linux, why?

本文关键字:插件 C++Linux 为什么 js 未定义 符号 节点      更新时间:2023-09-26

我是C++node.js编写插件的新手。

这是我的模块:

$ npm install simpleini

它基于 miniini-0.9。我的消息来源正在src/simpleIni.cc.我已经在Windows,OS X,Linux(Debian)下尝试过这个模块。它在Windows和OS X下运行良好。

但是当我在 Linux 中运行时,似乎:

node: symbol lookup err: .../simpleIni.node: undefined symbol: _ZNK10INISection10ReadStringEPKcRS1_

为什么?

经过一番搜索,这就是我发现的。

行为:

$ nm -C build/Release/simpleIni.node  | grep ReadString
                 U INISection::ReadString(char const*, char const*&) const
00000000000032b0 t INISection::ReadString(char const*, char const*&) const [clone .part.11]
0000000000003f80 W INISection::ReadString(std::string const&, std::string&) const
00000000000081a0 T INISection::ReadStrings(char const*, char const**, unsigned int) const
0000000000008f20 T INISection::ReadStrings(std::string const&, std::vector<std::string, std::allocator<std::string> >&) const
000000000000bca0 r INISection::ReadString(char const*, char const*&) const::__PRETTY_FUNCTION__
000000000000b8a0 r INISection::ReadStrings(char const*, char const**, unsigned int) const::__PRETTY_FUNCTION__

所以关键是

                 U INISection::ReadString(char const*, char const*&) const

显示为未定义...虽然还有另一个符号的副本

00000000000032b0 t INISection::ReadString(char const*, char const*&) const [clone .part.11]

现在我们可以在你的代码中搜索这个方法:

at src/miniini-0.9/miniini/include/inisection.h

class INISection
{
...
        bool ReadString(const char * const name, const char * & out) const;
}
在src/

miniini-0.9/miniini/src/inisection中.cpp

inline bool INISection::ReadString(const char * name, const char * & out) const
{
...
}

现在关键是这个内联。根据C++常见问题解答如何告诉编译器使成员函数内联?

您(几乎总是)将定义({...}部分)的原因 头文件中的内联函数是为了避免"未解析的外部" 来自链接器的错误。如果您将内联 函数在.cpp文件中的定义以及是否调用了该函数 从其他一些.cpp文件。

从启动部分中删除内联.cpp重建我们可以再试一次纳米

$ nm -C build/Release/simpleIni.node  | grep ReadString
00000000000069a0 T INISection::ReadString(char const*, char const*&) const
0000000000003f70 W INISection::ReadString(std::string const&, std::string&) const
00000000000080e0 T INISection::ReadStrings(char const*, char const**, unsigned int) const
0000000000008d20 T INISection::ReadStrings(std::string const&, std::vector<std::string, std::allocator<std::string> >&) const
000000000000bc20 r INISection::ReadString(char const*, char const*&) const::__PRETTY_FUNCTION__
000000000000b7e0 r INISection::ReadStrings(char const*, char const**, unsigned int) const::__PRETTY_FUNCTION__

这次没有未定义的符号,ReadString 只出现一次。