【问题标题】:undefined reference to std::ios_base::Init::Init()对 std::ios_base::Init::Init() 的未定义引用
【发布时间】:2013-11-15 20:44:32
【问题描述】:

我正在使用 C++ 学习 OOP,但遇到了问题。我确定这是一个内存分配问题,但我似乎无法理解它。任何帮助将不胜感激。

我的客户代码

    #include <iostream>
    #include "Box.cpp"

    using namespace std;

    int main(){
        Box *box = new Box;
        return 0;
    }

我的盒子类...

    #include <iostream>

    using namespace std;

    class Box{

        private:
            double width;
            double height;
            double perimeter;
            double area;


        public:
            Box(){
                cout << "Box created" << endl;
            }

            ~Box(){
                cout << "Box Destroyed" << endl;
            }

            double getWidth(){
                //
                return this->width;
            }

            double getHeight(){
                //
                return this->height;
            }

            double getArea(){
                //
                return this->area;
            }

            double getPerimeter(){
                //
                return this->perimeter;
            }

            void setWidth(double w){
                //
                this->width = w;
                if(!this->height){
                    computeSetArea(this->width, this->height);
                    computeSetPerimeter(this->width, this->height);
                }
            }

            void setHeight(double h){
                //
                this->height = h;
                if(!this->width){
                    computeSetArea(this->width, this->height);
                    computeSetPerimeter(this->width, this->height);
                }
            }

        private:
            void computeSetArea(double w, double h){
                //
                this->area = w*h;
            }

            void computeSetPerimeter(double w, double h){
                //
                this->perimeter = (w * 2) + (h + 2);
            }
    };

我使用 gcc 并执行:

    gcc Box.cpp client.cpp -o mainfile

之后我收到此错误。

/tmp/ccaVb21k.o: In function `__static_initialization_and_destruction_0(int, int)':
Box.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
Box.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccaVb21k.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
/tmp/ccjtbzi4.o: In function `main':
client.cpp:(.text+0x14): undefined reference to `operator new(unsigned int)'
client.cpp:(.text+0x3f): undefined reference to `operator delete(void*)'
/tmp/ccjtbzi4.o: In function `__static_initialization_and_destruction_0(int, int)':
client.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::Init()'
client.cpp:(.text+0x71): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccjtbzi4.o: In function `Box::Box()':
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x11): undefined reference to `std::cout'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x16): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccjtbzi4.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
/tmp/ccjtbzi4.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

【问题讨论】:

  • 按照惯例 .h 文件包含类定义,而 .cpp 文件包含类实现,因此您应该:将定义拆分为 .h 文件,并将实现保留在 cpp ,或将您的 .cpp 重命名为 .h (因为它实际上是一个内联实现的 .h )。如果您确实将文件重命名为 .h,请删除 using 命名空间 std;来自 .h 文件,因为污染头文件中的全局命名空间是不好的做法。

标签: c++ linker-errors


【解决方案1】:

您的代码编译正常,但出现链接器错误(ld 是链接器,它返回 1(错误)),抱怨缺少 c++ 库。

要修复,您需要将 stdc++ 库添加到命令行,或使用 g++。

gcc 替换为g++ 或将-lstdc++ 添加到您的gcc 命令行。

gcc Box.cpp client.cpp -o mainfile -lstdc++

g++ Box.cpp client.cpp -o mainfile

这会将 std c++ 库与您的编译代码链接起来。使用g++,可以省略这一步。

【讨论】:

    【解决方案2】:

    像这样设置你的类结构对我有用:

    Box.h:

    class Box
    {
    public:
        Box();
        ~Box();
    
        double getWidth();
        double getHeight();
        double getArea();
        double getPerimeter();
        void setWidth(double w);
        void setHeight(double h);
        void computeSetArea(double w, double h);
        void computeSetPerimeter(double w, double h);
    
    private:
        double width;
        double height;
        double perimeter;
        double area;
    };
    

    然后是 Box.cpp:

    #include "box.h"
    #include <iostream>
    using namespace std;
    
    Box::Box(){
        cout << "Box created" << endl;
    }
    
    Box::~Box(){
        cout << "Box Destroyed" << endl;
    }
    
    double Box::getWidth(){
        return this->width;
    }
    
    double Box::getHeight(){
        return this->height;
    }
    
    double Box::getArea(){
        return this->area;
    }
    
    double Box::getPerimeter(){
        return this->perimeter;
    }
    
    void Box::setWidth(double w){
        this->width = w;
        if(!this->height){
            computeSetArea(this->width, this->height);
            computeSetPerimeter(this->width, this->height);
        }
    }
    
    void Box::setHeight(double h){
        this->height = h;
        if(!this->width){
            computeSetArea(this->width, this->height);
            computeSetPerimeter(this->width, this->height);
        }
    }
    
    void Box::computeSetArea(double w, double h){
        this->area = w*h;
    }
    
    void Box::computeSetPerimeter(double w, double h) {
        this->perimeter = (w * 2) + (h + 2);
    }
    

    输出:

    Box created
    

    【讨论】:

    • 我应该用头文件源文件编译还是只用头文件编译?
    • 只是源文件,从不直接编译头文件。也永远不要在另一个源文件中包含一个源文件(如上面的代码)。
    • @seanr,嗨,肖恩,是的,就像约翰说的那样。我用过:“g++ Box.cpp main.cpp -o mainfile”然后运行当然我用过:“./mainfile”这对你有用吗?
    • @john,您好 John,Box.h 和 Box.cpp 是单独的文件,希望能澄清我列出的内容。谢谢。
    • 非常感谢您尝试更多地研究为什么要使用标头以及为什么不将 cpp 文件直接包含在源文件中。
    【解决方案3】:

    尝试使用 g++ 代替 gcc。

    【讨论】:

      猜你喜欢
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 2015-05-11
      • 1970-01-01
      • 2021-06-09
      • 2015-02-09
      相关资源
      最近更新 更多