博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题
阅读量:5812 次
发布时间:2019-06-18

本文共 6335 字,大约阅读时间需要 21 分钟。

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

<本文完>

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

你可能感兴趣的文章
(转) 多模态机器翻译
查看>>
【官方文档】Nginx负载均衡学习笔记(三) TCP和UDP负载平衡官方参考文档
查看>>
矩阵常用归一化
查看>>
Oracle常用函数总结
查看>>
【聚能聊有奖话题】Boring隧道掘进机完成首段挖掘,离未来交通还有多远?
查看>>
USNews大学排名遭美国计算机研究学会怒怼,指排名荒谬要求撤回
查看>>
七大关键数据 移动安全迎来历史转折点
查看>>
在AngularJS中学习javascript的new function意义及this作用域的生成过程
查看>>
盘点物联网网关现有联网技术及应用场景
查看>>
1、下载安装scala编译器(可以理解为scala的jdk),地址:http://www.scala
查看>>
mui 总结2--新建第一个app项目
查看>>
nginx的lua api
查看>>
考研太苦逼没坚持下来!看苑老师视频有点上头
查看>>
HCNA——RIP的路由汇总
查看>>
zabbix监控php状态(四)
查看>>
定时任务的创建
查看>>
实战Django:小型CMS Part2
查看>>
原创]windows server 2012 AD架构试验系列 – 16更改DC计算机名
查看>>
统治世界的十大算法
查看>>
linux svn安装和配置
查看>>