c++ with libpcap won't compile under windows - c++

First of all I'm quit new in programming in c/c++, so maybe it's just a basic error but i don't get it. The Problem is, I want to use libpcap in a bigger application, but i can't even integrate the libary into a simple HelloWorld.cpp. Atm I'm using Eclipse (MinGw Compiler) with Windows 64bit, but consindering to switch to Linux if my Problem(s) can't be solved.
Downloaded "libpcap-1.5.3.tar.gz" from tcpdump.org, extracted and added the root directory of the Libary to Eclipse (under "Properties/C|C++ Build" to all Compilers and to the MinGW Linker).
My Code:
#include <iostream>
#include <pcap.h>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[]) {
cout << "Hello World!!!" << endl; // prints Hello World!!!
char *dev = argv[1];
printf("Device: %s\n", dev);
return(0);
}
First error:
D:\Tools\MinGW\lib\libpcap/pcap-stdinc.h:49:22: fatal error: bittypes.h: No such file or directory
So I changed line 49 from
#include "bittypes.h"
to
#include <Win32/Include/bittypes.h>
(the actual location of bittypes.h) and next error pops up:
D:\Tools\MinGW\lib\libpcap/pcap/pcap.h:451:1: error: 'Adapter' does not name a type
'Adapter' is defined in "pcap-int.h" so I included this header into "pcap.h" but now I get:
D:\Tools\MinGW\lib\libpcap/pcap-int.h:46:22: fatal error: Packet32.h: No such file or directory compilation terminated.
and this "Packet32.h" does not exist...
I can't belive this popular libary is broken, so where is my mistake? Thanks in advance!

The Problem is, I want to use libpcap in a bigger application, but i can't even integrate the libary into a simple HelloWorld.cpp. Atm I'm using Eclipse (MinGw Compiler) with Windows
If you want to use libpcap on Windows, you need a version of libpcap that's been ported to Windows. The libpcap source won't build on Windows; on UNIXes, libpcap can and does use packet capture mechanisms built into the OS, but it doesn't do so on Windows (for one thing, older versions of Windows, at least, don't have a packet capture mechanism built in!), so it also needs a driver.
One port of libpcap to Windows is WinPcap; it includes the necessary driver. If you install WinPcap, and then download the WinPcap developer's pack and install it, it should be possible to configure Eclipse so that your program can be built with WinPcap.

libpcap-1.5.3.tar.gz is a source package for the library, not a binary package. So you need to build the library; you can't just add the directory to Eclipse and expect it to work.
The usual way to build a source package is to unpack it into a directory and look for a file called README or INSTALL, which is a simple text file containing instructions on how to build and install the package for various machines. Often there will be multiple such files for different platforms.
Generally, there will be a script called configure that you run to create a Makefile for your target; then you run make to build the code and make install to install it in a standard place so that other packages can find it.

Related

Cannot open include file: 'pcap.h'

