我最近将我的GCC(MinGW)升级到版本4.8.1并遇到-D选项的问题。例如,以下 main.c 代码
#include <stdio.h>
int main(void) {
printf("Hello world!\n");
#ifdef TEST
printf("We are in testing mode!\n");
#endif // TEST
return 0;
}
打印
当我输入时Hello world
:
gcc -DTEST main.c -o main.exe
但以下两项工作:
gcc -DTEST= main.c -o main.exe
gcc -DTEST=1 main.c -o main.exe
我之前的GCC版本(我相信4.6)没有这个问题。这是一个错误吗?
以下是gcc -v -DTEST -E -dM -c main.c
的已编辑输出转储。请注意#define test 1
。
...
#define STDOUT_FILENO 1
#define test 1
#define _PID_T_
#define __LDBL_MIN_EXP__ (-16381)
...
Using built-in specs.
COLLECT_GCC=c:\MinGW\bin\gcc.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=m
ingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto
--enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++
,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-l
ibstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gm
p-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --
with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-
libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/
mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)
COLLECT_GCC_OPTIONS='-v' '-D' 'TEST' '-E' '-dM' '-c' '-mtune=generic' '-march=pe
ntiumpro'
c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/cc1.exe -E -quiet -v -iprefix c:\ming
w\bin\../lib/gcc/mingw32/4.8.1/ -D TEST main.c -mtune=generic -march=pentiumpro
-dM
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/inclu
de"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../..
/../../include"
ignoring nonexistent directory "/mingw/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/inclu
de-fixed"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../..
/../../mingw32/include"
ignoring nonexistent directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include-fixed
c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../mingw32/include
End of search list.
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/;c:/mingw/bin/../libexec
/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/4.8.1/;c:/mingw/bin/../lib/gcc/;c:/
mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/;c:/mingw/bin/../lib/
gcc/mingw32/4.8.1/../../../
COLLECT_GCC_OPTIONS='-v' '-D' 'TEST' '-E' '-dM' '-c' '-mtune=generic' '-march=pe
ntiumpro'
答案 0 :(得分:0)
我确认了最新的MinGW上的错误:
使用MinGW(当前存储库版本)中提供的GCC 4.8.1,使用-DDEBUG定义小写调试符号。
以下是针对我的简化测试案例:
文件&#39; toto.cpp&#39;:
int debug = 1;
命令行:
$ g++ -c -DDEBUG toto.cpp
<command-line>:0:7: error: expected unqualified-id before numeric constant
toto.cpp:1:5: note: in expansion of macro 'debug'
int debug = 1;
^
有一个小写&#39; DEBUG&#39;在预处理文件中,显然是假的:
$ g++ -E -dMID -pipe -DDEBUG toto.cpp
[snip]
#define __WINNT__ 1
# 1 "toto.cpp"
#define WINNT 1
# 1 "toto.cpp"
#define _INTEGRAL_MAX_BITS 64
# 1 "toto.cpp"
#define __declspec(x) __attribute__((x))
# 1 "toto.cpp"
#define __DECIMAL_BID_FORMAT__ 1
# 1 "<command-line>"
#define debug 1
# 1 "toto.cpp"
int 1 = 1;
是的,这个版本的GCC的主要问题......