栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

E:CprogramTTL>g++ ttl.cpp -L.ttl.so -o main
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x1c): undefined reference to `ntohs@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x35): undefined reference to `ntohs@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x5d): undefined reference to `ntohl@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x71): undefined reference to `ntohl@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x89): undefined reference to `ntohs@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0xa2): undefined reference to `ntohs@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0xc5): undefined reference to `ntohs@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x31c): undefined reference to `ntohs@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x351): undefined reference to `ntohs@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x3bb): more undefined references to `ntohs@4' follow
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x450): undefined reference to `inet_ntoa@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x471): undefined reference to `inet_ntoa@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x586): undefined reference to `ntohs@4'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x76b): undefined reference to `pcap_findalldevs'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x870): undefined reference to `pcap_freealldevs'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x8d0): undefined reference to `pcap_open_live'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x901): undefined reference to `pcap_freealldevs'
C:Users14714AppDataLocalTempccrvoQSP.o:ttl.cpp:(.text+0x92c): undefined reference to `pcap_freealldevs'
C:Users14714AppDataLocalTempccrvoQSP.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:CprogramTTL>g++ ttl.cpp -lwsock32 -fPIC -shared -o ttl.so
C:Users14714AppDataLocalTempccydjmLD.o:ttl.cpp:(.text+0x76b): undefined reference to `pcap_findalldevs'
C:Users14714AppDataLocalTempccydjmLD.o:ttl.cpp:(.text+0x870): undefined reference to `pcap_freealldevs'
C:Users14714AppDataLocalTempccydjmLD.o:ttl.cpp:(.text+0x8d0): undefined reference to `pcap_open_live'
C:Users14714AppDataLocalTempccydjmLD.o:ttl.cpp:(.text+0x901): undefined reference to `pcap_freealldevs'
C:Users14714AppDataLocalTempccydjmLD.o:ttl.cpp:(.text+0x92c): undefined reference to `pcap_freealldevs'
C:Users14714AppDataLocalTempccydjmLD.o:ttl.cpp:(.text+0x985): undefined reference to `pcap_loop'
collect2.exe: error: ld returned 1 exit status

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

E:CprogramTTL>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 StudioWpdPackLiblibwpcap.a"

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879934.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号