问题
Looking to get the Winpcap developer pack (4.1.2) running on Windows 7 64-bit. I'm programming in C/C++ in the Netbeans IDE with the Cygwin (4.1.10) compiler. I'd like to directly pull some GPS data from UDP packets instead of using another program where I go through an intermediate step and pull them from a text file. If I can't sort this out I'm gonna try in Ubuntu next and see if I can get libpcap working there (even though Windows is preferred because of other equipment I am using). Here's where I'm at (my main resources have been gathered from Introduction to the WinPcap Networking Libraries, Using WinPcap in your programs, and Programming with pcap at tcpdump.org (note: I can't post the third link because it says my reputation needs to be higher)):
- I've installed the winpcap developers pack to C:\Users\\Documents
- I've installed the winpcap dll (though I'm not actually sure why this is necessary since all of the documentation I've seen only seems to use the .lib files)
- As instructed from the above links I've added the following settings in my project:
- For the "Include Directories and Headers" I put the path to the winpcap Include folder from the developer pack
- For "Preprocessor Definitions" I've added HAVE_REMOTE and WPCAP
- For "Additional Library Directories" I put the path to the winpcap Lib folder from the developer pack
By doing all of the above and including the pcap.h header with no additional code I can compile my program. However, the install instructions I've been referencing also state to add the following two "Additional Dependencies": winpcap.lib and Packet.lib
As soon as I do that I can't compile and get the following:
make[2]: Entering directory '/cygdrive/c/Users/<user>/Documents/NetBeansProjects/GPS_Parcer'
make[2]: *** No rule to make target 'winpcap.lib', needed by 'dist/Debug/Cygwin_4.x-Windows/gps_parcer.exe'. Stop.
make[2]: Leaving directory '/cygdrive/c/Users/<user>/Documents/NetBeansProjects/GPS_Parcer'
nbproject/Makefile-Debug.mk:61: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/<user>/Documents/NetBeansProjects/GPS_Parcer'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
Out of curiosity to see if those dependencies actually mattered I removed them and then added some basic code I copied from tcpdump.org:
#include <stdio.h>
#include <pcap.h>
int main(int argc, char *argv[])
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf); //error is here!
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
printf("Device: %s\n", dev);
return(0);
}
I get an undefined reference to 'pcap_lookupdev'
error.
I've also read that possibly Winpcap will only work with MS Visual Studio stuff so I actually don't know if that's true.
Sorry for the long write-up, but if anybody has some insights or experience with this it would be much appreciated.
回答1:
I had similar problems with the same set of instruments. Today my experiences became successful and i found solution!
- (most important). Because of you compile your code with cygwin (i.e. GNU Linux) toolchain, you need to add to your project straight libraries: libwpcap.a and libpacket.a from developer package of winpcap:
project properties -> linker tab -> libraries -> "add library"
(this just provide a correct names of that libraries in compilation line as you can see:-lwpcap -lpacket
). unfortunately, there are only x32 version of these libraries...
so, if you use x64 version of cygwin (as I), then:
- You need download and install x32 version of cygwin. Be sure to select compilers and other necessary packages in process of installation. then, in properties of NetBeans on tab C/C++ add toolchain from x32 cygwin and name it
cygwin_32
for example. Next, in properties of your project select this (cygwin_32
) tool kit and verify, that you supply path to the correct directories/lib
and/include
of WinPcap. try to build it. at this point, your project should be successfully built. Next, to be able to run your program, you must supply the proper (x32) version of cygwin1.dll located in/bin
directory of cygwin. (simply copy it to directory of your executable).
Thus obtained executable file i succesfully tested at WinXP (x32) with sp2 and of course on my Win7 x64. (For tests i used WpdPack\Examples-pcap\basic_dump\basic_dump.c
from WpdPack_4_1_2.zip
archive, as is).
Hope this would be helpful for someone.
来源:https://stackoverflow.com/questions/24703765/winpcap-developer-usage-with-cygwin-c-netbeans-ide