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

Time:12-10

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.

CodePudding user response:

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>

CodePudding user response:

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>.

CodePudding user response:

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.

  • Related