g++ compile using custom header file - c++

I want to use a header file,
its included as #include <custom.h>
how can I compile it by using the custom.h header file ?
I tried -I /path/to/custom.h , but its giving me error that its not a directory..

-I /path/to
This will enable all headers in that directory to be found.
In man gcc, search (using / in your pager) for "-I dir":
-I dir
Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. If the directory dir is a standard system include directory, the option is ignored to ensure that the default search order for system directories and the special treatment of system headers are not defeated. If dir begins with "=", then the "=" will be replaced by the sysroot prefix; see --sysroot and -isysroot.

Indeed /path/to/custom.h is not a directory but a file.
-I/path/to/custom/

Related

How to #include <whatever.h> installed with apt in Ubuntu

I have just installed hidapi in my Ubuntu 20.04 following the instructions, i.e. by doing
sudo apt install libhidapi-dev
I wrote my program in the file mwe.cpp which contains only this line:
#include <hidapi.h>
and now I want to compile it with
g++ -o mwe.o mwe.cpp
but I get
mwe.cpp:1:10: fatal error: hidapi.h: No such file or directory
How am I supposed to use this module? Sorry for such a basic question but cannot find out.
On Ubuntu based systems, the system package libhidapi-dev installs the include files to /usr/include/hidapi, so either include this (-I/usr/include/hidapi) in your command line or #include <hidapi/hidapi.h>
If that header is not within the standard search path for headers, then you can include it manually with the -I flag e.g.
g++ -I/usr/include/hidapi -o mwe.o mwe.cpp
To locate a file on ubuntu, you can run:
sudo updatedb
locate hidapi.h
> /usr/include/hidapi/hidapi.h
You can view the standard include search path with:
gcc -print-search-dirs
Alternatively, because /usr/include is on the standard search path, you can write your include as <hidapi/hidapi.h>.
How to #include <whatever.h> installed with apt in Ubuntu
If the package installs the header within a directory included in the default search path of your compiler - which is typical - then you can include the header using a relative path from the root of the search path where the header is installed. For example, if the file is in the path /usr/include/x/y.h, then you can include <x/y.h>.
If the package doesn't install the header within the default search path, then you must specify the search path for the compiler when you invoke it, and then include the header relative to the specified include directory. For example, if the file is in the path /opt/custom/x/y.h, then you can include <x/y.h> and specify /opt/custom as a search path for the compiler.
If you use GCC or compatible compiler, and the package supports it, then you can use a program called pkg-config to get the compiler options needed to use the library. Besides the header search path, this also takes care of linking with the library as well as any mandatory compiler options. Example:
pkg-config --libs --cflags libhidapi
I don't know, how do I find the location of hidapi.h?
There are several ways to find out the location of a file. A general tool is the program find. Example:
find / -name=hidapi.h
A more specific tool for learning the paths of files installed by an apt package is apt-file. Alternatively, you can look up the list of files in the https://packages.ubuntu.com/ website.

Using SDL with the g++ compiler

I'm trying to run this test code from the SDL website but I dont understand where to put the downloaded files and how to reference them using the g++ compiler. I've been trying to using the -I command but I dont quite understand that either. I keep getting the "fatal error: SDL.h: No such file or directory #include<SDL.h> "
Using windows and sublime text editor and g++ compiler
Directly from the g++ manual:
"-I dir
Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. If the directory dir is a standard system include directory, the option is ignored to ensure that the default search order for system directories and the special treatment of system headers are not defeated . If dir begins with "=", then the "=" will be replaced by the sysroot prefix; see --sysroot and -isysroot."
As for your error, I would try #include <SDL2/SDL.h>. This could vary based on the version you are using. If that doesn't work I would just make sure you have the correct path when including in the compiler as a flag.

How can I force cmake to use C++ header files in /usr/include in Linux?

