未定义对std :: ios_base :: Init :: Init()的引用 [英] undefined reference to std::ios_base::Init::Init()
问题描述
我正在使用C ++学习OOP并遇到问题.我确定这是一个内存分配问题,但似乎无法解决.任何帮助将不胜感激.
I'm working on learning OOP with C++ and having a problem. I'm sure its a memory allocation problem but cant seem to get my head around it. Any help will be greatly appreciated.
我的客户代码
#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
推荐答案
您的代码可以正常编译,出现链接器错误( ld
是链接器,并且返回1(错误)),它抱怨缺少c ++库.
Your code compiles fine, you're getting a linker error (ld
is the linker, and it's returning 1 (error)), which is complaining about missing c++ libs.
要解决此问题,您需要将stdc ++ lib添加到命令行中,或使用g ++.
To fix, you'll need to add the stdc++ lib to your commandline, or use g++.
用 g ++
替换 gcc
或在您的gcc命令行中添加 -lstdc ++
.
Replace gcc
with g++
or add -lstdc++
to your gcc command line.
gcc Box.cpp client.cpp -o mainfile -lstdc ++
或
g ++ Box.cpp client.cpp -o mainfile
这会将std c ++库与您的已编译代码链接.使用 g ++
,您可以省略此步骤.
This will link the std c++ library with your compiled code. Using g++
, you can omit this step.
这篇关于未定义对std :: ios_base :: Init :: Init()的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!