c**y 发帖数: 172 | 1 我的下面的程序,在gcc可以编译通过,但是g++抱怨multi-definition of x。哪个大
侠指点一下为什么g++报这个错。
====程序====
1.m.c
#include
#include "l1.h"
extern void incx();
int main() {
x = 10;
px();
incx();
return 0;
}
2.l1.h
int x;
void px();
3.l1.c
#include
#include "l1.h"
void px()
{
printf("x = %d\n",x);
}
4.l2.c
#include
#include "l1.h"
void incx()
{
x += 1;
px();
}
========
gcc编译命令
gcc m.c l1.c l2.c
g++编译命令及报错
-bash-2.05b$ g++ m.c l1.c l2.c
/tmp/cc4JpQXE.o(.bss+0x0): multiple definition of `x'
/tmp/cccfPl17.o(.bss+0x0): first defined here
/tmp/cc677Tsg.o(.bss+0x0): multiple definition of `x'
/tmp/cccfPl17.o(.bss+0x0): first defined here
collect2: ld returned 1 exit status |
X****r 发帖数: 3557 | 2 l1.h里改为
extern int x;
l1.c里加入
int x;
C和C++在这个地方的确是不一样的。
【在 c**y 的大作中提到】 : 我的下面的程序,在gcc可以编译通过,但是g++抱怨multi-definition of x。哪个大 : 侠指点一下为什么g++报这个错。 : ====程序==== : 1.m.c : #include : #include "l1.h" : extern void incx(); : int main() { : x = 10; : px();
|
p*********t 发帖数: 2690 | 3 看这几个程序,思维真乱.
gcc 一般编译 c 程序;
g++ 编译 c++ 程序.
【在 c**y 的大作中提到】 : 我的下面的程序,在gcc可以编译通过,但是g++抱怨multi-definition of x。哪个大 : 侠指点一下为什么g++报这个错。 : ====程序==== : 1.m.c : #include : #include "l1.h" : extern void incx(); : int main() { : x = 10; : px();
|