ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题

转载 2016年06月02日 07:58:05

转载:http://www.cnblogs.com/chinazhangjie/archive/2011/05/23/2054598.html

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++。(个人觉得这条是最重要的)

相关文章推荐

Dev C++中的错误的解决[Linker error] undefined reference to `__dyn_tls_init_callback'

Dev C++中的错误的解决[Linker error] undefined reference to `__cpu_feat...       解决NOIP环境GUIDE和Dev之间的并存...

CFree “undefined reference to `__dyn_tls_init_callback'” 的解决方法

CFree undefined reference to `__dyn_tls_init_callback'以及undefined reference to `_setargv'的解决方案

gcc "undefined reference to" 问题解决方法

最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func'   ...

【嵌入式Linux】[内核编译]:内核编译时出现的问题:undefined reference to `__gnu_mcount_nc'

编译内核时出现了以下错误信息: To see full details build your kernel with: 'make CONFIG_DEBUG_SECTION_MISMATCH=y'  ...
  • z21109
  • z21109
  • 2014-07-29 23:51
  • 1442

linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'

解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件中引入了#include "cocos-ext.h",在进行C++编译的时候会遇到如下错误: undefi...

linux 链接库文件出现“undefined reference to" 问题的解决方法

最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误:(.text+0x13): undefined reference to `func'     关于u...

编译U-boot 出现 undefined reference to `dm9000_initialize' 的问题

下载的  tekkamanninja-U-boot-2009.11_tekkaman 源代码编译时出现 undefined reference to `dm9000_initialize' 等一系列错...

linux 链接库文件出现“undefined reference to" 问题的解决方法

在linux、ubantu系统下使用QT CREATOR 编程时遇到动态链接库调用出现“undefined reference to”的错误和问题,困扰多日,终于解决,将解决问题的方法转载在此,供更多...

解决编译u-boot-1.1.6 出现undefined reference to `raise'问题后,串口不输出问题

解决编译u-boot-1.1.6 出现undefined reference to `raise'问题后,串口不输出问题
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)