g ++不链接并抛出未定义的引用

时间:2017-01-09 09:34:31

标签: c++ reference compilation linker

抱歉,这可能是一个非常愚蠢的初学者问题。我试图编译一个程序,但我只是不断收到错误。我已经看过一些教程和阅读线程,但适用于其他人的方法对我来说似乎并不适用。我用cygwin的windows。要编译我使用:

/cygdrive/c/Users/juris/AppData/Local/Temp/ccc9S76y.o: In function `operator<<(std::ostream&, Node*)':
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:146: undefined reference to `std::ostream& operator<< <Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&)'
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:146:(.text+0xf98): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `std::ostream& operator<< <Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&)'
/cygdrive/c/Users/juris/AppData/Local/Temp/ccc9S76y.o: In function `Node::calcLowerBound()':
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:278: undefined reference to `std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > > operator-<Vertex*, bool>(std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > >, std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > >)'
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:278:(.text+0x1c95): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > > operator-<Vertex*, bool>(std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > >, std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > >)'
collect2: error: ld returned 1 exit status

我收到错误:

...
std::map<Vertex*, std::map<Vertex*, int>> Node::getConstraints(){
    return constraints;
}

std::ostream& operator<<(std::ostream& os, Node *N){
    os << "\r\nNode:" << N -> getLB() << ":\r\n" << N -> getConstraints();
    if(N -> left() != nullptr){
        os << "\r\nlchild " << N -> left();
    }
    if(N -> right() != nullptr){
        os << "\r\nrchild " << N -> right();
    }
    return os;
}

branch_and_bound.cpp (第138行)

#ifndef _Branch_and_bound
#define _Branch_and_bound

#include "vertex.hpp"
#include "graph.hpp"
#include "utils.hpp"

#include "my_globals.hpp"
#include "my_operators.hpp"


class Node{
...

因此错误在N - >; getConstraings();,它是地图中的地图,我试图打印它。

branch_and_bound.hpp

#ifndef _utils
#define _utils

#include <stdlib.h>
#include <map>
#include <string>
#include <iostream>
#include <chrono>
#include <fstream>

#include "coord_visual.cpp"
#include "vertex.hpp"
#include "graph.hpp"

#include "my_globals.hpp"

void printPath(Tour ll, std::string name = "", bool showSize = true);
void printMap(std::map<Vertex*, auto> map, std::string name = "");
void printVector(std::vector<auto> v);
void timestamp(bool renew = false);
Graph* generateRandomGraph(int v_count = 0, int link_count = 10);
void exportGraphToCSV(Graph &gg, std::string name = "");
void swap(int &a, int &b);
std::ostream& recursiveMapPrint(std::ostream& os, const auto not_map, int recurse = 1);
std::ostream& recursiveMapPrint(std::ostream& os, const std::map<auto, auto> &M, int recurse = 0);
template <class T> std::vector<T> operator-(std::vector<T> A, std::vector<T> B);
template <class K, class V> std::map<K, V> operator-(std::map<K, V>A, std::map<K, V>B);
std::ostream& operator<<(std::ostream& os, const std::map<auto, auto> &M);
std::ostream& operator<<(std::ostream& os, const std::vector<auto> &V);
std::ostream& operator<<(std::ostream& os, Vertex *V);
template <class T> std::vector<T> operator-(std::vector<T> A, std::vector<T> B);
template <class K, class V> std::map<K, V> operator-(std::map<K, V>A, std::map<K, V>B);
template <class K, class V> std::ostream& operator<<(std::ostream& os, const std::map<K, V> &M);
template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T> &V);
Tour twoOptSwap(Tour T, int from, int to);

#endif

这里重要的一行是包含utils.hpp

utils.hpp

...
template <class K, class V> std::ostream& operator<<(std::ostream& os, const std::map<K, V> &M){
    recursiveMapPrint(os, M);
    return os;
}

utils.cpp

/cygdrive/c/Users/juris/AppData/Local/Temp/ccB8CmGa.o: In function `std::ostream& operator<< <Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&)':
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:170: undefined reference to `std::ostream& recursiveMapPrint<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&, int)'
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:170:(.text$_ZlsIP6VertexSt3mapIS1_iSt4lessIS1_ESaISt4pairIKS1_iEEEERSoSA_RKS2_IT_T0_S3_ISB_ESaIS5_IKSB_SC_EEE[_ZlsIP6VertexSt3mapIS1_iSt4lessIS1_ESaISt4pairIKS1_iEEEERSoSA_RKS2_IT_T0_S3_ISB_ESaIS5_IKSB_SC_EEE]+0x22): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `std::ostream& recursiveMapPrint<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&, int)'
collect2: error: ld returned 1 exit status

所以应该落入显示的utils.cpp函数中,而是得到一个未定义的引用。如果我将该函数复制到branch_and_bound,那么错误只需向上移动一步:

edx

看起来好像branch_and_bound没有与utils链接,感觉就像我被阻滞并且遗漏了一些明显的东西。我在这做错了什么?

0 个答案:

没有答案