How can I avoid explicitly declaring directory paths in C or C ++ #include directives?

I am building a simulator and have written a lot of files and headers. The problem is that when I include a file, I am specifying a relative path to a specific file. For example, typical code in my application will start as

#ifndef AI_H
#define AI_H

#include <cstdlib>

#include "../world/world.h"
#include "pathPlan.h"
#include "skills/tryskill.h"
#include "../info/condition.h"
#include "dataStructures/destination.h"
#include "../params/gamePlay.h"
#include "../modules/controlModule.h"


class ai
{
    public:

etc etc

      

I want to avoid using relative paths. For example, I want to directly include "tryskill.h" and "destination.h" without providing absolute paths. This way, I won't bother if I change the location of any particular file. I am using Ubuntu 9.10. Any help would be much appreciated.

+2


a source to share


6 answers


Actually it all depends on your include path, different compilers may call it different things, but in gcc

-Idir  Append directory dir to the list of directories searched for include files.

      



So in your example, you have to ../world etc...

list the directories in-I

+2


a source


Generally, if you are compiling from the command line, you should include the search paths in your compiler (gcc example: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html ). If you are using an IDE, you must provide search paths to your development environment and your IDE will pass them on to your compiler.



+2


a source


Compilers allow you to specify, on the command line (or response / config file) directories to look for header files. You can usually configure this in your project's makefile or IDE settings if you are building using one of these tools.

However, in general, I prefer to specify a relative path for headers that "belong" to the project (as opposed to libraries that are used in projects). This way, when you add a new module, you don't have to guess with project settings or create files to keep things building.

This is if you want to keep the header for the module alongside the module implementation, instead of having the headers stacked up in one (or a small set) of directories. Any organization is perhaps as good as another.

+1


a source


You must use paths relative to the top level on / off path of your program / library. There are many different ways to do this, depending on how you build your program. If you are using the CMake build system, which I strongly urge you to use, then you should use the commandINCLUDE_DIRECTORIES

INCLUDE_DIRECTORIES (include)

If your "info / condition.h" file is in "include / blah / info / condition.h", you can include it with:

#include <blah / info / condition.h>

If you are compiling from the command line using g ++, you can use the command line switch -I

:

g ++ file1.cpp file2.cpp ... fileN.cpp -I./include

If you are using Make, you can verify that this flag is used by adding the following line:

CPPFLAGS + = -I./include

Another possibility, although I don't recommend it, is to define an environment variable CPATH

:

# Note the following is what you would do in BASH:
export CPATH = "$ CPATH": "` pwd` / include "
+1


a source


Food for thought: what is the alternative?

#include "pathPlan.h"
#include "exception.h"
#include "world.h"
#include "exception.h" // uh ?

      

I was always wary of having too many paths declared in the include paths variable, the problem is that the more paths there are, the more likely you are to come across a filename, and this is very annoying to debug. / p>

I prefer to use this:

// 3rd party libraries
#include <3rdParty1/foo.h>
#include <3rdParty2/foo.h>

// Projects I depend on
#include "myProject1/bar.h"

// Current project, from the include directory
#include "currentProject/foobar.h"
#include "currentProject/another.h"

// Current project, from the source directory (private includes)
#include "../world.h"
#include "../detail/helper.h"

      

What does "thingy" mean for this project I have the following file:

// in thingy/1-3-0-2/include/thingy/foo.h

namespace thingy  // base namespace is project name
                  // namespace hierarchy identical to folders hierarchy
{
}

      

And then I have the following line of my compilation:

-I${Repository}/thingy/1-3-0-2/include

      

Sure it prints a little more, but it helps keep things tidy:

  • name-to-folder ratio makes it easier to find file location Project prefix
  • makes it impossible to mix 2 files from different projects, even if they have the same name
+1


a source


Any good IDE or makefile will allow you to display relative file search paths. Look at this for your solution.

0


a source







All Articles