ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
在ubuntu vim下编辑如下代码(代码来自《C++标准程序库》p107):
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<string> coll;
/* read all words from the standard input
* - source: all strings until end-of-file (or erro)
* - destination: coll(inserting)
*/
copy (istream_iterator<string>(cin), // start of source
istream_iterator<string>(), // end of source
back_inserter(coll));
// sort elements
sort (coll.begin(), coll.end());
/* print all elements without duplicates
* - source: coll
* - destination: standard output (with newline between elements)
*/
unique_copy (coll.begin(), coll.end(), // source
ostream_iterator<string>(cout, "\n")); // destination
return 0;
}
使用GCC编译后出现下列可怕的问题:
hello.o: In function `main':
hello.cpp:(.text+0x41): undefined reference to `std::cin'
hello.cpp:(.text+0xd1): undefined reference to `std::cout'
hello.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x19e): undefined reference to `std::ios_base::Init::Init()'
hello.cpp:(.text+0x1a3): undefined reference to `std::ios_base::Init::~Init()'
hello.o: In function `std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>::~istream_iterator()':
hello.cpp:(.text._ZNSt16istream_iteratorISscSt11char_traitsIcEiED2Ev[_ZNSt16istream_iteratorISscSt11char_traitsIcEiED5Ev]+0x10): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
hello.o: In function `std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>::istream_iterator(std::basic_istream<char, std::char_traits<char> >&)':
hello.cpp:(.text._ZNSt16istream_iteratorISscSt11char_traitsIcEiEC2ERSi[_ZNSt16istream_iteratorISscSt11char_traitsIcEiEC5ERSi]+0x24): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
hello.cpp:(.text._ZNSt16istream_iteratorISscSt11char_traitsIcEiEC2ERSi[_ZNSt16istream_iteratorISscSt11char_traitsIcEiEC5ERSi]+0x41): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
hello.o: In function `std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>::istream_iterator()':
hello.cpp:(.text._ZNSt16istream_iteratorISscSt11char_traitsIcEiEC2Ev[_ZNSt16istream_iteratorISscSt11char_traitsIcEiEC5Ev]+0x24): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
hello.o: In function `std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>::istream_iterator(std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int> const&)':
hello.cpp:(.text._ZNSt16istream_iteratorISscSt11char_traitsIcEiEC2ERKS2_[_ZNSt16istream_iteratorISscSt11char_traitsIcEiEC5ERKS2_]+0x2f): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
hello.o: In function `std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>::_M_read()':
hello.cpp:(.text._ZNSt16istream_iteratorISscSt11char_traitsIcEiE7_M_readEv[std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>::_M_read()]+0x29): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator void*() const'
hello.cpp:(.text._ZNSt16istream_iteratorISscSt11char_traitsIcEiE7_M_readEv[std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>::_M_read()]+0x61): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
hello.cpp:(.text._ZNSt16istream_iteratorISscSt11char_traitsIcEiE7_M_readEv[std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>::_M_read()]+0x7f): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator void*() const'
hello.o: In function `void std::__insertion_sort<__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >(__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>,
……
但是使用Code::Blocks编译、链接可以通过,不解,上网搜了一下。
网上的解释是:“ you need to add -lstdc++, or use 'g++' rather than 'gcc' as your driver program.”,也就是说如果想要使用g++编译程序的话,那么链接时需要添加“-Istdc++”。
我试了一下 , 在链接时加上"-Istdc++",就没有问题了。
关于g++,让我想起了,在ubuntu下一开始安装完code::blocks的时候,提示需要安装g++。原来code::blocks内置编译器使用的是g++,但是为什么在建工程的时候,提示使用的编译器是gcc呢??
这就引来了一个问题:“gcc和g++的区别和联系是什么?”网上搜了一下“gcc和g++”(http://www.yuanma.org/data/2007/0406/article_2498.htm),整理下来有这么几条是有用的:
(1)gcc和g++都是GNU(组织)的一个编译器。
(2)后缀名为.c的程序和.cpp的程序g++都会当成是c++的源程序来处理。而gcc不然,gcc会把.c的程序处理成c程序。
(3)对于.cpp的程序,编译可以用gcc/g++,而链接可以用g++或者gcc -lstdc++。(个人觉得这条是最重要的)
<本文完>