Solved

GCC C++: Defining unicode string const in macro

Posted on 2014-03-09
9
1,001 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
  • 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now