I am trying to do one small pcap program in c++ and I am getting an error mentioned above.
Below is the program I have written.
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<pcap.h>
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
{
cout<<"Got a Packet"<<endl;
}
int main()
{
pcap_t *handle;
char errbuf[PCAP_ERRUF_SIZE];
struct bpf_program fp;
char filter_exp[] = "icmp";
bpf_u_int32 net;
//Step 1
handle = pcap_open_live("enp0se", BUFSIZ, 1, 1000, errbuf);
//Step 2
pcap-compile(handle, &fp, filter_exp, 0, net);
pcap_setfilter(handle, &fp);
//Step 3
pcap_loop(handle, -1, got_packet, NULL);
pcap_close(handle);
return 0;
}
This problem might be caused by a couple things:
You don't have pcap lib installed. (note: you need something like pcap-devel, not just pcap)
Your compiler can't find it so you will need to provide the lib header path using the -I flag.
Also, don't forget to use -lpcap when linking.
If you're developing a program that uses a particular library, you will need to have the header files that declare functions and variables, and define data types and constants, required by programs using that library.
If you're doing this on Linux:
Linux distributions usually have more than one installable package for a library:
The package whose name is the name of the library, such as "libpcap", includes shared libraries, which are required in order to run programs built with the shared version of the library, but does not include the header files.
The package whose name is the name of the library plus a suffix such as "-devel" or "-dev", includes the library's headers.
The idea is that:
the first package is installed if any programs linked with the shared version of the library is installed - typically, the packages for those programs include the first package for the library as a dependency, so that if the program's package is installed, the first package for the library is also installed;
the second package is only installed if the user wants to develop a program using the library.
Thus, as you're developing a program using libpcap, you'd have to, as others have noted, install the libpcap-devel or libpcap-dev or... package (the name would depend on the distribution you're using).
Note that this isn't something special about libpcap; it applies to other libraries. For example, just as, on Debian and derivatives such as Ubuntu, there's a libpcap-0.8 package (the "0.8" is there for historical reasons) and a libpcap-0.8-dev package (and a libpcap-dev package, which I think is a better-named alias for libpcap-0.8-dev) there's a libssl package and a libssl-dev package.
Operating systems other than Linux may do this differently. For example, macOS doesn't provide headers, but the software development kits in Xcode do, and you get headers for all the macOS APIs, including libpcap - no need to install individual packages, just install Xcode. I think the BSDs either always provide the headers or provide them if you install the compilers.

FATAL ERRORS with compilation after installing Aspose.Cells library for C++

Can someone please help me with my issue with Aspose.Cells library for C++?
I was writing my first C++ programme using Aspose.Cells library. Everything seemed smooth except that the following error was produced after I built the file:
Error before I launch is:
"Error exist in a required project.Continue launch?"
Error after running the code is:
"**fatal error: boost/config/compiler/gcc.hpp: No such file or directory**".**
If I commented out the line #<include Aspose.Cells.h>, the file can run with no errors.
I tried to solve the error by installing Boost library for C++ from zip file "boost_1_73_0", as I think Aspose depends on Boost to run. However, I couldn't link to Boost successfully as there doesn't seem to be a "include" folder and "lib" folder for me to add into project properties.
My questions are:
Will installing Boost solve my problem?
If yes, how can I install Boost library successful?
The following is my code in C++. Thanks a Lot in advance!
#include <iostream>
#include <Aspose.Cells.h>
using namespace std;
int main() {
cout << "!!!I am little red!!!" << endl;
return 0;
}
Regards
Hillary
UPDATE: I have successfully installed and linked Boot library now but I have got three warning message upon building: "Ignoring #pragma warning [-wunknown-pragmas]" , are these warning messages serious?
I also ran into another fatal error: unicode/uloc.h:No such file or directory. How can I correctly link up to unilib-master/Unicode library?
Yes, installing Boost helps.
If Aspose only requires header-only libraries from Boost, then you don't have to do much. The "include" path you're looking for is just the folder where you extracted the zip. The actual library headers are under boost/ in that folder, which are then found by the compiler.
If you need the shared libraries, you will need to build them. Follow the steps here Getting Started On Windows

Why does "cout" keep giving me error C1083?

I was using Visual C++ 6.0 just now, and I keep getting this error:
fatal error C1083: Cannot open include file: 'streambuf': No such file or directory
My code is just a simple hello world program.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
cout<<"Hello World.";
return 1;
}
Then I went and checked my INCLUDE folder and found a file called STREAMBF, but the compiler was looking for STREAMBUF. Notice that the file that is present is missing the U, between the B and the F. This was from a good copy of VC++6.0, directly from the actual CD, not a pirated copy. So there should be all the files needed. But it appears that a file is missing! Is this MS being stupid again, and yet making another big mistake, and forgetting to include an important file on their CDs? I'd hate to think that every single CD for VC++6.0 that was pressed that came out of MS factories had this problem. And I know that it is a missing file, not just a misnamed file, as renaming STREAMBF to STREAMBUF just led to more errors.
Anybody know where I can find a copy of the file STREAMBUF? Or am I just overlooking something here? Is this exact error a known problem with running old copies of VC++ on modern OS's like Windows 7? Is it possible that the only reason that it's looking for STREAMBUF is that this is a newer file associated with Win7, and that if it was running in a different environment (an older OS), it would actually be looking for the correct file, STREAMBF? Can somebody help me here?
Your installation is either broken, deprecated or interpretes your code in wrong way.
You should only use older compiles if you are trying to build project developed entirely for this version.
Try to compile same code with new compiler, if you want to use VS then you should look for Visual Studio Express 2013.
Your code does not have any errors.
Modify your program to, you should be able to see it okay.
#include <iostream.h>
using namespace std;
int main()
{
cout<<"Hello World.";
return 1;
}
However,
your compiler is pretty old. You need to an upgrade.
There are C++ compilers for Windows from Microsoft Express Visual Studios Link and Info VS2013 to
some other non-Microsoft like GCC for Windows.
If you don't have installation access there are some portable c++ compilers.
Finally there are some online compilers for simple test. web based online compilers.
For my win 10 installation of VC 6.0, I had the same problem ... fatal error C1083: Cannot open include file: 'streambuf': No such file or directory
Replacing with <iostream.h> does not solve the problem.
I have checked the header file installation folder (Program files\VS98\VC98\INCLUDE). For some (unknown) reason, some file names have been changed during installation. Restoring the original name has solved the problem, in my case, in example:
Turn STREAMBF into STREAMBUF, STDXCEPT into STDEXCEPT, XCEPTION into EXCEPTION, FCTIONAL into FUNCTIONAL.
Notice: other header file names might be wrong. I have listed above the file names wrong in my installation.
I hope this may help.

