C++ include libraries

Jay picture Jay · Mar 18, 2012 · Viewed 64.8k times · Source

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

Answer

nos picture nos · Mar 18, 2012

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.