Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

c - Preprocessor concatenation for include path

I have a set of includes that reside in a far off directory meaning that including them requires a long include, such as:

#include "../../Path/to/my/file.h"

Where I have multiple of these it becomes a bit inconvenient so I am thinking I may be able to use a #define for the directory path and then concat the file name that I need, i.e.

#define DIR "../../Path/to/my/"
#define FILE1 "file.h"
#define FILE2 "anotherFile.h"

#include DIR FILE1 // should end up same as line in first example after pre-proc

However this does not work... is there anyway to concatenate within the workings of the C pre-processor suitable for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The compiler will do macro replacement on an #include line (per C 2011 [N1570] 6.10.2 4), but the semantics are not fully defined and cannot be used to concatenate file path components without additional assistance from the C implementation. So about all this allows you to do is some simple substitution that provides a complete path, such as:

#define MyPath "../../path/to/my/file.h"
#include MyPath

What you can do with most compilers and operating systems is:

  • Tell the compiler what directories to search for included files (as with GCC’s -I switch).
  • Create symbolic links to other directories, so that #include "FancyStuff/file.h" becomes equivalent to ../../path/to/FancyStuff because there is a symbolic link named FancyStuff that points to the longer path.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...