[英]clang: error: linker command failed with exit code 1 (use -v to see invocation) when linking library in cmake
[英]clang: error: linker command failed with exit code 1 (use -v to see invocation) *about global variables
我在編譯文件時遇到問題。 我編譯並收到此消息
"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文件中使用。(我編輯了!)
我認為那是發生問題的地方
在頭文件的聲明中使用標准名稱:
#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
)實際放在一個文件(僅一個文件)中,鏈接器將很高興。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.