I have an old MCPP project which was used as a communication layer between C++ code which runs on machines and C# which runs on a desktop computer. Recently we decided to try and kill this "glue" project.
This project has in it a few lists of constants which are used in the communication and these will be preferably used as an external link in both, C++ and C#.
A colleague how has done a similar thing before used the following trick to keep changes to the constants in one place:
#if !__cplusplus
public const string
#else
static const TCHAR* const
#endif
XML_V1_TagRoot = "r";
The __cplusplus
is set depending on the compiler, so the prepeocessor makes each compiler see what it can compile.
Now I have the problem with a bunch of #define
statements of the type:
#define TX_TAG2xHWID_PARAMETER _T("PR")
Where _T()
is a macro. So I tried the following:
#if __cplusplus
#define TX_TAG2xHWID_PARAMETER _T("PR")
#else
internal const string TX_TAG2xHWID_PARAMETER = "PR";
#endif
Which does not work because C# has no value for the defines. source
Then I tried:
#if __cplusplus
#define TX_TAG2xHWID_PARAMETER \
#else
internal const string TX_TAG2xHWID_PARAMETER =
#endif
#if __cplusplus
_T(\
#endif
"PR"
#if __cplusplus
)
#else
;
#endif
Here the problem is that C# does not allow a multiline #define
.
Basically the issue is that a #define
is a preprocessor instruction in itself, but should be executed only if the file is compiled in a C++ project.
I have also played with the idea of making the C# project think it is a comment, with placing /*
and */
into a different #if
, but I had no success there.
So, does anyone know a solution how I can make the C# compiler not complain about that line which it should never try to compile?