LNK2019 内联函数的未解决错误

发布于 2024-12-22 09:09:08 字数 2989 浏览 2 评论 0原文

我不知道为什么会发生这种情况。我得到的错误如下:

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;
}

I have no idea why this is happening. The error I get is the following:

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  

What isn't being linked is a getRowSize() and getColumnSize() function from my Matrix class. What am I doing wrong?

So, what am I doing wrong here? I've been searching...searching every which way.

The Code

Header file:

#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

Source:

#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)
            );
        }   
    }

}

Main:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黎歌 2024-12-29 09:09:08

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

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

编辑:从我之前写的内容中显然无法理解。此功能并不是为了让懒惰的程序员免于在每个翻译单元中声明内联函数而设计的。但它确实会给你带来可能导致问题的副产品。
请在头文件中声明内联函数。

The common knowledge that inline function MUST be declared in the header file is no longer true. Since several years, most compilers implement a feature called Link Time Optimization (gcc) or Link Time Code Generation (VC) that save important information about inline functions (among others) allowing the linker to look at all the object files as "one big happy translation unit". Thus the linker can inline functions you put in a cpp file.

Relevant links:
http://msdn.microsoft.com/en-us/library/xbf3tbeh.aspx
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html (search for -flto)

Edit: It was apparently not understood from what I wrote earlier. This feature was not designed to save lazy programmers from declaring inline functions in each translation unit. But it does give you this byproduct that can cause problems.
Do declare inline functions in the header file.

何处潇湘 2024-12-29 09:09:08

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

Inline functions must be defined in header files. (More specifically, the definition must be visible in every translation unit in which it is used.)

软甜啾 2024-12-29 09:09:08

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

2. 内联函数

C++11,§3.2/3

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

§7.1.2/4

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

您的“源”翻译单元具有 getRowSizegetColumnSize 的定义。您的“主”翻译单元还没有!这是未定义的行为,因为编译器不需要检查这一点。

LTCG/LTO

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

LTCG/LTO 的目的是使所有函数同时对编译器可见,而编译器又可能决定内联在正常情况下不可见的函数。声明为内联的函数必须在每个使用它们的地方都对编译器可见。因此,LTCG/LTO 不需要(也不应该被滥用)来解决丢失的内联链接错误。


Just to make things clear: I'm well aware of the fact that this is an old question but it isn't answerd correctly yet and I just fell over it.

1. A translation unit is a single source file ("Source" and "Main" in your case) together with the included files.

2. Inline functions

C++11, §3.2/3

"[...] An inline function shall be defined in every translation unit in which it is odr-used. [...]"

§ 7.1.2/4

An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [...]

Your "Source" translation unit has a definition of getRowSize and getColumnSize. Your "Main" translation unit has not! This is undefined behaviour since the compiler is not required to check for that.

LTCG/LTO

Parts of Vladimir's answer are correct. There is link time optimization (or link time code generation). But it doesn't serve the purpose of helping lazy programmers to save function-definitions for inline functions.

LTCG/LTO is done to make all functions visible to the compiler at once, which in turn may decide to inline function which aren't visible to it under normal circumstances. Functions declared inline must per definition visible to the compiler at every point they are used at. Therefore, LTCG/LTO is not needed for (and should not be abused for the purpose of) solving missing inline link errors.


Just to make things clear: I'm well aware of the fact that this is an old question but it isn't answerd correctly yet and I just fell over it.

美煞众生 2024-12-29 09:09:08

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

I was also facing with this linking error issue for the inline functions in the library for Visual Studio 2019. I set the property "Remove Unreferenced Code and data" to "No" in the tab Language of C/C++ properties and resolved the linking error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文