由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C 和 C++ 的区别
相关主题
请问c++为什么会编译失败?G++用-g和-O3编译运行结果竟然不一样
C code参数传递出错可能的原因[合集] 一道C++面试题 (转载)
[合集] Intel 9编译器在vc 6.0的环境里编译openmp的问题大家帮我看看这个C程序为什么出错了
请教如何修正这个C程序的bug。namespace defined in another file
关于在C中定义常量微软VS修改bug的效率不行啊
求助 怎么编辑 多个 .c files(比如a.c, b.c) 和一个.h file(ab (转载)C++小问题
请教一个用gsl库的问题[合集] 6个变态的C语言写的Hello World (ZZ)
[合集] 这段C++程序哪种写法是正确的问一个defining array 的问题
相关话题的讨论汇总
话题: 0x0话题: include话题: bss话题: incx话题: tmp
进入Programming版参与讨论
1 (共1页)
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();

1 (共1页)
进入Programming版参与讨论
相关主题
问一个defining array 的问题关于在C中定义常量
请教一个弱弱问题,关于#include的pathname求助 怎么编辑 多个 .c files(比如a.c, b.c) 和一个.h file(ab (转载)
添加C++ std lib请教一个用gsl库的问题
Intel C++ compiler 求教[合集] 这段C++程序哪种写法是正确的
请问c++为什么会编译失败?G++用-g和-O3编译运行结果竟然不一样
C code参数传递出错可能的原因[合集] 一道C++面试题 (转载)
[合集] Intel 9编译器在vc 6.0的环境里编译openmp的问题大家帮我看看这个C程序为什么出错了
请教如何修正这个C程序的bug。namespace defined in another file
相关话题的讨论汇总
话题: 0x0话题: include话题: bss话题: incx话题: tmp