c++ include define DIR

Started by
5 comments, last by user0 10 years, 11 months ago

I am trying to set a directory to a define like #define DIR "/dir"

and use that to include a bunch of files like #include DIR+"header.h"

this is not working its giving me #include expects "FILENAME" or <FILENAME>

is there some way of making this work or does the preprocessor not work like this?
Advertisement
No. The preprocessor does not work like this.


Why do you need a preprocessor variable to define a directory?

Hmm that sucks theres so much it can do you would think it could do that somehow.

I have 37 header files I want to include that are in a versioned directory ie library-0.4.1/

I could rename the folder to just library but I was wanting to keep multiple versions to change them easily.

When 0.4.2 comes out I could simply change dir defined path to library-0.4.2 and if I dont like it or it doesnt pass the tests I could revert easily

Hmm that sucks theres so much it can do you would think it could do that somehow.

I have 37 header files I want to include that are in a versioned directory ie library-0.4.1/

I could rename the folder to just library but I was wanting to keep multiple versions to change them easily.

When 0.4.2 comes out I could simply change dir defined path to library-0.4.2 and if I dont like it or it doesnt pass the tests I could revert easily

Add a path to that library in the "Additional Include Directories" (for Visual Studio), or whatever is the same thing in your IDE. Then you can include files from that directory like this:

#include <subfolder/file.h> // where "subfolder" is a folder inside whatever path you told the compiler about

This will give you exactly what you want.

You could do this:

#define HEADER(path) "/dir/" ## path
 
#include HEADER("my path/file.h")

But really, it's better to just specify "/dir/" manually so as to not confuse others reading your code (including yourself in the future).

If "/dir/" is long, then make sure you specify your compiler's include paths properly. In gcc and MinGW, this is: "gcc -I <include path>" (That -I is a uppercase 'i'). You can specify multiple include paths to search.

As mentioned above, there's a whole bunch of methods that are all better than abusing the preprocessor for this.

a) If stuff is versioned, why not use a version control system instead of folder names?

b) Failing that, the -I option exists for exactly that kind of thing (ie. it belongs in your project settings or Makefile, not the actual source code)

c) Use a symlink that points to whatever version you want to use (yes, Windows finally has them too)

f@dzhttp://festini.device-zero.de

@0r0d that would do it I can set the paths in gcc but not quite the solution I was hoping for

@Servant of the Lord Thats exactly what I was looking for although its giving me an error im still messing with it to figure it out.

@Trienco

a. The local versions differ from the actual version granted I could setup my own version control but I dont want other parts of the program to revert just this single part I could setup 2 one for the library and one for the program but thats just asking for issues and I dont want to have to manage anything a static folder is simple and already exists on my machine/in the source for any machine.

b. Yes I get that just like 0r0d but its much easier for me to work on a certain section that try to coordinate a compile time option across developers.

c. That would work and would be in my specific section (all compilation is on a linux machine so no worries im not familiar with symlinks in windows havent compiled or done anything other than games on it in a long time) I may fallback to this if I cant get Servants way to work.

This topic is closed to new replies.

Advertisement