C preprocessor: building a path string

Given the macro that was previously defined:

#define FILENAME somefile.h

I want to combine this with another macro line that defines the (relative) path of this file. My current approach is as follows:

#define DIRECTORY ../somedir/

#define STRINGIFY_(x) #x
#define FILE2_(dir, file) STRINGIFY_(dir ## file)
#define FILE_(dir, file) FILE2_(dir, file)

#include FILE_(DIRECTORY, FILENAME)

This results in an error (GCC4.9):

error: inserting "/" and "file" does not give a valid preprocessing token

Removing the final slash using the definition DIRECTORYeliminates this error, but obviously does not give the desired result. Similar errors occur when I try to smuggle /otherwise. For instance:

#define FILE2_(dir, file) STRINGIFY_(dir ## / ## file)

not working for the same reason.

I would like to know what is happening here, and obviously how to get around this.

. . -, , , , , ( , ).

+4
1

[cpp.include]/4:

        # include pp-tokens new-line

( ). include ( , ). , , , undefined. 152


152 , (. 2.2); , , , .

, #include MACRO , MACRO #include. .

, ## [cpp.concat]/3:

-, -, , ## ( ) , . [..] , undefined.

, A##B ./ , .
"abc /xyz", abc/ - "abc , , "abc" .
, <abc/ xyz>, / xyz , .

, , ##. .


GCC , :
#define PATH <foo/bar/
#define FILE boo>

#define ARG PATH FILE
#include ARG

, GCC ( - ). V++ Clang , .

+3

All Articles