Gcc compiles the c source file into the so dynamic link library and reports an error /mingw32/bin/ld.exe: cannot find -lpcap

Recently, when working on a project, it is necessary to capture the data packets of the network card and then parse and extract useful field values. Since the previous projects were all developed at the application layer, they were based on Java code, but now it is necessary to capture and analyze packets, which obviously cannot be realized with Java code. After thinking, I decided to use c++ for development, and then compile it into a Java executable file. Just do it!

First of all, since I haven't learned c++, I can only find ready-made code to modify.

After finishing the code, there is no problem in compiling and executing, and then start compiling the so file.

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

Good guy! First, notice the undefined reference to `ntohs@4' error. Everyone should have encountered this problem when compiling in editors such as vc++. The reason for the error is that the necessary link library -lwsock32 is missing, and this attribute is re-executed.

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

The reason for the problem this time is that the mentioned functions are only defined in the referenced header file, but not implemented, and the method needs to be exported (because I have little knowledge and have never played C++, so I don’t understand how to operate it. So you can check how this is achieved). The method I use is to use the -lpcap option after the compile statement.

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

Note that a critical error has occurred! The error is actually very clear, the lpcap library file cannot be found.

According to the blog post: MinGW compiled with GCC, ld.exe: cannot find -ladvapi32_xuzonghao's blog-CSDN blog

You need to put the corresponding library file in the specified directory, or download WpdPack from the Internet when you don’t know which directory to put it in, find the libwpcap.a file in its lib directory after decompression, and add the copy path to the above compilation command to replace -lpcap.

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

Similarly, if it is not -lpcap, but other library files are wrong, the above method is also applicable!

Guess you like

Origin blog.csdn.net/py_123456/article/details/124712359
Recommended