GCC C++: Defining unicode string const in macro

This macro is intended to define a string as both ASICII and unicode.

#define DEFINE_XML_TOKEN_STRING(n, s) const char n##a[] = #s; const wchar_t n##w[] = L#s;


Show result in this code:

const char CONFIG_ELMa[] = "config"; const wchar CONFIG_ELMw[] = L"config";

Works with fine Visual Studio. The preprocessor in GCC seems to have a problem reporting error:
error: 'L' was not declared in this scope
note: in expansion of macro 'DEFINE_XML_TOKEN_STRING'
Who is Participating?
Duncan RoeSoftware DeveloperCommented:
Now I see your problem.  Your macro expands to
const char CONFIG_ELMa[] = "config"; const wchar_t CONFIG_ELMw[] = L "config";

Open in new window

Note the space after "L".
Duncan RoeSoftware DeveloperCommented:
I would try compiling with compiler flag -std=c++11. Will try it when I can.
Duncan RoeSoftware DeveloperCommented:
It works for me without any special flags. I have gcc 4.8.2 - what do you have?
Also I had to use wchar_t rather than wchar
14:53:04$ cat t.cpp
using namespace std;
int main (int argc, char **argv)
  const char CONFIG_ELMa[] = "config"; const wchar_t CONFIG_ELMw[] = L"config";
  return 0;
}                                  /* int main (int argc, char **argv) */
15:02:10$ cat Makefile 
t: t.cpp
        g++ $^ -o $@
15:02:14$ make
g++ t.cpp -o t

Open in new window

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Duncan RoeSoftware DeveloperCommented:
This macro works for g++
#define DEFINE_XML_TOKEN_STRING(n, s) const char n##a[] = #s; const wchar_t n##w[] = L###s;

Open in new window

You will have to test whether Visual Studio accepts it
jrheiseyAuthor Commented:
Then the pre-processor is adding the space. It is not in the macro definition.
jrheiseyAuthor Commented:
Duncan RoeSoftware DeveloperCommented:
Fine. But you now have a solution. Does Visual Studio accept the amended macro? If not, you will have to conditionally define the macro according to platform.
Duncan RoeSoftware DeveloperCommented:

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
You could most likely also use L"" #s too due to string concatenating rules.

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.