Solved

GCC C++: Defining unicode string const in macro

Posted on 2014-03-09
9
1,071 Views
Last Modified: 2014-03-10
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;

DEFINE_XML_TOKEN_STRING(CONFIG_ELM, config)

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'
0
Comment
Question by:jrheisey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39916651
I would try compiling with compiler flag -std=c++11. Will try it when I can.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39916673
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

0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 300 total points
ID: 39916680
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".
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39916685
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
0
 
LVL 1

Author Comment

by:jrheisey
ID: 39917929
Then the pre-processor is adding the space. It is not in the macro definition.
0
 
LVL 1

Author Comment

by:jrheisey
ID: 39918756
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39918785
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.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39919489
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60492

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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question