gcc编译c源文件为so动态连接库时报错/mingw32/bin/ld.exe: cannot find -lpcap

最近,在做项目时需要抓取过网卡的数据包然后解析提取有用的字段值。由于前期项目都在应用层进行开发,所以是基于Java代码进行的,但现在要抓包分析,显然不能用Java代码实现了。经过思考,决定使用c++进行开发,然后编译为Java可执行的文件不就行了。说做就做!

首先,由于本人没有学过c++,于是只能去找现成的代码进行修改。

弄完代码,编译执行都没问题,接下开始进行so文件的编译。

E:\Cprogram\TTL>g++ ttl.cpp -L.ttl.so -o main
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x1c): undefined reference to `ntohs@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x35): undefined reference to `ntohs@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x5d): undefined reference to `ntohl@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x71): undefined reference to `ntohl@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x89): undefined reference to `ntohs@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0xa2): undefined reference to `ntohs@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0xc5): undefined reference to `ntohs@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x31c): undefined reference to `ntohs@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x351): undefined reference to `ntohs@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x3bb): more undefined references to `ntohs@4' follow
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x450): undefined reference to `inet_ntoa@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x471): undefined reference to `inet_ntoa@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x586): undefined reference to `ntohs@4'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x76b): undefined reference to `pcap_findalldevs'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x870): undefined reference to `pcap_freealldevs'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x8d0): undefined reference to `pcap_open_live'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x901): undefined reference to `pcap_freealldevs'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x92c): undefined reference to `pcap_freealldevs'
C:\Users\14714\AppData\Local\Temp\ccrvoQSP.o:ttl.cpp:(.text+0x985): undefined reference to `pcap_loop'
collect2.exe: error: ld returned 1 exit status

好家伙!首先,注意到undefined reference to `ntohs@4'错误。大家在vc++等编辑器里编译的时候应该都遇到过这个问题。错误原因是缺少了必要的链接库-lwsock32,加上该属性重新执行。

E:\Cprogram\TTL>g++ ttl.cpp -lwsock32 -fPIC -shared -o ttl.so
C:\Users\14714\AppData\Local\Temp\ccydjmLD.o:ttl.cpp:(.text+0x76b): undefined reference to `pcap_findalldevs'
C:\Users\14714\AppData\Local\Temp\ccydjmLD.o:ttl.cpp:(.text+0x870): undefined reference to `pcap_freealldevs'
C:\Users\14714\AppData\Local\Temp\ccydjmLD.o:ttl.cpp:(.text+0x8d0): undefined reference to `pcap_open_live'
C:\Users\14714\AppData\Local\Temp\ccydjmLD.o:ttl.cpp:(.text+0x901): undefined reference to `pcap_freealldevs'
C:\Users\14714\AppData\Local\Temp\ccydjmLD.o:ttl.cpp:(.text+0x92c): undefined reference to `pcap_freealldevs'
C:\Users\14714\AppData\Local\Temp\ccydjmLD.o:ttl.cpp:(.text+0x985): undefined reference to `pcap_loop'
collect2.exe: error: ld returned 1 exit status

这次问题的原因是提示的几个函数在引用的头文件中只进行了定义,而没有去实现,需要对方法进行导出(由于本人才疏学浅,也没玩过c++,所以不是很理解怎么操作,所以大家可以去查查这个怎么实现)。我所使用的方法是在编译语句后面使用-lpcap选项。

E:\Cprogram\TTL>g++ ttl.cpp -lwsock32 -fPIC -shared -o ttl.so -lpcap
d:/program files (x86)/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lpcap
collect2.exe: error: ld returned 1 exit status

注意,关键错误出现了!该错误其实讲得很明白,找不到lpcap库文件。

根据博文:MinGW使用GCC编译,出现ld.exe: cannot find -ladvapi32_xuzonghao的博客-CSDN博客

需要将对应库文件放到指定目录里就行,或者当你不知道应该放到那个目录时就在网上下载WpdPack,解压后在其lib目录里找到libwpcap.a文件,复制路径添加到上面的编译指令里去替换-lpcap。

g++ ttl.cpp -lwsock32 -fPIC -shared -o ttl.so "D:\Program Files (x86)\Microsoft Visual Studio\WpdPack\Lib\libwpcap.a"

同理,如果不是-lpcap,而是其它库文件错误,以上方法同样适用!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值