【发布时间】:2012-08-06 03:38:47
【问题描述】:
我在 c++ 中的乘法声明有问题,但在 c 中没有。 您可以查看代码以获取更多信息。
文件 main.c
#ifndef VAR
#define VAR
int var;
#endif
int main(){}
文件 other.c
#ifndef VAR
#define VAR
int var;
#endif
用 gcc 编译
gcc main.c other.c
>> success
用 g++ 编译
g++ main.c other.c
Output:
/tmp/ccbd0ACf.o:(.bss+0x0): multiple definition of `var'
/tmp/cc8dweC0.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
我的 gcc 和 g++ 版本:
gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 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.
g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 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.
【问题讨论】:
-
这很奇怪,虽然你过于复杂了:ideone.com/zdZUg vs ideone.com/swhxi
-
CFLAGS 和 CPPFLAGS 环境变量中有什么内容?
-
我尝试了 extern "C" 块,但它直到不起作用:(
-
我已将
other.c更改为other.h和g++编译正常。 -
@VictorHugo 这是作弊。
g++会将 .h 文件视为要预编译的头文件。由于 main.c 没有包含它,所以只有一个定义。
标签: c++ c gcc compiler-errors g++