c++ - G++ - 未定义对已定义成员函数的引用

标签 c++ makefile linker g++ linker-errors

我目前正在开发一个处于早期阶段的虚拟运行时环境程序,由于使用我的 makefile 时出现链接器错误,我无法继续我的工作,如下所示。我收到的错误是:

g++ controller.o processor.o test.o -o final
controller.o: In function `Controller::run()':
controller.cpp:(.text+0x1e0): undefined reference to
Processor::codeParams(char)'
controller.o: In function `Controller::fetch()':
controller.cpp:(.text+0x290): undefined reference to `Controller::pc'
controller.cpp:(.text+0x299): undefined reference to `Controller::pc'
collect2: error: ld returned 1 exit status
makefile:16: recipe for target 'final' failed
make: *** [final] Error 1

我不确定为什么会出现此错误,因为我认为我已经在与 header 对应的源文件中定义了这些内容。下面将给出所有文件,以便编译程序。

测试.cpp:

#include <iostream>
#include <vector>
#include "includes/controller.h"

using namespace std;

int main()
{
    vector<char> prog = {0x0};

    Controller contr(prog);
    cout << "Error Code: " << contr.run() << endl;

    return 0;
}

Controller .cpp:

/*
    Author(s):  James Dolan
    File:       controller.cpp
    Build:      0.0.0
    Header:     includes/controller.h
    DoLR:       21:39 11/1/2017

    Todo: n/a
*/



#include "includes/controller.h"


Controller::Controller(vector<char> prog)
{
    printf("Program:");                         //Display program
    for(auto i : program)
    {
        printf("%02X", i);
    }
    printf("\n");

    Controller::program = program;
}

Controller::~Controller ()
{
}

int Controller::run()
{
    bool runFlag = true;
    int errorCode = 0;
    char curCode;
    vector<char> curInstr;
    int paramRef;

    while(runFlag)
    {
        curCode = fetch();
        printf("curCode:%02X\n", curCode);
        curInstr.push_back(curCode);
        paramRef = proc.codeParams(curCode);

        if (paramRef == 0xffff){runFlag = false; continue;}     //Check if shutdown signal was returned, if so shutdown
        printf("opcode good\n");

        for(int i; i<paramRef; i++){curInstr.push_back(fetch());}
    }


    return errorCode;
}

char Controller::fetch()
{
    return program[pc++];                       //Return next instruction then increment the program counter
}

Controller .h:

/*
    Author(s):  James Dolan
    File:       controller.h
    Source:     ../controller.cpp
    DoLR:       21:39 11/1/2017

    Todo: n/a
*/


#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <iostream>
#include <vector>
#include <cstdlib>

#include "processor.h"

using namespace std;

class Controller{

    public:
        Controller(vector<char> prog);
        ~Controller();
        int run();

    protected:

    private:
        vector<char> program;
        static int pc;
        char fetch();
        Processor proc();


};

#endif

处理器.cpp:

#include "includes/processor.h"

Processor::Processor()
{
}

Processor::~Processor()
{
}

int codeParams(char code)
{

    switch(code)
    {
        case 0x0:                   //Halt
            return 0;
        default:
            printf("[ERROR!] Invalid opcode [%02X]", code);
            return 0xffff;          //Return shutdown signal
    }
}

处理器.h:

#ifndef PROCESSOR_H
#define PROCESSOR_H

#include <iostream>
#include <cstdlib>

class Processor{

    public:
        Processor();
        ~Processor();
        int codeParams(char code);

    protected:

    private:

};

#endif

如果有任何帮助,我将不胜感激,因为它将帮助我继续我的热情,开发一个完全成熟的开源虚拟运行时环境,如 java vm,感谢您抽出时间。

最佳答案

在 Controller.cpp 中你需要一个 int Controller::pc;int Controller::pc = 0;

在头文件中,您声明了一个存在于某处的名为 pc 的静态整数。它需要实际存在于某处的翻译单元中(在本例中为 Controller.cpp),以便当链接器试图找到它时......它存在。

在 Processor.cpp 中,您的签名应该类似于 int Processor::codeParams(char code) 让编译器知道这是 Processor 的 codeParams 而不是名为 codeParams 的随机函数恰好也采用性格。

关于c++ - G++ - 未定义对已定义成员函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618972/

相关文章:

c++ - 内存段中存储的全局变量和静态变量在哪里?

c++ - opengl中渲染对象的移动

c - 界面像c语言吗?

make 中的 Shell 状态代码

c - 使用 GCC 链接对象的两种方法?

delphi - 链接 sqlite3.object 发出不满意的前向声明错误

c++ - 如何在 Visual Studio 中启用 CUDA 项目的单独编译

c++ - 继承比较运算符而不能相互比较派生类

c++ - 使用 GCC 编译 WineMaker 生成的 MakeFile

c++ - 简单派生类上的 Linux GCC 链接错误