即使包含库也未定义对某些功能的引用

时间:2018-09-19 13:24:22

标签: c++ linker

我正在制作一个简单的程序来计算矩阵。共有3个文件,mat.hmat.cppmain.cpp

mat.h的内容:

#ifndef MAT_H
#define MAT_H

#include <array>

template<.....>
class Mat {
    ...
};

#endif

mat.cpp的内容:

#include "mat.h"

using std::array;

// Definitions of Mat functions/constructors

main.cpp的内容:

#include <iostream>
#include "mat.h"
using namespace std;

int main() {
    //Here I create a couple of Mat objects.
    return 0;
}

问题是,当我使用g++ mat.cpp main.cpp进行编译时,它给了我一个链接器错误,指出mat.cpp中定义的所有函数均未定义(未定义引用)。如果我在mat.cpp中定义了main函数,问题就消失了,但是我想拥有不同的文件。

为清楚起见,以下是错误消息:

main.o: En la función `main':
main.cpp:(.text+0x42): referencia a `Mat<double, 1ul, 2ul>::Mat(std::array<double, 2ul> const&)' sin definir
main.cpp:(.text+0x6e): referencia a `Mat<double, 1ul, 2ul>::Mat(std::array<double, 2ul> const&)' sin definir
main.cpp:(.text+0x9a): referencia a `Mat<double, 2ul, 1ul>::Mat(std::array<double, 2ul> const&)' sin definir
main.cpp:(.text+0xc6): referencia a `Mat<double, 2ul, 1ul>::Mat(std::array<double, 2ul> const&)' sin definir
main.cpp:(.text+0x10c): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::Mat(std::array<Mat<double, 1ul, 2ul>, 2ul> const&)' sin definir
main.cpp:(.text+0x152): referencia a `Mat<Mat<double, 2ul, 1ul>, 2ul, 1ul>::Mat(std::array<Mat<double, 2ul, 1ul>, 2ul> const&)' sin definir
main.cpp:(.text+0x190): referencia a `Mat<int, 2ul, 2ul>::Mat(std::array<int, 4ul> const&)' sin definir
main.cpp:(.text+0x1c6): referencia a `Mat<int, 2ul, 2ul>::Mat(std::array<int, 4ul> const&)' sin definir
main.cpp:(.text+0x1fc): referencia a `Mat<int, 2ul, 2ul>::Mat(std::array<int, 4ul> const&)' sin definir
main.cpp:(.text+0x23e): referencia a `Mat<int, 2ul, 2ul>::Mat(std::array<int, 4ul> const&)' sin definir
main.cpp:(.text+0x254): referencia a `Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>::Mat(std::array<Mat<int, 2ul, 2ul>, 4ul> const&)' sin definir
main.cpp:(.text+0x2e7): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o: En la función `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul> mult<double, int, 1ul, 2ul, 1ul, 2ul, 2ul, 2ul>(Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>, Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>)':
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0x3a): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::Mat()' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0x8d): referencia a `Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0xb9): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0xfc): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE[_Z4multIdiLm1ELm2ELm1ELm2ELm2ELm2EE3MatIS0_IT_XT1_EXT6_EEXT3_EXT5_EES0_IS0_IS1_XT1_EXT2_EEXT3_EXT4_EES0_IS0_IT0_XT2_EXT6_EEXT4_EXT5_EE]+0x10e): referencia a `Mat<double, 1ul, 2ul>& Mat<double, 1ul, 2ul>::operator+=<double>(Mat<double, 1ul, 2ul>)' sin definir
main.o: En la función `Mat<double, 1ul, 2ul> mult<double, int, 1ul, 2ul, 2ul>(Mat<double, 1ul, 2ul>, Mat<int, 2ul, 2ul>)':
main.cpp:(.text._Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE[_Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE]+0x2b): referencia a `Mat<double, 1ul, 2ul>::Mat()' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE[_Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE]+0x77): referencia a `Mat<double, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE[_Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE]+0x97): referencia a `Mat<int, 2ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.cpp:(.text._Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE[_Z4multIdiLm1ELm2ELm2EE3MatIT_XT1_EXT3_EES0_IS1_XT1_EXT2_EES0_IT0_XT2_EXT3_EE]+0xbe): referencia a `Mat<double, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIS_IiLm2ELm2EELm2ELm2EE[_ZTV3MatIS_IiLm2ELm2EELm2ELm2EE]+0x10): referencia a `Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIS_IiLm2ELm2EELm2ELm2EE[_ZTV3MatIS_IiLm2ELm2EELm2ELm2EE]+0x18): referencia a `Mat<Mat<int, 2ul, 2ul>, 2ul, 2ul>::set(Mat<int, 2ul, 2ul> const&, unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIiLm2ELm2EE[_ZTV3MatIiLm2ELm2EE]+0x10): referencia a `Mat<int, 2ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIiLm2ELm2EE[_ZTV3MatIiLm2ELm2EE]+0x18): referencia a `Mat<int, 2ul, 2ul>::set(int const&, unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIS_IdLm1ELm2EELm1ELm2EE[_ZTV3MatIS_IdLm1ELm2EELm1ELm2EE]+0x10): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIS_IdLm1ELm2EELm1ELm2EE[_ZTV3MatIS_IdLm1ELm2EELm1ELm2EE]+0x18): referencia a `Mat<Mat<double, 1ul, 2ul>, 1ul, 2ul>::set(Mat<double, 1ul, 2ul> const&, unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIdLm2ELm1EE[_ZTV3MatIdLm2ELm1EE]+0x10): referencia a `Mat<double, 2ul, 1ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIdLm2ELm1EE[_ZTV3MatIdLm2ELm1EE]+0x18): referencia a `Mat<double, 2ul, 1ul>::set(double const&, unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIdLm1ELm2EE[_ZTV3MatIdLm1ELm2EE]+0x10): referencia a `Mat<double, 1ul, 2ul>::get(unsigned long, unsigned long)' sin definir
main.o:(.data.rel.ro._ZTV3MatIdLm1ELm2EE[_ZTV3MatIdLm1ELm2EE]+0x18): referencia a `Mat<double, 1ul, 2ul>::set(double const&, unsigned long, unsigned long)' sin definir
collect2: error: ld returned 1 exit status

0 个答案:

没有答案