错误 - ld返回1退出状态未定义引用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误 - ld返回1退出状态未定义引用相关的知识,希望对你有一定的参考价值。

我正在使用DevC ++ 5.11,我有一个包含5个文件的项目:

mainDistances.cpp

distances.cpp

distances.h

matrix.cpp

matrix.h

我收到这些错误:

[Error] ld returned 1 exit status
mainDistances.cpp: undefined reference to `Matrix<float>::operator=(Matrix<float> const&)'
mainDistances.cpp:(.text+0xb5f): undefined reference to `Matrix<float>::rows()'
[...]distances.o   distances.cpp:(.rdata$_ZTV6MatrixIiE[_ZTV6MatrixIiE]+0x18): undefined reference to `Matrix<int>::readMatrix(std::string const&)'

AND MORE

几乎所有东西看起来都不明确。它就像它不读取其他文件。

# Project: mainDistances
# Makefile created by Dev-C++ 5.11

CPP      = g++.exe
CC       = gcc.exe
WINDRES  = windres.exe
OBJ      = distances.o mainDistances.o matrix.o
LINKOBJ  = distances.o mainDistances.o matrix.o
LIBS     = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc
INCS     = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include"
CXXINCS  = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
BIN      = mainDistances.exe
CXXFLAGS = $(CXXINCS) -std=c++11
CFLAGS   = $(INCS) -std=c++11
RM       = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
    ${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
    $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)

distances.o: distances.cpp
    $(CPP) -c distances.cpp -o distances.o $(CXXFLAGS)

mainDistances.o: mainDistances.cpp
    $(CPP) -c mainDistances.cpp -o mainDistances.o $(CXXFLAGS)

matrix.o: matrix.cpp
    $(CPP) -c matrix.cpp -o matrix.o $(CXXFLAGS)

这些文件每个都有近200行,所以我不打算把这个帖子变得很大,但请告诉我你是否需要其他代码部分。

distances.h

#ifndef DISTANCES_H
#define DISTANCES_H

#include "matrix.h"

template<class T>
class Distances : public Matrix<T> {

 public:

  using Matrix<T>::matr_;
  using Matrix<T>::mRows;
  using Matrix<T>::mCols;
  using Matrix<T>::operator+;
  using Matrix<T>::operator+=;
  using Matrix<T>::operator=;

  Distances(size_t first, size_t second) : Matrix<T>(first, second) {};
  Distances(size_t first, size_t second, T value) : Matrix<T>(first, second, value) {};

  //Costruttore senza parametri. Vengono utilizzati quelli di Matrix
  Distances(void) : Matrix<T>() {};

  //Costruttori che permettono il confronto
  static bool equal(const Distances<T>& , const Distances<T>& );
  bool operator == ( const Distances<T>& rsh) const;

  //Costruttori di lettura, scrittira e stampa della Matrice
  void readMatrix (const std::string &filename = "distIn.txt");
  void writeMatrix(const std::string &filename = "distOut.txt");
  void print(const std::string s);

  //Ridefinizione dell'operatore *
  virtual Distances<T>& operator*(const Distances<T>& );

  //Costruttori delle funzioni che risolvono il minPath
  static Distances<T> minPathFunct(const Distances<T>&, int y );
  static Distances<T> minPathFW(const Distances<T>& , int y);

};

#endif

distaces.cpp

#include "distances.h"


/** Funzione che ci permette di leggere la Matrice del file **/
template <class T>
void Distances<T>::readMatrix(const std::string &filename) {

    std::ifstream file;
    file.open(filename, std::ios::in); //Apriamo il file

    //Controlliamo la presenza del file
    if(!file.is_open()) {
      std::cout << "Could not open file " << filename << std::endl;
      return;
    }

    //Dichiariamo le variabili utili per l'estrazione dei valori della Matrice
    std::string line, str;
    T value;
    int rows = 0;

    //Ciclo while che ci permette di estrarre i valori matriciali dalle n-righe
    while(std::getline(file, line)) {

      std::istringstream iss(line);
      rows++;
      int cols = 0;

      //Ciclo while che estrae ogni valore di una riga
      while(iss >> str) {
        cols++;
        if (str=="*") value = std::numeric_limits<T>::max();
        else value = stof(str);
        matr_.push_back(value);
      }

      mCols = cols;

    }

    mRows = rows;

}


/** Funzione di scrittura della Matrice ottenuta in un file di testo **/
template <class T>
void Distances<T>::writeMatrix(const std::string &filename) {

    //Apertura del file
    std::ofstream file;
    file.open(filename, std::ios::out);

    if(!file.is_open()) {
      std::cout << "Could not open file " << filename << std::endl;
      return;
    }

    T value;
    std::string str;

    //Cicli for che ci permettono il salvataggio del valore M[i][j] nel file testo
    for (int i=0;i<mRows;i++) {
      for(int j=0;j<mCols;j++) {
        value = matr_[i*mRows + j];
        str =std::to_string(value);
        if (value == std::numeric_limits<T>::max()) str = "*";
        file << std::setw(7) << str << " ";
      }
      file << endl;
    }

    //Chiusura del file
    file << endl;
    file.close();

}

[...]

template class Distances<int>;
template class Distances<float>;
答案

您的代码可能称为math.h,因为c ++默认称为libc.so(它不需要add -lc参数)。如果您调用math.h,则需要添加“-lm”来添加libm。所以。希望它可以帮助你。我的英语非常糟糕,对不起...

以上是关于错误 - ld返回1退出状态未定义引用的主要内容,如果未能解决你的问题,请参考以下文章

C ++错误:对“”的未定义引用,并且ld返回了1个退出状态[重复]

C++ 错误:对“<func name>”的未定义引用和 ld 返回 1 个退出状态 [重复]

错误:未找到架构 x86_64 的符号,collect2:ld 返回 1 退出状态

多重定义collect2:错误:ld在C中返回1退出状态

c++中对静态变量的未定义引用

collect2:错误:ld 返回 1 退出状态 graphics.h