6

我的问题是,当 g++ 在 c++11 模式下运行时,某些预处理器宏未正确扩展。这在使用 Qt 编译程序期间给我带来了麻烦。

$ g++ --version
g++ (GCC) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

以下片段暴露了问题:

$ cat foo.cpp
//#include <QtGui>
#define QTOSTRING_HELPER(s) #s
#define QTOSTRING(s) QTOSTRING_HELPER(s)
#ifndef QT_NO_DEBUG
# define QLOCATION "\0"__FILE__":"QTOSTRING(__LINE__)
# define METHOD(a)   qFlagLocation("0"#a QLOCATION)
# define SLOT(a)     qFlagLocation("1"#a QLOCATION)
# define SIGNAL(a)   qFlagLocation("2"#a QLOCATION)
#else
# define METHOD(a)   "0"#a
# define SLOT(a)     "1"#a
# define SIGNAL(a)   "2"#a
#endif

METHOD(grml)

在没有 c++11 的情况下对其进行预处理是正确的。

$ g++ -E foo.cpp
# 1 "foo.cpp"
# 1 "<command-line>"
# 1 "foo.cpp"
# 15 "foo.cpp"
qFlagLocation("0""grml" "\0""foo.cpp"":""15")

但是在 C++11 模式下,QTOSTRING 宏没有得到扩展,导致源代码行出现编译错误。

$ g++ -std=c++11 -E foo.cpp
# 1 "foo.cpp"
# 1 "<command-line>"
# 1 "foo.cpp"
# 15 "foo.cpp"
qFlagLocation("0""grml" "\0"__FILE__":"QTOSTRING(15))

这种行为是有意的,我可以做些什么来启用扩展?

4

1 回答 1

9

这是一个已知问题,新的 GCC 行为是由于新的 C++11 特性(即用户定义的文字)而有意的。您可以在前面插入一个空格__FILE__QTOSTRING以确保它始终被视为单独的标记并因此展开。

QT 错误报告在这里

于 2013-05-06T10:00:34.097 回答