Roman Pushkin
Roman Pushkin

Reputation: 6079

"Define" Macro Concatenation and _TEXT

is there any way of concatenation #define macros in c++ (ms compiler)?

I have existing code like this:

#define LOG_FILENAME _TEXT("VendorBlaBlaDriver.log")
#define REGISTRY_FILENAME _TEXT("VendorBlaBlaDriver")
#define VENDOR_NAME _TEXT("VendorBlaBla")

I wanna do the following:

#define NAME_PART1 "VenorBlaBla"
#define NAME_PART2 "Driver"

#define LOG_FILENAME _TEXT(NAME_PART1 NAME_PART2 ".log")
#define REGISTRY_FILENAME _TEXT(NAME_PART1 NAME_PART2)
#define VENDOR_NAME _TEXT(NAME_PART1)

But compiler gives me an error:

error C2308: concatenating mismatched strings

Is there any way of doing this right? The thing is that's a component and I want to specify -D option to compiler later. I don't want to store NAME_PART1 and NAME_PART2 in source code.

Upvotes: 0

Views: 1084

Answers (3)

MSalters
MSalters

Reputation: 179809

You can't apply the _TEXT macro twice. or on multiple strings, but you can concatenate the results. I.e.

#define LOG_FILENAME _TEXT(NAME_PART1) _TEXT(NAME_PART2) _TEXT(".log")

However, you really shouldn't bother. The _TEXT macro was used around the turn of the century, when there were still non-Unicode platforms around. Today we just write #define NAME_PART1 L"VenorBlaBla" ; nobody is selling Windows 95 software anymore.

Upvotes: 3

Skizz
Skizz

Reputation: 71070

Wouldn't this work (not got a compiler to hand yet to try it out):

#define LOG_FILENAME _TEXT(NAME_PART1) _TEXT(NAME_PART2) _TEXT(".log")

and then let the compiler concatenate the two strings.

Upvotes: 1

Geoffrey
Geoffrey

Reputation: 11353

This is because the _TEXT is being applied to NAME_PART1 only, and not the other parts, this should be written as:

#define NAME_PART1 "VenorBlaBla"
#define NAME_PART2 "Driver"

#define LOG_FILENAME      _TEXT(NAME_PART1) _TEXT(NAME_PART2) _TEXT(".log")
#define REGISTRY_FILENAME _TEXT(NAME_PART1) _TEXT(NAME_PART2)
#define VENDOR_NAME       _TEXT(NAME_PART1)

The mismatch is _TEXT is defining a wide-string, where as the other strings are still normal strings.

Upvotes: 1

Related Questions