C和C++混合编程相关问题解决方法
来源:互联网 发布:c语言在线手册 编辑:程序博客网 时间:2024/04/30 03:36
一 :解决C或C++中的multiple definition of问题
server.cpp
clientp2p.cpp
#include "exception.h"
#include "clientp2p.h"
clientp2p.h中写有所有的全局变量及其初始化值 和函数声明
1.server.cpp中:
引用:
#include "clientp2p.h"
int Main(....)
{
...
}
2.clientp2p.cpp中,
也引用:
#include "clientp2p.h"
....
3. file_2.cpp
#include "global.h"
...
G++在编译的时候就会出错:
clientp2p.o:(.bss+0x0): multiple definition of `ClientList'
server.o:(.bss+0x0): first defined here
原因是因为在多次包含clientp2p.h时重复定义了变量和函数。
解决方法:
方法一:
在clientp2p.c(或.cpp) 中声明变量(不初始化),然后头文件clientp2p.h中在所有的变量声明前加上extern
如 extern int flag;
然后在其他需要使用全局变量的 cpp文件中包含.h 文件而不要包含 .cpp 文件。编译器会为global.cpp 生成目标文件,然后连接时,在使用全局变量的文件中就会连接到此文件 。
方法二:
在clientp2p.h中加入防止多次重复定义的宏判断符号
你的.H里面要加上条件编译
#ifndef GLOBAL
#define GLOBAL
XXXXX
XXXXX
#endif
记住:在ifndef时一定要在第一行,前边不要有任何的注释或语句。
二:
undefined reference to `std::ios_base::Init::Init() 解决
在linux下编译c++ 程序要使用g++ 编译器,如果你要是使用gcc编译器就会有上面的报错信息,只要在在gcc后加上 -lstdc++就ok了
eg: gcc example.c -lstdc++
- C和C++混合编程相关问题解决方法
- C和汇编混合编程
- c和C++混合编程
- c++和c混合编程
- C和C++混合编程
- 汇编和c混合编程
- C和C++混合编程
- C和C++混合编程
- C 和 C++ 混合编程
- Matlab和C混合编程
- C和C++混合编程
- c和matlab混合编程
- C和C++混合编程
- C和C++混合编程
- C和C++混合编程
- C和MATLAB混合编程
- C和C++混合编程
- C++和C混合编程
- mysql 表数据备份和恢复
- DB2 转Oracle
- http-equiv是什么意思
- sqlplus中利用spool生成带日期的文件名
- Unix 正则表达式
- C和C++混合编程相关问题解决方法
- 2410的初始化流程(设备初始化)
- IE 9: Hardware rendering, new JS engine, CSS, standards, and more
- .net-【20091119】Could not load type 'System.DateTimeOffset'的问题
- 最令PHP初学者头痛的十四个问题
- 【转帖】 IEEE 1588同步机制
- 牛人的话
- tomcat配置https
- oracle 12479