具有未定义引用和重新定位的简单程序被截断以适应错误

Simple program with undefined reference & relocation truncated to fit error

本文关键字:程序 简单 错误 引用 未定义 新定位 定位      更新时间:2023-10-16

好吧,我正在youtube上学习c++教程,因为我是一个相对较新的程序员。我正在努力学习如何在创建类时使用头文件等。在本教程中,我们将学习如何使用箭头成员选择操作符。

https://www.youtube.com/watch?v=2RP4f9beidc&list=PLAE85DE8440AA6B83&索引=42

我没有使用代码块,这是他正在使用的IDE。我正在gedit上编写代码,并使用Cygwin编译和运行它。

如有任何帮助,我们将不胜感激!

这是一个简单的代码:

主要.cpp

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

int main(){

     Sally sallyObject;
     Sally *sallyPointer = &sallyObject;

     sallyObject.printCrap();
     sallyPointer->printCrap();
}

Sally.cpp

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

Sally::Sally()
{
}

void Sally::printCrap(){
    cout << "did someone say steak?" << endl;
}

莎莉

#ifndef SALLY_H
#define SALLY_H
#include <iostream>

class Sally
{
    public:
        Sally();
        void printCrap();
    protected:
    private:
};

#endif // SALLY_H

错误代码:

/tmp/ccfOEJJF.o:Main.cpp:(.text+0x15):对Sally::Sally()' /tmp/ccfOEJJF.o:Main.cpp:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol Sally::Sally()的未定义引用'/tmp/ccfOEJJF.o:Main.cpp:(.text+0x29):对Sally::printCrap()' /tmp/ccfOEJJF.o:Main.cpp:(.text+0x29): relocation truncated to fit: R_X86_64_PC32 against undefined symbol Sally::printCrap()的未定义引用'/tmp/ccfOEJJF.o:Main.cpp:(.text+0x35):对Sally::printCrap()' /tmp/ccfOEJJF.o:Main.cpp:(.text+0x35): relocation truncated to fit: R_X86_64_PC32 against undefined symbol Sally::printCrap()的未定义引用'collect2:错误:ld返回1退出状态

为此在cygwin中输入的命令必须是

g++ Sally.cpp Main.cpp

命令行中不包含头文件。作为一条建议,请查找如何使用makefiles

testsuite/Makefile中,我将prog的规则更改为:

prog.o: prog.c
    $(CC) -c -o $@ $<
prog.exe: prog.o
    $(CC) -o $@ $<

这推动了事情的发展——我不确定它是否正确——但它在protos.h中继续出现问题,因为已经有Elf_[ESP]hdr类型的系统头定义。。。对于中的所有原型、定义和参考,我不得不将它们更改为pelf_[ESP]hdr

protos.h
chrpath.c
elf.c
killrpath.c