pozz said:
In a C file I want to include a file depending on the macro NAME. For
example, if it is "foo", the file to include will be "foo
\foo_params.h". If it is bar, the include file will be "bar
\bar_params.h".
I tried to achieve this, but without success. I started from:
#define INCLUDE_FILE "foo\foo_params.h"
#include INCLUDE_FILE
and it worked. After that I tried the following:
#define INCLUDE_FILE "foo\foo" ## "_params.h"
#include INCLUDE_FILE
without success...
You *probably* don't want a backslash character in your #include
directive.
If you had a string literal "foo\foo_params.h", the \f sequence would be
replaced by a form feed character.
But in
#include "foo\foo_params.h"
that thing that *looks* like a string literal is really a
"q-char-sequence" enclosed in quotation marks (see section 6.10.2
of the C standard). The rules for how the characters between the quotes
are interpreted are implementation-defined.
Normally on Windows, you'd use a forward slash to denote a directory
delimiter; Windows actually permits both / and \ characters, and the
compiler might do some translation as well.
There are three forms of #include directive:
# include <h-char-sequence> new-line
# include "q-char-sequence" new-line
# include pp-tokens new-line
where the third form must, after preprocessing, match one of the first
two forms. But (C11 6.10.2p4):
The method by which a sequence of preprocessing tokens between
a < and a > preprocessing token pair or a pair of " characters
is combined into a single header name preprocessing token is
implementation-defined.
And there's a footnote:
Note that adjacent string literals are not concatenated into a
single string literal (see the translation phases in 5.1.1.2); thus,
an expansion that results in two string literals is an invalid
directive.
I don't see portable, or even non-portable, way to do what you're
trying to do. If there's a solution that I'm missing, it probably
involves token-pasting with the ## operator.
If you have a finite set of "foo" and "bar" values, you can use a more
brute-force approach:
#if FOO
#include "foo/foo_params.h"
#elif BAR
#include "bar/bar_params.h"
#elif
...
#endif