Preprocessor concatenation for include path

前端 未结 2 1685
情话喂你
情话喂你 2020-12-20 03:04

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\         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 03:27

    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.

提交回复
热议问题