Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override default header search path

Tags:

c++

gcc

I'm currently trying to get a program to compile on a system that I don't have control over.

The problem that I'm having is the include directories look like this:

/usr/include:
gmpxx.h gmp.h

/usr/local/include:
gmp.h

In my cpp file, I use

#include <gmpxx.h>

and this finds the correct file in /usr/include, however when gmpxx.h includes gmp.h, it pulls it from /usr/local/include, which breaks the build.

Right now, I see 3 very ugly solutions to the problem

  1. In my cpp file, add #include </usr/include/gmp.h>
    Having an absolute include path is pretty ugly and non-portable, and I think that this sort of thing should belong in the Makefile instead of the source.

  2. add the -nostdinc flag to my makefile, and specify the include paths by hand

  3. create local symlinks to the libraries that I really want, and then do local includes (#include "gmp.h")

Is there a better solution that I'm missing?

like image 228
cobbal Avatar asked Nov 02 '11 23:11

cobbal


People also ask

How do you tell the compiler to look for header files in a different directory?

it's simple, use the "-B" option to add . h files' dir to search path. Show activity on this post. Headers included with #include <> will be searched in all default directories , but you can also add your own location in the search path with -I command line arg.

How do you add a search header path?

Look at Preferences->Locations->"Custom Paths" in Xcode's preference. A path added here will be a variable which you can add to "Header Search Paths" in project build settings as "$cppheaders", if you saved the custom path with that name. Set HEADER_SEARCH_PATHS parameter in build settings on project info.

Where is the header file path?

Most standard headers are stored in /usr/include . It looks like stdbool. h is stored somewhere else, and depends on which compiler you are using. For example, g++ stores it in /usr/include/c++/4.7.

Where does g ++ look for header files?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat.


1 Answers

The search paths for includes are taken in the following order:

  • The -I command-line option.
  • The CPLUS_INCLUDE_PATH environment variable.
  • The standard defaults.

So, you can use either of the first two (whichever seems better/more convenient for your purposes).

like image 104
ruakh Avatar answered Sep 24 '22 01:09

ruakh