8

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
Jay
  • 983
  • 2
  • 8
  • 23

3 Answers3

9

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.

nos
  • 223,662
  • 58
  • 417
  • 506
  • nope that doesn't work I tried that, unless I'm doing it the wrong way when I go `g++ code.cpp` it throws that error – Jay Mar 18 '12 at 01:30
  • First verify that the file exists in /usr/include/ and that you didn't mis-spell it. Then copy paste the code that fails into your question, so we can verify it. Or list the files of the package you installed. Perhaps it installed the headers in a subdirectory in /usr/include, or elsewhere. – nos Mar 18 '12 at 01:32
  • There we go! the part about -lname_of_library is the key, along with #include – Jay Mar 18 '12 at 01:49
  • I'm picking Emile as the answer because he solve the original answer. – Jay Mar 18 '12 at 01:52
9

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.

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • Aha! that's more like it. So that error went away, but know I'm stuck with another problem that I'm pretty sure is the same problem, see my edit in the question (When I type it) – Jay Mar 18 '12 at 01:38
  • yeah, I got it, it's been a while since straight up c++, thanks for the help – Jay Mar 18 '12 at 04:30
1

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.

pmr
  • 58,701
  • 10
  • 113
  • 156