randomThought
randomThought

Reputation: 6393

including .h file from a different application/directory

I have some .h files as follows (on Linux)

Source/Server/connect.h
Source/Server/message.h
...

I am developing another application that needs the two .h files but is in a different directory

Source/App2/..

How can I include the connect.h file in the App2 application, considering that I use perforce and everyone else working on the application would have their own copy so adding an absolute path to the include library might not be a good idea but im not sure.

EDIT: I use a proprietary build mechanism for building the code so will not be able to specify gcc options directly.

Upvotes: 5

Views: 34330

Answers (5)

Thomas Matthews
Thomas Matthews

Reputation: 57678

I suggest removing paths from the #include statements. As others have stated, put the paths into the parameters to the compiler. Resolve the path differences in the makefile or use environment variables (may need to do both).

My experience is that files will move. Anything that doesn't use relative paths will break the build (which is very bad).

Upvotes: 3

plinth
plinth

Reputation: 49179

in addition static relative paths, you can also play with preprocessor chicanery. One technique I saw used at Adobe for cross-platform code, was to do something like this:

/* globalplatform.h */
#ifdef MAC
#define PLATFORM "../Platform/Mac/MacPlatform.h"
/* custom standard IO etc */
#define STDIO "../Platform/Mac/io/stdio.h"
#define CTYPE "../Platform/Mac/io/ctype.h"
#endif
#ifdef WIN32
#define PLATFORM "../Platform/Win/WinPlatform.h"
#define STDIO <stdio.h>
#define CTYPE <ctype.h>
#endif
/* etc */
#ifndef PLATFORM
#error undefined PLATFORM
#endif

/* some C file */
#include "globalplatform.h"
#include PLATFORM
#include STDIO
/* don't need CTYPE, no penalty */

While the platform problem isn't your problem, you can define the relative paths based on build configuration if you want to and the config changes happen in one place instead of many and client files only pull in what they need. The down side is that any tools you use for browsing header files (right-click and so on) are hosed.

Upvotes: 1

Liz Albin
Liz Albin

Reputation: 1489

You can change the compiler directives as above, or modify the path within your code (either relative or absolute).

I would suggest that you consider the best locations for headers and object files (and libraries) for all your projects and set that up.

If you have standard include and lib locations you'll simplify the development down the road

Upvotes: 0

James McNellis
James McNellis

Reputation: 354969

You can #include a relative path to the files:

#include "../Server/connect.h"

or you can add a flag to tell the compiler to look in the other directory. For gcc you can use -I../Server; for Visual C++ you can use /I"../Server"; other compilers, I'm sure, have their own flags for this purpose.

I think the second is better in most cases, since it allows you to move your projects around while only requiring you to modify the include path in one place (the makefiles or property sheets).

Upvotes: 17

Drakosha
Drakosha

Reputation: 12155

What about adding include search path to he compiler, for gcc it's -I switch.

Upvotes: 3

Related Questions