C++ "multiple definition of .. first defined here"
C++ "multiple definition of .. first defined here"
在C++中,有时候需要在不同文件中使用同一个变量。对于这类变量如果处理不当,很容易出现“multiple definition of... first defined here”的错误。
例如,定义了如下3个文件:global.h, a.cpp, b.cpp
//global.h:
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
const int a=1;
int b;
#endif
//a.cpp
#include <iostream>
#include <stdlib.h>
#include "global.h"
using namespace std;
void test1()
{
cout<<"test1"<<endl;
}
//b.cpp
#include <iostream>
#include <stdlib.h>
#include "global.h"
using namespace std;
void test2()
{
cout<<"test2"<<endl;
}
void main()
{
cout<<"hello world"<<endl;
}
执行编译命令:
g++ -o main a.cpp b.cpp
提示错误为:
[chris@zz jojo]g++ -o main a.cpp b.cpp
/tmp/ccc7OcsO.o:(.bss+0x0): multiple definition of `b'
/tmp/ccs7q2VA.o:(.bss+0x0):第一次在此定义
出错原因:a.cpp和b.cpp先分别被编译为.o格式的目标文件,两个目标文件再被链接器链接起来,这当中a.cpp和b.cpp分别进行了一次include,相当于global.h中的代码重复出现了一次。因为a是const类型,所以重新定义也没事;但是b只是普通变量,重复定义显然不行。
显然,一个解决办法是把b定义为const int类型。或者,定义成static int类型也行。
还有一种解决方案,就是把global.h变为global.c文件,a.cpp和b.cpp中不再include它,但是编译的时候把global.c也编译进去,就可以了:
g++ -o main global.c a.cpp b.cpp
再举一个class相关的例子。比如有Body和Mouth两个类,Body的greet方法会调用Mouth的say方法,而main函数中会调用全局变量body的greet方法。为了只是用一个body和一个mouth对象,可以这么写:
//body.h
#ifndef BODY_H
#define BODY_H
#include <mouth.h>
class Body {
public:
Body();
~Body();
void greet();
};
extern Body body;
#endif
//body.cpp
#include <body.h>
Body::Body(){}
Body::~Body() {}
void Body::greet()
{
mouth.say();
}
//mouth.h
#ifndef MOUTH_H
#define MOUTH_H
class Mouth
{
public:
Mouth();
~Mouth();
void say();
};
extern Mouth mouth;
#endif
//mouth.cpp
#include <mouth.h>
#include <iostream>
using namespace std;
Mouth::Mouth() {}
Mouth::~Mouth() {}
void Mouth::say()
{
cout << "Have a Nice day!" << endl;
}
//class.cpp
#include <body.h>
#include <mouth.h>
Body body;
Mouth mouth;
//main.cpp
#include <iostream>
#include <body.h>
using namespace std;
int main()
{
body.greet();
}
上面代码中的include,虽然都是用的尖括号,但因为编译时可以通过指定include路径,不会出问题~
编译命令:
g++ -I ./ mouth.cpp body.cpp class.cpp main.cpp -o main
能够正常运行。
C++ "multiple definition of .. first defined here"的更多相关文章
- stb_image multiple definition of first defined here 多文件包含问题
首先吐槽一下,网上的其他的一些内容都是瞎写,根本没有指出问题的根本原因,使用时出现异常情况不能自己解决也说明了C语言基础不牢固, 该头文件可以分为两种情况使用(推荐使用办法2,办法1中有解释原因)(任 ...
- multiple definition of `err_sys' 《UNIX环境高级编程》
本文地址:http://www.cnblogs.com/yhLinux/p/4079930.html 问题描述: [点击此处直接看解决方案] 在练习<UNIX环境高级编程>APUE程序清单 ...
- gcc: multiple definition of [转]
/home/tace/openav/source/SeamlessMessage/CPaoFlt.o: In function `CPaoFlt::get_m_strPrmair() const':C ...
- multiple definition of qt_plugin_query_metadata
dustije 5 years ago I have a project with several plugins i want to compile into one library. I get ...
- [err]multiple definition of `***'
err CMakeFiles/dsm.dir/src/main_stateEstimation.cpp.o: In function `align_mean(cv::Mat, cv::Rect_< ...
- 链接错误:multiple definition of 'xxx' 问题解决及其原理
内容借鉴 于CSDN炸鸡叔 错因 截图: “multiple definition of 'head' ” “multiple definition of 'tail' ” 解决过程: 1.首先要 ...
- multiple definition of XXX情况分析
近日在写代码,各个.cpp源文件编译时没有问题,将*.o进行链接时,出现了许多multiple definition of XXX的链接错误.于是在网上搜索了一番,结合自己的代码包含逻辑,最终发现了问 ...
- QT编译错误: multiple definition of `qMain(int, char**)'
QT使用过程中来回添加修改代码,结果出现了编译错误:error: multiple definition of `qMain(int, char**)' 一直看我的源文件是都哪里有错误,最后发现是在p ...
- qt用mingw编译时报错 multiple definition of
网上相关回答不少,但过于简单,这里做一下记录. qt用mingw编译程序时报“multiple definition of …”这个错误,错误信息大概是如下图所示: 1 2 3 首先,检查自己的程序是 ...
随机推荐
- 模拟退火算法 R语言
0 引言 模拟退火算法是用来解决TSP问题被提出的,用于组合优化. 1 原理 一种通用的概率算法,用来在一个打的搜索空间内寻找命题的最优解.它的原理就是通过迭代更新当前值来得到最优解.模拟退火通常使用 ...
- Android Studio 安装在Windows10中的陷阱
操作系统:Windows 10 Pro CPU:AMD IDE:Android Studio 2.0 JDK:8.0 安装完AS(Android Studio)之后,运行AS发现无法启动模拟器,提示“ ...
- state.sls与state.highstate区别
最近编写kubernetes的saltstack状态配置文件,在github上找到一个开源的salt文件,根据自己的需要,完成修改之后.执行部署测试 大致目录结构如下: |----k8s | |___ ...
- NATS_10:NATS服务配置文件详解
尽管NATS可以无配置的运行,但也可以使用配置文件配置NATS服务的启动,在实际应用当中,一般都是通过使用配置文件来启动服务的. 1. 配置项包括 客户端监听器端口 Client listening ...
- P3942 将军令
P3942 将军令 梦里,小 F 成了一个给将军送密信的信使. 现在,有两封关乎国家生死的密信需要送到前线大将军帐下,路途凶险,时间紧迫.小 F 不因为自己的祸福而避趋之,勇敢地承担了这个任务. 不过 ...
- P1783 二分并查集写法
并查集 + 二分 我是 并查集 + 二分 做的QVQ 思路:两两枚举点之间的距离,sort排序,使距离有序.二分答案,每次判断是否符合条件,然后缩小查询范围,直到满足题目要求(保留2位小数精度就为 0 ...
- 深入理解FIFO
深入理解FIFO(包含有FIFO深度的解释) FIFO: 一.先入先出队列(First Input First Output,FIFO)这是一种传统的按序执行方法,先进入的指令先完成并引退,跟着才执行 ...
- Shell记录-Shell命令(磁盘)
inux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式 df [选项] [文件] Shell ...
- jar包读取包内properties文件
properties位于src目录下 project --src -----package -----test.properties Properties p = new Properties(); ...
- call 大佬 help7——kmp 补齐 循环节
http://acm.hdu.edu.cn/showproblem.php?pid=3746 用kmp算法,那么 但是也等于上面的是正确的 也等于下面是错误的 why? #include<cst ...