I am making a simulator and have written lots of files and headers. The problem is whenever I include a file I give the relative path of the particular file. For example a typical code in my application would begin like
#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 "../para开发者_高级运维ms/gamePlay.h"
#include "../modules/controlModule.h"
class ai
{
public:
etc etc
I want to avoid using the relative paths. For example I want to directly include "tryskill.h" and "destination.h" without giving the absolute paths. That way I wont need to bother if I change the location of any particular file. I am using Ubuntu 9.10. Any help would be highly appreciated.
Really it all depends on your include path, different compilers might 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 would specify ../world etc...
in the list of directories in -I
Typically, if you're compiling from the command line, you would supply include search paths to your compiler (gcc example: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html). If you're using an IDE, you should be able to specify include search paths to your IDE and your IDE will pass these on to your compiler.
You should be using paths that are relative to your program's/library's top-level include path. There are many different ways to do this, depending on how you are building your program. If you are using the CMake build system -- which I strongly urge you to use -- then you would use the INCLUDE_DIRECTORIES
command:
INCLUDE_DIRECTORIES(include)
If your "info/condition.h" file is located in "include/blah/info/condition.h", then you would include it with:
#include <blah/info/condition.h>
If you are compiling from the commandline with g++, you can use the -I
commandline switch:
g++ file1.cpp file2.cpp ... fileN.cpp -I./include
If you are using Make, you can ensure that this is flag is used by adding the following line:
CPPFLAGS += -I./include
Another possibility, though one I do not recommend, is to define the environment variable CPATH
:
# Note the following is what you would do in BASH: export CPATH="$CPATH":"`pwd`/include"
Food for thoughts: what is the alternative ?
#include "pathPlan.h"
#include "exception.h"
#include "world.h"
#include "exception.h" // uh ?
I've always been wary of having too many paths declared in the include paths variable, the issue is that the more paths there are the more likely you are to get a filename clash, and it's annoying to debug, really.
I much prefer using 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"
Which means for a given project "thingy" 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 on my compilation line:
-I${Repository}/thingy/1-3-0-2/include
Sure it's a bit more typing, but it helps keeping things all tidy:
- namespace / folder correlation makes it easier to find the location of the file
- projects prefixing makes it impossible for 2 files from different projects to be mixed up even when they have the same name
Compilers will let you specify on the command line (or a response/configuration file) the directories to search for header files. You can generally configure this in a makefile or IDE's project settings if you're driving a build using one of those tools.
However, in general, I prefer to specify the relative path for headers that 'belong' to a project (as opposed to maybe libraries that are used across projects). That way when you add a new module, you don't have to muck around with project settings or make files to get things to continue to build.
That's if you like to keep the header for a module along-side the module's implementation instead of having the headers piled into a single (or small set of) directories. Either organization is arguably as good as the other.
Any good IDE or makefile will allow you to list relative search paths for files. Look into this for your solution.
精彩评论