Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ include libraries

Ok, so it's been a while, and i'm having problems with #includes

So I'm doing

#include "someheader.h"

but it's giving me

fatal error: someheader.h: No such file or directory

It's a system wide library I guess you could say. I'm running arch linux and I installed the library from the repo, and I think the .h files are in /usr/include.

I could just copy all the header files into the folder my code is in but that would be a hack.

What is the "right" way to do this?

Edit: I wasn't correct by saying the .h files were in /usr/include, what I meant was that the library folder was in there So, Emile Cormier's answer worked to a certain extent. The problem now is that there are some include in the header file and it seems from the methods I'm trying to access that those includes are not happening it's giving my the error

undefined reference to Namespace::Class::method()

Edit: Ok so the final answer is:

#include <library_name/someheader.h>

And compile with

g++ code.cpp -llibrary_name
like image 614
Jay Avatar asked Mar 18 '12 01:03

Jay


People also ask

How many libraries does C have?

The ANSI C standard library consists of 24 C header files which can be included into a programmer's project with a single directive. Each header file contains one or more function declarations, data type definitions and macros.

Do C have libraries?

The C standard library provides macros, type definitions and functions for tasks such as string handling, mathematical computations, input/output processing, memory management, and several other operating system services.

Can you use C without libraries?

Then the compiler is smart enough to resolve the import dependencies and compile your program. If you disassemble the program, you can see only your code is there, there is no standard library bloat in it. So you can use C without the standard library.


3 Answers

Sometimes, header files for a library are installed in /usr/include/library_name, so you have to include like this:

#include <library_name/someheader.h>

Use your file manager (or console commands) to locate the header file on your system and see if you should prefix the header's filename with a directory name.


The undefined reference error you're getting is a linker error. You're getting this error because you're not linking in libsynaptics along with your program, thus the linker cannot find the "implementation" of the libsynaptics functions you're using.

If you're compiling from the command-line with GCC, you must add the -lsynaptics option to link in the libsynaptics library. If you're using an IDE, you must find the place where you can specify libraries to link to and add synaptics. If you're using a makefile, you have to modify your list of linker flags so that it adds -lsynaptics.

Also the -L <path_to_library> flag for the search path needs to be added, so the linker can find the library, unless it's installed in one of the standard linker search paths.

See this tutorial on linking to libraries with GCC.

like image 120
Emile Cormier Avatar answered Oct 12 '22 07:10

Emile Cormier


You'd use #include <someheader.h> for header files in system locations.

#include "someheader.h" would try to include the file someheader.h in the directory of your .c file.

In addition to including the header file, you also need to link in the library, which is done with the -l argument:

g++ -Wall youprogram.cpp -lname_of_library

Not doing so is the reason for the "undefined reference .. " linker errors.

like image 44
nos Avatar answered Oct 12 '22 07:10

nos


The quick fix is to do use:

#include <someheader.h>

assuming that someheader.h is in the standard include locations (to find it use the command locate someheader.h in a shell. If it is in /usr/include it is in a standard location. If it is in a subdirectory of /usr/include you only need to add the part of the directory up to /usr/include in the #include directive (e.g. #include <fancy_lib/someheader.h>)

However, this is only half of the story. You also will need to set up your build system in a way that locates the given library and adds its include path (the path under which it's header files are stored) to the compiler command (for gcc that is -I/path/to/header). That way you can also build with different versions by configuring them in your build system. If the library is not header-only you will also have to add it to the linker dependencies. How this is achieved in your build system is best found out by consulting its documentation.

like image 24
pmr Avatar answered Oct 12 '22 06:10

pmr