madreblu
madreblu

Reputation: 383

How to set priority of conflicting headers c++

Imagine you have two something.h in two different directories. You cannot write to those directories and do not have root access.

You have code that does :

#include <something.h>

How do you specify use the something.h in a specific directory and ignore the other ?

Upvotes: 7

Views: 5593

Answers (2)

bane
bane

Reputation: 831

try using:
#include "../directory/something.h"
Note that GCC looks for headers using the Search Path.
You can also ask GCC to look for header files in specified directories. Use -iquote dir, to add the directory dir to the head of the list of directories to be searched for header files.

Upvotes: 2

mvp
mvp

Reputation: 116197

Typically, list of directories that are eligible for searching of what is considered system includes (using angle brackets in #include), is provided as set of -I switches to compiler. Often these include directories are specified in makefiles or project files.

Many (but not necessarily all) compilers will honor order of directories listed as include directories - so you should be able to choose your preference by changing that order in your makefiles. However, in some compilers it may be difficult, as some directories are considered always included (like gcc by default assumes that you have /usr/include included). In other words this is very implementation specific.

If you use not angle brackets, but double quotes, then you can simply specify your desired file directly like #include "dir/file.h".

Upvotes: 1

Related Questions