2013-05-06 51 views
6

我有問題,當g ++在C++ 11模式下運行時,一些處理器宏不能正確擴展。這在使用Qt編譯程序的過程中會導致我遇到麻煩。在C++ 11模式下奇怪的g ++預處理器行爲

$ 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 Preprocesing它做正確的事。

$ 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)) 

此行爲是否打算,我可以做什麼來啓用擴展?

+2

下一次:總是包含實際的錯誤。 – Xeo 2013-05-06 10:09:46

回答

9

這是一個已知的問題,新的GCC行爲是由於新的C++ 11功能(即用戶定義的文字)而產生的。您可以在__FILE__QTOSTRING之前插入一個空格,以確保它始終被視爲單獨的令牌並進行擴展。

QT bugreport here