MinGW completely bugged on NetBeans

The following code shoudn't produce an error:
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std ;
int main ( int argc , char** argv )
{
int n ;
cin >> n ;
cout << n ;
return 0 ;
}
Yet a get a "RUN FAILED (exit value -1,073,741,511, total time: 46ms)" whilst running MinGW/Msys on Netbeans. Any advice like switching back to Cygwin?
I recommend using MinGW Distro if you want to develop C++ under a Microsoft Windows operating system. It ships with a pretty new GCC version and with the Boost libraries.
NetBeans IDE is pretty picky regarding the build environment settings. E.g. It doesn't work together with all versions of make (we have to distinct make.exe from MSYS and mingw32-make.exe from MinGW for example) and there are problems regarding the used Java Runtime Enviroment (JRE).
With the settings shown in the following screenshot you should be able to build your example with MinGW Distro and NetBeans 8. I recommend to not configure a absolute path to the make.exe file but add that path to your Microsoft Windows environment variable PATH. Otherwise you may get build errors.
Maybe these two blog posts help if you want to use the "default" MinGW distribution:
Installing Minimum GNU for Windows (MinGW)
Configure NetBeans IDE for Minimum GNU for Windows (MinGW)
I hope this helps others as well.
Not related to your question: Don't use using namespace std:
#include <iostream>
int main(int argc, char** argv) {
int n;
std::cin >> n;
std::cout << n;
return 0;
}
I ran into this same issue (with exit code -1,073,741,511), so though a dated question, I'm posting this here for anyone else who runs into the problem.
Run the executable for the program manually. You might get an error such as "the procedure entry point __gx_personality_v0 coud not be located in the dynamic library libstdc++-6.dll". (OP has confirmed this in a comment.)
The .dll file referred to in the error message above is either not being linked, or linked incorrectly. The correct version of the .dll that needs to be linked is the one in the ...\MinGW\bin directory. In Windows, you can check the .dll file being linked by typing where libstdc++-6.dll in a command prompt; the first result that is listed will be the file that is linked. If you already see ...\MinGW\bin\libstdc++-6.dll as the first result here, my fix below will not help you.
If you see a message "INFO: Could not find files for the given pattern(s).", then ...\MinGW\bin needs to be added to your %PATH% variable. (OP has already confirmed this was not the issue.)
The issue I was having was that a program I had installed had its own (likely outdated) version of libstdc++-6.dll, which was in a folder also included in my %PATH% variable, ahead of ...\MinGW\bin. This meant that this other .dll file was being picked up and linked to during execution. This can be fixed by editing your %PATH% variable to make sure the ...\MinGW\bin entry is ahead of all other directories that also have a version of the .dll file.
Edit: The other option is to statically link the .dll at program compilation, or place a copy of the correct .dll in the program executable directory. However, neither of these fixes is 'global', and needs to be done for each project individually.
Hope this helps!

Missing cyg*.dll when using libraries compiled with Cygwin, and errors when added

I just compiled zlib and libzip with Cygwin to use them with Code::Blocks in Windows.
My code is that:
#include <iostream>
#include <zip.h>
int main()
{
//Open the ZIP archive
int err = 0;
zip *z = zip_open("main.zip", 0, &err);
zip_close(z);
std::cout << "Hello world!" << std::endl;
return 0;
}
When I build my code, it works well, no errors and warnings.
When I launch my program, it says that I don't have cygzip-2.dll. Okay, I search it and put it in my executable folder. Then, it says that I don't have cygwin1.dll. Okay, I put it too. The same for cygz.dll and cyggcc_s-1.dll.
Oh, it works! But then, my program stops with always the same status: -1073741819.
It doesn't even tell me hello :(
I compiled it with MinGW (it did the same error on Cygwin), and I linked libz.a, libzip.a and libzip.dll.a. Where does the problem could come from?
Thanks!
EDIT: When I try to compile my program IN Cygwin, it says 'undefined reference to '_zip_open'' and 'undefined reference to '_zip_close''. Probably something is missing, but what?
Eventually, I succeeded to use my 2 libraries! I had already tried to use CMake, but failed miserably.
So today, I decided to retry it with the GUI. Firstly, I compiled zlib. I chose the zlib folder, and put the build folder in it. I configured with the option for Code::Blocks and MinGW Makefiles, and native compilers. Then, I opened the .cbp (Code::Blocks Project) in my 'build' folder, and built it.
For libzip, I did the steps except that I specified 2 variables:
ZLIB_INCLUDE_DIR = the root of zlib folder (where there are all the .h and .c) and ZLIB_LIBRARY = [the path to your build folder from zlib]\libzlib.dll
I built it from the .cbp too. And I linked all my files to my project, and it was done!