ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
2011-07-11 11:20
786 查看
在ubuntu vim下编辑如下代码(代码来自《C++标准程序库》p107):
原文地址:http://archive.cnblogs.com/a/2054598/
#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++。(个人觉得这条是最重要的)
原文地址:http://archive.cnblogs.com/a/2054598/
相关文章推荐
- ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
- ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题——gcc编译C++代码
- ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
- ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
- ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
- GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
- ffmpeg libx264编译出现error: undefined reference to 'x264_picture_init'等问题的解决方法
- 用gcc编译.cpp文件可能出现"undefined reference to `__gxx_personality_v0'"问题的解决
- 用gcc编译.cpp文件可能出现"undefined reference to `__gxx_personality_v0'"问题的解决
- 解决gcc 编译时报错"undefined reference to 'main'" 问题
- gcc编译cpp文件 出现undefined reference to `__gxx_personality_v0'
- Ubuntu11.20 32位linux下编译cppunit报 libcppunit.so: undefined reference to `dlsym'错误问题
- ubuntu下eclipse编译c项目出现undefined reference to `sin'
- gcc undefined reference to '__stack_chk_fail'【操作系统开发编译中遇到的问题】
- gcc编译线程程序需带-lpthread选项(否则出错:undefined reference to `pthread_create')
- linux 下安装opencv3.0在编译时出现的问题undefined reference to `parallel_pthreads_set_threads_num(int)'
- 编译程序出现 undefined reference to `__gxx_personality_v0' 错误
- UNIX环境高级编程中的11章程序11-2编译问题---undefined reference to `pthread_create'
- linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
- TORNADO下编译出现undefined reference to `itoa'错误