cobbal
cobbal

Reputation: 70753

Override default header search path

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?

Upvotes: 8

Views: 12117

Answers (2)

Antti Huima
Antti Huima

Reputation: 25532

Remove gmp.h from /usr/local/include, or find out why you have a software distribution that wants to have gmp.h in /usr/local/include and remove the distribution. I think the problem is caused by you having for some reason two conflicting header file sets for GMP. If you have a standard installation of GMP development files on your system (/usr/include/...) there shouldn't be a reason to have another set of headers in /usr/local/include/.

There's no clean way to fix it otherwise, because you should include gmpxx.h using angle brackets

#include <gmpxx.h>

as you do. Now gmpxx.h includes gmp.h using angle brackets also, and on your system /usr/local/include takes precedence over /usr/include, which kind of makes sense.

So I'd recommend you to figure out why there are two gmp.h's and remove the spurious one. There is something fishy in your header file setup.

You can't easily resuffle /usr/include and /usr/local/include because they are considered system include directories and if you try to use -I on them, GCC will ignore the option.

Upvotes: -2

ruakh
ruakh

Reputation: 183381

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

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

Upvotes: 15

Related Questions