简体   繁体   English

使用共享库时对函数的 C++ 代码未定义引用

[英]C++ code undefined reference to function when using shared library

编译器输出

I am trying to compile a simple c++ program that calls some functions from the NDPI C library.我正在尝试编译一个简单的 C++ 程序,该程序从 NDPI C 库中调用一些函数。 When compiling using the shared library (.so file) it can't find some of the functions in the library (like set_ndpi_flow_malloc) but seems to be able to find another function called set_ndpi_malloc.使用共享库(.so 文件)进行编译时,它无法在库中找到某些函数(如 set_ndpi_flow_malloc),但似乎能够找到另一个名为 set_ndpi_malloc 的函数。

Note: I believe the functions it can't find were newly added to the library when it updated from version 1.8 to 2.0, but the library files I am using were all compiled from source of version 2.0.注意:我相信找不到的函数是在从1.8版本更新到2.0版本时新添加到库中的,但我使用的库文件都是从2.0版本的源代码编译而来的。

Strangely when I compile using the static library (.a) it has no problem finding these functions.奇怪的是,当我使用静态库 (.a) 进行编译时,找到这些函数没有问题。

See the image linked above to see the compiler output each time and the code.查看上面链接的图像以查看每次的编译器输出和代码。 The two compile attempts were made within a minute of each other and nothing was changed except for one line in the makefile to compile with the static library instead of the shared library.两次编译尝试都在一分钟之内进行,除了 makefile 中的一行使用静态库而不是共享库进行编译外,没有任何更改。

Update更新

This has been identified as an issue with the library not exporting all the symbols.这已被确定为库未导出所有符号的问题。

See the issue on github here: https://github.com/ntop/nDPI/issues/459在此处查看 github 上的问题: https : //github.com/ntop/nDPI/issues/459

I made some effort to reproduce your steps.我做了一些努力来重现您的步骤。

  1. Clone (download/unpack) the library source from their Github从他们的 Github克隆(下载/解压)库源
  2. Enter the source directory of the library进入库的源目录
  3. ./autogen.sh (as per README.md , also: libpcap-dev package is needed to be installed in the system) ./autogen.sh (根据README.md ,另外:需要在系统中安装libpcap-dev包)
  4. ./configure (as per README.md ) ./configure (根据README.md
  5. make (as per README.md ) make (根据README.md
  6. Create a file in the home directory called test.cpp and place your code there (however, I think that include statement shall be reworded as simply #include "ndpi_api.h" )在主目录中创建一个名为test.cpp的文件并将您的代码放在那里(但是,我认为 include 语句应改写为简单的#include "ndpi_api.h"
  7. g++ -c -O3 -Wall -I ./src/include ~/test.cpp (as per your wish) g++ -c -O3 -Wall -I ./src/include ~/test.cpp (根据您的意愿)
  8. g++ -O3 -Wall -o test test.o -L ./lib -lndpi (as per your wish) g++ -O3 -Wall -o test test.o -L ./lib -lndpi (根据您的意愿)

So, indeed, there are undefined symbols in this case.因此,确实在这种情况下存在未定义的符号。 I used nm -gC ./lib/libndpi.so to read the symbol list and didn't find set_ndpi_flow_malloc and set_ndpi_flow_free .我使用nm -gC ./lib/libndpi.so读取符号列表并没有找到set_ndpi_flow_mallocset_ndpi_flow_free Not mentioned in either way.没有以任何方式提及。

I then inspected ./src/include/ndpi_api.h and ./src/lib/ndpi_main.c and couldn't wrap my head around what might have been wrong there.然后我检查了./src/include/ndpi_api.h./src/lib/ndpi_main.c并且无法理解那里可能有什么问题。 Everything seemed to be OK since these two functions were implemented and defined similar to their counterparts without _flow_ .一切似乎都很好,因为这两个函数的实现和定义与没有_flow_的对应函数类似。 Nevertheless, the difference certainly exists and could be observed in ./libndpi.sym file.尽管如此,差异肯定存在并且可以在./libndpi.sym文件中观察到。 As you might see, set_ndpi_malloc and set_ndpi_free are listed there whilst their _flow_ counterparts are not.如您所见, set_ndpi_mallocset_ndpi_free列在那里,而它们的_flow_对应项没有。

So, I deleted the library source and followed the steps 1 and 2 one more time.因此,我删除了库源并再次执行了步骤12 Then I added these two lines to ./libndpi.sym just after non- _flow_ function names:然后我将这两行添加到./libndpi.sym在非_flow_函数名称之后:

set_ndpi_flow_malloc
set_ndpi_flow_free

Next I had to proceed with the steps 3 - 8 and the last one turned to be successful.接下来,我必须继续执行第3 - 8步,最后一个成功了。 Also, nm -gC ./lib/libndpi.so shows the symbols of interest just fine.此外, nm -gC ./lib/libndpi.so显示感兴趣的符号就好了。

I may suppose that forgetting to add the symbol names to .sym file is some sort of mistake.我可能认为忘记将符号名称添加到.sym文件是某种错误。 Here is the commit by Vito Piserchia .这是Vito Piserchia提交 Perhaps it would be better if you contacted that person or, maybe, lucaderi , who makes the impression of an avid contributor, to tell them about the issue with the symbols.也许最好联系那个人,或者联系lucaderi ,他给人的印象是一个狂热的贡献者,告诉他们符号的问题。 As you may learn from libndpi.sym commit history , they often "add missing symbols", etc.正如您可以从libndpi.sym提交历史中了解到的,它们经常“添加缺少的符号”等。

That's it.就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM