简体   繁体   English

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

[英]clang: error: linker command failed with exit code 1 (use -v to see invocation) *about global variables

I have a problem with compiling my files. 我在编译文件时遇到问题。 I compiled and got this message 我编译并收到此消息

"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)"

I think there are some problems in global variable in FlightMap.h file. 我认为FlightMap.h文件中的全局变量存在一些问题。
(I edited Flight.h -> FlightMap.h) I declared 4 global variables like (我编辑了Flight.h-> FlightMap.h)我声明了4个全局变量,例如

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

I have already tried to put "extern" in front of each global variable but it does not work. 我已经尝试将“ extern”放在每个全局变量的前面,但是它不起作用。

In FlightMap.h file code(Edited Flight.h -> FlightMap.h) 在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;
 .
 .
 .

Also, those global variables are used in FlightMap.cpp file.(I edited!) 另外,这些全局变量在FlightMap.cpp文件中使用。(我编辑了!)

I think that is where problems occur 我认为那是发生问题的地方

Use fully qualified names in your header file's declarations: 在头文件的声明中使用标准名称:

#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

And in FlightMap.cpp you need to define these global variabes: 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 tells the compiler that the variable will be defined somewhere, in another translation unit, and leaves resolving this up to the linker. extern告诉编译器该变量在另一个翻译单元的某个位置定义,然后将其解析给链接器。 The linker now has to find the definition in any of the compiled files, but can't since there is no definition anywhere. 链接器现在必须在任何已编译文件中找到定义,但是由于在任何地方都没有定义,因此无法找到。 By actually putting the definition (that is, without extern ) in one file (and one file only), the linker will be happy. 通过将定义(即,不带extern )实际放在一个文件(仅一个文件)中,链接器将很高兴。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 clang:错误:在cmake中链接库时,链接器命令失败,退出代码为1(使用-v查看调用) - clang: error: linker command failed with exit code 1 (use -v to see invocation) when linking library in cmake VSCode:clang:错误:linker 命令失败,退出代码为 1(使用 -v 查看调用) - VSCode: clang: error: linker command failed with exit code 1 (use -v to see invocation) clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)控制台应用程序 - clang: error: linker command failed with exit code 1 (use -v to see invocation) console app 铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)MINIX3 - clang: error: linker command failed with exit code 1 (use -v to see invocation) MINIX3 铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)-Qt Creator 3.3 - clang: error: linker command failed with exit code 1 (use -v to see invocation) - Qt Creator 3.3 如何修复 clang:错误:linker 命令失败,退出代码为 1(使用 -v 查看调用) - How do I fix a clang: error: linker command failed with exit code 1 (use -v to see invocation) 编译我的 C++ 程序:clang: error: linker command failed with exit code 1(使用 -v 查看调用) - Compiling my c++ program: clang: error: linker command failed with exit code 1 (use -v to see invocation) Cocos2d-x-铛:错误:链接器命令失败,退出代码为1(使用-v查看调用) - Cocos2d-x - clang: error: linker command failed with exit code 1 (use -v to see invocation) clang++:错误:linker 命令在带有 ffmpeg 的 cpp 中失败,退出代码为 1(使用 -v 查看调用) - clang++: error: linker command failed with exit code 1 (use -v to see invocation) in cpp with ffmpeg 获取错误clang:错误:从终端编译C ++文件时,链接器命令失败,退出代码为1(使用-v查看调用) - Getting error clang: error: linker command failed with exit code 1 (use -v to see invocation) while compile C++ file from terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM