g++ 链接问题

_画船听雨眠 _画船听雨眠 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#ifndef _MAP_TEST_H
#define _MAP_TEST_H

#include <map>
#include <string>

using std::map;
using std::string;

map<string, string> map_config;

void init_map_config();

#endif

以上是map_test.h


#include "map_test.h"
void init_map_config()
{
map_config.insert({"123", "123"});
map_config.insert({"456", "456"});
map_config.insert({"789", "789"});
}

以上是map_test.cpp


#include <iostream>
#include "map_test.h"

using std::cout;
using std::endl;

int main()
{
init_map_config();

for (auto it = map_config.begin(); it != map_config.end(); it++)
cout << it->first << " " << it->second << endl;
cout << endl;
}

以上是main.cpp

编译
g++ -c map_test.cpp -o map.o -std=c++11
g++ -c main.cpp -o main.o -std=c++11
链接
g++ map.o main.o -o main
链接时出错:
main.o:(.bss+0x0): `map_config'被多次定义
map.o:(.bss+0x0):第一次在此定义
collect2: error: ld returned 1 exit status


这是为什么呢?
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
_画船听雨眠
C++ 语言 65,214 成员 250,514 内容