铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)*关于全局变量

时间:2019-07-03 14:59:25

标签: c++ g++ clang

我在编译文件时遇到问题。 我编译并收到此消息

"Undefined symbols for architecture x86_64:
"_airport_label", referenced from:
  FlightMap::pathfind_before(std::__1::basic_string<char, 
std::__1::char_traits<char>, std::__1::allocator<char> > const&,             
std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
  FlightMap::pathfind(std::__1::basic_string<char, 
std::__1::char_traits<char>, std::__1::allocator<char> > const&,  
std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
"_dijkstra", referenced from:
  FlightMap::findShortestRoute(std::__1::basic_string<char, 
std::__1::char_traits<char>, std::__1::allocator<char> > const&, 
std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
  FlightMap::pathfind_before(std::__1::basic_string<char, 
std::__1::char_traits<char>, std::__1::allocator<char> > const&, 
std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
  FlightMap::fun_dijkstra(std::__1::basic_string<char, 
std::__1::char_traits<char>, std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
 (maybe you meant: __ZN9FlightMap12fun_dijkstraERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE)
"_distance_label", referenced from:
  FlightMap::pathfind_before(std::__1::basic_string<char, 
std::__1::char_traits<char>, std::__1::allocator<char> > const&, 
std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
  FlightMap::pathfind(std::__1::basic_string<char, 
std::__1::char_traits<char>, std::__1::allocator<char> > const&, 
std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
"_path", referenced from:
FlightMap::pathfind(std::__1::basic_string<char, 
std::__1::char_traits<char>, std::__1::allocator<char> > const&, 
std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> > const&) in FlightMap-9a7ab0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)"

我认为FlightMap.h文件中的全局变量存在一些问题。
(我编辑了Flight.h-> FlightMap.h) 我声明了4个全局变量,如

stack<string> path;
map<string, string > airport_label;
map<double, string > distance_label;
map<string, double > dijkstra;

我已经尝试在每个全局变量的前面加上“ extern”,但这不起作用。

在FlightMap.h文件中的代码(已编辑Flight.h-> FlightMap.h)

#ifndef LAB6_FLIGHTMAP_H
#define LAB6_FLIGHTMAP_H

#include <iostream>
#include <string>
#include <stdexcept>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include "AdjacencyListDirectedGraph.h"

 extern stack<string> path;
 extern map<string, string > airport_label;
 extern map<double, string > distance_label;
 extern map<string, double > dijkstra;
 .
 .
 .

此外,这些全局变量在FlightMap.cpp文件中使用。(我编辑了!)

我认为这是发生问题的地方

1 个答案:

答案 0 :(得分:1)

在头文件的声明中使用标准名称:

#ifndef LAB6_FLIGHTMAP_H
#define LAB6_FLIGHTMAP_H

//#includes ...

extern std::stack<std::string> path;
extern std::map<std::string, std::string> airport_label;
extern std::map<double, std::string> distance_label;
extern std::map<std::string, double> dijkstra;
...
#endif

FlightMap.cpp中,您需要定义以下全局变量:

std::stack<std::string> path;
std::map<std::string, std::string> airport_label;
std::map<double, std::string> distance_label;
std::map<std::string, double> dijkstra;

extern告诉编译器变量定义在另一个翻译单元中的某个位置,然后将其解析为链接器。链接器现在必须在任何已编译文件中找到定义,但是由于在任何地方都没有定义,因此无法找到。通过将定义(即没有extern的文件)放到一个文件(仅一个文件)中,链接器将很高兴。