LNK2019内联函数的未解决错误

时间:2011-12-23 02:40:35

标签: visual-c++ linker compiler-errors unresolved-external

我不知道为什么会这样。我得到的错误如下:

Error   2   error LNK2019: unresolved external symbol "public: int __thiscall Graphics::GLMatrix::getColumnSize(void)" (?getColumnSize@GLMatrix@Graphics@@QAEHXZ) referenced in function _SDL_main  C:\Users\holland\Documents\code\c++\projects\OpenGL_01\OpenGL_01\main.obj
Error   3   error LNK2019: unresolved external symbol "public: int __thiscall Graphics::GLMatrix::getRowSize(void)" (?getRowSize@GLMatrix@Graphics@@QAEHXZ) referenced in function _SDL_main    C:\Users\holland\Documents\code\c++\projects\OpenGL_01\OpenGL_01\main.obj
Error   4   error LNK1120: 2 unresolved externals   C:\Users\holland\Documents\code\c++\projects\OpenGL_01\Debug\OpenGL_01.exe  

未链接的是我的Matrix类中的getRowSize()getColumnSize()函数。我做错了什么?

那么,我在这里做错了什么?我一直在寻找...搜索每一个方向。

守则

标题文件:

#ifndef GLMATRIX_H
#define GLMATRIX_H

#pragma once

#include <array>


namespace Graphics {

    class GLMatrix
    {
    public:
        GLMatrix(int sizeX, int sizeY);
        ~GLMatrix();
        void allocMatrix();
        void addColumnI(int row, int column, long item);
        void revertRowsByColumns(); /*changes the formula of r * c to c * r*/
        GLMatrix &operator *(float scalar); /*multiply by scalar*/
        inline int getRowSize();
        inline int getColumnSize();
    private:
        int _sizeX, _sizeY;
        long** _pArray;
    };

}

#endif //GLMATRIX_H

来源:

#include "GLMatrix.h"

namespace Graphics {

    GLMatrix::GLMatrix(int sizeX, int sizeY)
    {
        _sizeX = sizeX; 
        _sizeY = sizeY;
    }

    GLMatrix::~GLMatrix()
    {
        delete _pArray;
    }

    void GLMatrix::addColumnI(int row, int column, long item) {

    }

    inline int GLMatrix::getRowSize() {
        return _sizeX;
    }

    inline int GLMatrix::getColumnSize() {
        return _sizeY;
    }

    void GLMatrix::allocMatrix() {

        _pArray = new long*[_sizeX];

        for (int i = 0; i < _sizeX; ++i) {          
            _pArray[i] = new long[_sizeY];
        }

    }

    void GLMatrix::revertRowsByColumns() {

        long** columns = new long*[_sizeY];

        for (int col = 0; col < _sizeY; ++col) {
            columns[col] = new long[_sizeX];
            memmove(
                columns + col, 
                _pArray[_sizeX - col], 
                sizeof(_sizeX) - sizeof(col)
            );
        }   
    }

}

主:

#include <SDL.h>
#include "GLMatrix.h"


int main(int argc, char* argv[]) {

    //SDL_Init(SDL_INIT_EVERYTHING);

    //matrix test

    Graphics::GLMatrix* matrix = new Graphics::GLMatrix(3, 3);

    int num_rows = matrix->getRowSize();
    int num_columns = matrix->getColumnSize();
    for (int row = 0; row < num_rows; ++row) {

    }

    //SDL_Quit();

    delete matrix;

    return 0;
}

4 个答案:

答案 0 :(得分:1)

必须在头文件中定义内联函数。 (更具体地说,该定义必须在使用它的每个翻译单元中都可见。)

答案 1 :(得分:1)

必须在头文件中声明内联函数的常识不再适用。几年以来,大多数编译器都实现了一个名为链接时间优化(gcc)或链接时间代码生成(VC)的功能,它保存了有关内联函数(以及其他)的重要信息,允许链接器将所有目标文件视为“一个大的快乐”翻译单位“。因此,链接器可以内联您放在cpp文件中的函数。

相关链接: http://msdn.microsoft.com/en-us/library/xbf3tbeh.aspx http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html(搜索-flto)

编辑:我之前写的内容显然不明白。此功能的目的不是为了避免懒惰的程序员在每个翻译单元中声明内联函数。但它确实会给你这种可能导致问题的副产品。 在头文件中声明内联函数。

答案 2 :(得分:1)

1。翻译单元是单个源文件(在您的情况下为“Source”和“Main”)以及包含的文件。

2。内联函数

  

C ++ 11,§3.2/ 3

     

“[...]内联函数应在每个使用它的翻译单元中定义。[...]”

     

§7.1.2/ 4

     

内联函数应在每个使用过的翻译单元中定义,并且在每种情况下都应具有完全相同的定义(3.2)。 [...]

您的“来源”翻译单元的定义为getRowSizegetColumnSize。你的“主要”翻译单位没有!这是未定义的行为,因为编译器不需要检查它。

LTCG / LTO

弗拉基米尔的部分答案是正确的。有链接时间优化(或链接时间代码生成)。但它不能帮助懒惰的程序员保存内联函数的函数定义。

完成LTCG / LTO以使编译器立即看到所有函数,这反过来可能决定在正常情况下内联函数。声明为inline的函数必须按照定义在编译器可用的每个位置都可见。因此,不需要LTCG / LTO(并且不应该为了解决丢失的内联链路错误而滥用LTCG / LTO)。

<小时/> 只是为了说清楚:我很清楚这是一个老问题,但它还没有正确回答,我只是摔倒了。

答案 3 :(得分:1)

我也遇到了 Visual Studio 2019 库中内联函数的链接错误问题。我在 C/C++ 属性的选项卡语言中将属性“删除未引用的代码和数据”设置为“否”并解决链接错误。