Cannot open include file: 'pcap.h' - c++

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.

Related

CLion C++17 boost: No member named xxx in namespace

I'm setting up a C++17 + boost 1.76.0 project in Clion 2018.3, with this minimal code:
#include <boost/asio/ip/tcp.hpp>
int main(int argc, char **argv)
{
auto const address = boost::asio::ip::make_address(argv[1]);
return 0;
}
In my CMakeLists.txt I have correctly setup the C++ version:
add_definitions(-std=c++17)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
And the project compiles and run fine. But Clion shows the following error on the make_address line:
No member named 'make_address' in namespace 'boost::asio::ip'
CTRL-hover on the method name correctly shows the doc, and CTRL-click correctly opens the header file.
Are we supposed to do something else to tel Clion which C++ version are we using?
Looks like the IDE and compiler don't agree in the version of boost. These functions are not specific toi a language version, but I think were introduced in a relatively recent Boostt release
It turned out that I had 2 Boost versions installed on the system, one shipped with ROS (I didn't know that) and v1.76.0 that I compiled to get boost::asio. Looks like Clion was using the one of ROS which older.
Uninstalling v1.76.0 system-wide and adding it back as a header library in the project fixed these false errors, though Clion now takes forever to anaylise the code (despite I excluded the libs folder). Using only the version shipped with ROS would be the best solution for someone not needed the latest additions of Boost (but I do).

How to implement a github C++ library?

I am trying to use G+smo library from github. I download, unzip it and make. Then I could run all examples in the package. But there is no tutorial that guide me to build my own cpp file. For example I tried to build the simplest code from the tutorial:
# include <gismo.h>
using namespace gismo;
int main(int argc, char* argv[])
{
gsInfo << "Hello G+Smo.\n";
real_t a = 2.0; // a real number, ie. double
index_t b = 3; // an integer, ie. int
GISMO_ASSERT( a*b == 6, "This is an error, 2*3 should be 6.");
return 0;
}
And linked the lib file by -lgismo, but it says 'gismo.h: No such file or directory
compilation terminated'. I know it my be the fact that I am not familiar with c++. Can you guys give me some suggestion about it? Or, if it is overly obvious, can you just suggest me some book to read?
Thank you.
The error 'gismo.h: No such file or directory compilation terminated.' suggests that you didn't set the path where the compiler should look for the library headers. Try adding -I/path/to/gismo/headers to the compiler flags, e.g. on my system make install by default installed it to /usr/local/include/gismo so I have to add -I/usr/local/include/gismo.
By the way, there is a "tutorial": The README.txt in the deploy folder.

How to use GMP library in dev c with gcc4.7.2

I have absolutely no idea about using gmp. Need some functions for a project and need a quick installation guide. I am Absolutely beginner to this field so please help accordingly.
I have:
Dev C++ 5.4.2 in windows 8.1 configuration with GCC4.7.2 as default
compiler.
gmp-static-mingw-4.1.tar
Please specify the correct procedure to configure gmp library.
At first put gmp.h into ..\Dev-Cpp\MinGW32\include and both libgmp.a and libgmp.la into ..\Dev-Cpp\MinGW32\lib directory, then create some project in DevCpp, for example:
#include <stdio.h>
#include <gmp.h>
int main(void)
{
mpz_t x;
mpz_init(x);
mpz_set_str(x, "12345", 10);
mpz_mul_ui(x, x, 2);
gmp_printf("%Zd\n", x);
mpz_clear(x);
return 0;
}
After that go to Project Options -> Parameters and click Add Library of Object:
From the list select libgmp.a file (your static library) and click Open:
Compile and run you project, you will see some note about Makefile update, simply confirm.
Note that GMP 4.1 is now rather old, consider latest version and/or manual compilation for best possible performance on your configuration.

c++ with libpcap won't compile under windows

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.

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!