首页 > 解决方案 > GPP 在链接阶段失败:“未定义对 [function] 的引用”(没有生成文件或模板)

问题描述

g++ 在链接阶段无法编译以下文件并出现多个undefined reference to [function]错误。

我正在使用教科书学习 C++11。关于这个主题的所有其他问题和答案都涉及我在这里没有使用的 makefile 或模板。

g++ 版本为 7.3.0(Ubuntu 7.3.0-27ubuntu1~18.04)。

主.cpp:

#include "Screen.h"

// #include <string>
using std::string;

// #include <iostream>
using std::cout;
using std::endl;

int main()
{
    Screen myScreen(5, 5, 'X');
    myScreen.move(4,0).set('#').display(cout);
    cout << endl;
    myScreen.display(cout);
    cout << endl;

    return 0;
}

屏幕.h:

#ifndef SCREEN_H
#define SCREEN_H

#include <string>
#include <iostream>

class Screen
{
public:
    // typedefs
    using pos = std::string::size_type;

    // constructors
    Screen()
        = default;
    Screen(pos ht, pos wd):
        height  (ht),
        width   (wd),
        contents(ht * wd, ' ')
    {}
    Screen(pos ht, pos wd, char c):
        height  (ht),
        width   (wd),
        contents(ht * wd, c)
    {}

    // public member functions
              char get    () const;
    inline    char get    (pos, pos) const;
           Screen& move   (pos, pos);
           Screen& set    (char);
           Screen& set    (pos, pos, char);
           Screen& display(std::ostream&);
     const Screen& display(std::ostream&) const;

private:
    // class variables
            pos cursor   = 0,
                height   = 0,
                width    = 0;
    std::string contents    ;

    // private member functions
    void do_display(std::ostream&) const;
};
#endif

屏幕.cpp:

#include "Screen.h"

// #include <string>
using std::string;

// #include <iostream>

/*
 ####  #   # ####  #     ###  ###
 #   # #   # #   # #      #  #   #
 ####  #   # ####  #      #  #
 #     #   # #   # #      #  #   #
 #      ###  ####  ##### ###  ###

 #    # ##### #    # ####  ##### ####   ####
 ##  ## #     ##  ## #   # #     #   # #
 # ## # ####  # ## # ####  ####  ####   ###
 #    # #     #    # #   # #     #  #      #
 #    # ##### #    # ####  ##### #   # ####
*/

char Screen::get() const
    {return contents[cursor];}

/*inline*/ char Screen::get (pos r, pos c) const
{
    pos row = r * width;
    return contents[row + c];
};

inline Screen& Screen::move(pos r, pos c)
{
    pos row = r * width;
    cursor = row + c;
    return *this;
}

inline Screen& Screen::set(char c)
{
    contents[cursor] = c;
    return *this;
}

inline Screen& Screen::set(pos row, pos col, char ch)
{
    contents[(row * width) + col] = ch;
    return *this;
}

Screen& Screen::display(std::ostream& os)
{
    do_display(os);
    return *this;
}

const Screen& Screen::display(std::ostream& os) const
{
    do_display(os);
    return *this;
}

/*
 ####  ####  ### #   #  ###  #####  #####
 #   # #   #  #  #   # #   #   #    #
 ####  ####   #  #   # #####   #    ####
 #     #  #   #   # #  #   #   #    #
 #     #   # ###   #   #   #   #    #####

 #    # ##### #    # ####  ##### ####   ####
 ##  ## #     ##  ## #   # #     #   # #
 # ## # ####  # ## # ####  ####  ####   ###
 #    # #     #    # #   # #     #  #      #
 #    # ##### #    # ####  ##### #   # ####
*/

inline void Screen::do_display(std::ostream& os) const
    {os << contents;}

我遵循了书中的所有内容,所以我希望 g++ 编译文件时没有错误。但是我在控制台中遇到了这些错误:

$ g++ -o program main.cpp Screen.cpp -std=c++11
/tmp/ccBELGiY.o: In function `main':
main.cpp:(.text+0x45): undefined reference to `Screen::move(unsigned long, unsigned long)'
main.cpp:(.text+0x52): undefined reference to `Screen::set(char)'
collect2: error: ld returned 1 exit status

标签: c++compiler-errorsg++

解决方案


正如Mike Kinghan 指出的那样,这已经在这里得到了回答:https ://stackoverflow.com/a/54296817/1362568

基本上,问题是我inline在我的 Screen.cpp 文件而不是我的 .h 文件中实现了我的功能。由于inlines 是“内联”扩展的并且不是“真实”函数,因此链接器找不到“真实”函数来定义我在 Screen.h 中声明的函数。

这是我的错误,因为我的导师告诉我总是把定义放在一个单独的 .cpp 文件中,所以没有按照我的教科书来做。

我通过在我的 .h 文件中定义我的内联来解决这个问题。


推荐阅读