MinGW g++ 编译带静态变量的类时的问题,求高手解释一下!
2011-07-19 00:12
375 查看
打算建一个类,每生成一个对象就自动获取一个唯一的ID。
main.cpp
#include "stdio.h"
#include "CNode.h"
int main(int argc, char** argv)
{
CNode cNode1;
CNode cNode2;
CNode cNode3;
printf("cNode1.ID = %d\n ", cNode1.ulID);
printf("cNode2.ID = %d\n ", cNode2.ulID);
printf("cNode3.ID = %d\n ", cNode3.ulID);
}
CNode.h
#ifndef CNODE__
#define CNODE__
class CNode {
public:
CNode();
virtual ~CNode();
static unsigned int ulIDCreator;
unsigned int ulID;
};
unsigned int CNode::ulIDCreator = 0;
#endif
CNode.cpp
#include "CNode.h"
CNode::CNode(){
ulID = ++ulIDCreator;
}
CNode::~CNode(){
}
*** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\CNode.o ..\src\CNode.cpp
g++ -oArctic.exe src\Main.o src\CNode.o
src\CNode.o: In function `ZN5CNodeC1Ev':
E:/EclipseWorkspace/Arctic/Debug/../src/CNode.cpp:9: multiple definition of `CNode::CNode()'
src\Main.o:E:/EclipseWorkspace/Arctic/Debug/../src/Main.cpp:13: first defined here
src\CNode.o: In function `ZN5CNodeD1Ev':
E:/EclipseWorkspace/Arctic/Debug/../src/CNode.cpp:13: multiple definition of `CNode::~CNode()'
src\Main.o:E:/EclipseWorkspace/Arctic/Debug/../src/Main.cpp:13: first defined here
src\CNode.o: In function `ZN5CNodeD0Ev':
E:/EclipseWorkspace/Arctic/Debug/../src/CNode.cpp:13: multiple definition of `CNode::~CNode()'
src\Main.o:E:/EclipseWorkspace/Arctic/Debug/../src/Main.cpp:13: first defined here
src\CNode.o: In function `ZN5CNodeC2Ev':
E:/EclipseWorkspace/Arctic/Debug/../src/CNode.cpp:9: multiple definition of `CNode::ulIDCreator'
src\Main.o:E:/EclipseWorkspace/Arctic/Debug/../src/Main.cpp:13: first defined here
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 687 ms.
很奇怪的问题!怀疑是在不同文件中编译再连接在一块有问题。
于是去掉了CNode.cpp,只剩CNode.h。结果问题消失,
#ifndef CNODE__
#define CNODE__
class CNode {
public:
CNode(){ulID = ++ulIDCreator;};
virtual ~CNode(){};
static unsigned int ulIDCreator;
unsigned int ulID;
};
unsigned int CNode::ulIDCreator = 0;
#endif
console:
cNode1.ID = 1
cNode2.ID = 2
cNode3.ID = 3
本文出自 “general195” 博客,请务必保留此出处http://general.blog.51cto.com/927298/616159
main.cpp
#include "stdio.h"
#include "CNode.h"
int main(int argc, char** argv)
{
CNode cNode1;
CNode cNode2;
CNode cNode3;
printf("cNode1.ID = %d\n ", cNode1.ulID);
printf("cNode2.ID = %d\n ", cNode2.ulID);
printf("cNode3.ID = %d\n ", cNode3.ulID);
}
CNode.h
#ifndef CNODE__
#define CNODE__
class CNode {
public:
CNode();
virtual ~CNode();
static unsigned int ulIDCreator;
unsigned int ulID;
};
unsigned int CNode::ulIDCreator = 0;
#endif
CNode.cpp
#include "CNode.h"
CNode::CNode(){
ulID = ++ulIDCreator;
}
CNode::~CNode(){
}
*** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\CNode.o ..\src\CNode.cpp
g++ -oArctic.exe src\Main.o src\CNode.o
src\CNode.o: In function `ZN5CNodeC1Ev':
E:/EclipseWorkspace/Arctic/Debug/../src/CNode.cpp:9: multiple definition of `CNode::CNode()'
src\Main.o:E:/EclipseWorkspace/Arctic/Debug/../src/Main.cpp:13: first defined here
src\CNode.o: In function `ZN5CNodeD1Ev':
E:/EclipseWorkspace/Arctic/Debug/../src/CNode.cpp:13: multiple definition of `CNode::~CNode()'
src\Main.o:E:/EclipseWorkspace/Arctic/Debug/../src/Main.cpp:13: first defined here
src\CNode.o: In function `ZN5CNodeD0Ev':
E:/EclipseWorkspace/Arctic/Debug/../src/CNode.cpp:13: multiple definition of `CNode::~CNode()'
src\Main.o:E:/EclipseWorkspace/Arctic/Debug/../src/Main.cpp:13: first defined here
src\CNode.o: In function `ZN5CNodeC2Ev':
E:/EclipseWorkspace/Arctic/Debug/../src/CNode.cpp:9: multiple definition of `CNode::ulIDCreator'
src\Main.o:E:/EclipseWorkspace/Arctic/Debug/../src/Main.cpp:13: first defined here
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 687 ms.
很奇怪的问题!怀疑是在不同文件中编译再连接在一块有问题。
于是去掉了CNode.cpp,只剩CNode.h。结果问题消失,
#ifndef CNODE__
#define CNODE__
class CNode {
public:
CNode(){ulID = ++ulIDCreator;};
virtual ~CNode(){};
static unsigned int ulIDCreator;
unsigned int ulID;
};
unsigned int CNode::ulIDCreator = 0;
#endif
console:
cNode1.ID = 1
cNode2.ID = 2
cNode3.ID = 3
本文出自 “general195” 博客,请务必保留此出处http://general.blog.51cto.com/927298/616159
相关文章推荐
- 高手解释一下 :python两个模块的交叉编译
- 使用网页文本编辑器的问题,请高手指点一下
- ffmpeg1.1.4 实现ffmpeg demo tutorial01。 mark一下其中库的接口更新以及编译问题
- i=i+1 i++ ++I的区别? 请高手解释一下.
- 求高手解释关于CSS特殊性算法的问题
- 请教高手:关于在delphi中编译时对输出文件分类的问题?
- FFMPEG 编译的一些问题 标记一下
- 遇到个问题想发帖、发现积分不够、有高手路过指点一下吧!
- 编引用之swap()---高手帮忙写下笔记,还有解决一下问题,谢谢了
- 关于使用Android6.0编译程序时,出现getSlotFromBufferLocked: unknown buffer: 0xac0f8650问题的解释
- python语言的解释性执行问题(备份一下,以供以后学习参考)
- 那位高手能给解释一下,在SqlDataReader读数据和直接返回DataTable(DataSet)之类的那个好?
- 数据结构 栈问题(在TC能运行,但是是死循环,请高手帮忙调试一下,谢谢~)
- 问几个 User.GetUser()的问题。各位高手来帮忙回答一下
- 请Flash高手回答一下我的问题。急!!!!!
- 小弟的程序有点问题 请高手帮忙改一下
- win tc的一个完整代码,编译正确,但黑屏,请高手帮忙指导你一下!!!
- 有哪位高手帮忙解决一下问题!!!DataContract 的using引用指令用什么?
- java 请JAVA高手回答一下小弟这个简单的问题 不胜感激啊
- 求信息高手参考一下~我说的问题??谢谢