Ubuntu GCC 编译C++ 文件出错。main.o: In function `__static_initialization_and_destruction_0':

时间:2022-02-06 13:22:44
gcc -g -c main.cc#gcc -lstdc++ -g -c main.cc#默认生成main.o
g++ -c -o printf1.o printf1.cc
g++ -c -o printf2.o printf2.cc
gcc -g main.o printf1.o printf2.o -o out #gcc -lstdc++ -g main.o printf1.o printf2.o -o out
main.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.4/iostream:72: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/4.4/iostream:72: undefined reference to `std::ios_base::Init::~Init()'
main.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
printf1.o: In function `printf1()':
printf1.cc:(.text+0x11): undefined reference to `std::cout'
printf1.cc:(.text+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*)'
printf1.cc:(.text+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> >&)'
printf1.cc:(.text+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> >&))'
printf1.o: In function `__static_initialization_and_destruction_0(int, int)':
printf1.cc:(.text+0x4e): undefined reference to `std::ios_base::Init::Init()'
printf1.cc:(.text+0x53): undefined reference to `std::ios_base::Init::~Init()'
printf1.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
printf2.o: In function `printf2()':
printf2.cc:(.text+0x11): undefined reference to `std::cout'
printf2.cc:(.text+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*)'
printf2.cc:(.text+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> >&)'
printf2.cc:(.text+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> >&))'
printf2.o: In function `__static_initialization_and_destruction_0(int, int)':
printf2.cc:(.text+0x4e): undefined reference to `std::ios_base::Init::Init()'
printf2.cc:(.text+0x53): undefined reference to `std::ios_base::Init::~Init()'
printf2.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [outcc] Error 1

出现这种错误。从网上查了资料。解决方法如下:

一、可以将gcc换成g++编译器编译。

二、如果坚持使用gcc的话,可以在gcc后面加上-lstdc++.

    代码:    gcc -lstdc++ 

     搞定!。