I have a cmake project that uses a header installed in /usr/include, let's call it freeglut.h. When I use find_package(GLUT) I get ${GLUT_INCLUDE_DIR} pointing to /usr/include. All's well.
Now, I'm adding CUDA, which keeps its own copies of these and other headers in one of the paths included with find_package(CUDA). Normally I would resolve this by placing ${GLUT_INCLUDE_DIR} before ${CUDA_INCLUDE_DIRS} in include_directories(). However, on Unix systems cmake, in UnixPaths.cmake, maintains a list called CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES, which contains /usr/include and apparently keeps cmake from emitting -I<dir> arguments for the compiler for the directory /usr/include, meaning that the compiler searches the CUDA path first, and uses the freeglut.h header found there.
I have tried using list(REMOVE_ITEM ... to remove /usr/include from the CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES list, but that didn't change the compiler commands that cmake made.
I could of course start hacking around with the CUDA installation, delete the headers I don't want, or modify the CUDA_INCLUDE_DIRS variable, but is there a clean way to tell cmake to use the system header files first, if they exist?
I suppose the -idirafter flag should help you:
-idirafter dir
Search dir for header files, but do it after all directories specified with -I and the standard system directories have
been exhausted. dir is treated as a system include directory.
https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html
You can use it like this to lower CUDA include dirs priority:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -idirafter /usr/include/<CUDA_includes>")

fatal error: libxml/xmlmemory.h: No such file or directory

Can anyone explain to me that error and what should I do to solve it !?:
In file included from /home/jros/catkin_ws/src/kinectueye/include/MIXEDVISION/CModelStereoXml.h:6:0,
from /home/jros/catkin_ws/src/kinectueye/src/kinect_ueye.cpp:10:
/home/jros/catkin_ws/src/kinectueye/include/MIXEDVISION/CXml.h:6:31: fatal error: libxml/xmlmemory.h: No such file or directory
compilation terminated.
CModelStereoXml, CXml and xmlmemory all are files in a library (so I can't edit it) that I use in my program kinect_ueye.cpp.
It says CXml.h line 6 is:
#include <libxml/xmlmemory.h>
But libxml/xmlmemory.h is not in your include path. The include path is set with -I options on the compiler command line.
The error is "fatal" because compilation cannot continue past that point.
Find out where that file is actually installed and make sure the path to its libxml directory is in a -I option. For example, if it's installed in /opt/local/include/libxml/xmlmemory.h, then you need -I /opt/local/include on your command line.
CModelStereoXml, CXml and xmlmemory all are files in a library (so I can't edit it)
Only the compiled code is in a library (.a, .la, or .so file) that you can't edit. The headers will be located somewhere else.

How to set Eclipse Library paths?

I am trying to get MLPack to work in Eclipse, but have some problems with including a header file.
I manage to read a header file in Eclipse
#include <neighbor_search.hpp>
This header file calls itself
#include <mlpack/core.hpp>.
I included in Eclipse in the library path the path just upto mlpack/core.hpp, i.e.
/usr/include/MLPack/mlpack-1.0.8/src in the includes tab of paths and symbols.
I get the error message though:
/usr/include/MLPack/mlpack-1.0.8/src/mlpack/methods/neighbor_search/neighbor_search.hpp:26:27: fatal error: mlpack/core.hpp: No such file or directory
#include <mlpack/core.hpp>
How do set my path correctly so that <mlpack/core.hpp> will be found?
I also use MLPack (but not in Eclipse) and had this error.
To resolve this problem you have to specify to GCC where the files of mlpack are. The thing is when you include a header file with #include <file.h> , GCC looks in these directory :
/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include
So what I did is create a soft link in /usr/include/ to the mlpack directory :
cd /usr/include/
sudo ln -s /full/path/to/the/mlpack/folder/ mlpack
Like that the GCC will have access to mlpack directory.
You'll have to download and install at least these 2 libraries (if you don't have them) :
boost and armadillo.
Personally I also had to create a soft link for the libxml library :
cd /usr/include/
sudo ln -s /usr/libxml2/libxml/ libxml
I'm a bit in late to answer, but I hope it'll help further people !
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Freference%2Fcdt_u_prop_general_pns_libpath.htm
Project Properties -> C/C++ General category -> Paths and symbols -> Includes tab