不能用gcc编译cpp

软件和网站开发以及相关技术探讨
回复
头像
mszjk
帖子: 94
注册时间: 2008-05-25 23:13

不能用gcc编译cpp

#1

帖子 mszjk » 2008-09-05 16:35

#include <iostream>
using namespace std ;

int main ()
{
cout << "haha" << endl ;
renturn 0 ;
}

用gcc编译的时候得到如下内容:/tmp/ccgd8lBO.o: In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
test.cpp:(.text+0xe): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
test.cpp:(.text+0x59): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
test.cpp:(.text+0x97): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
test.cpp:(.text+0xdf): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
/tmp/ccgd8lBO.o: In function `main':
test.cpp:(.text+0x128): undefined reference to `std::cout'
test.cpp:(.text+0x12d): 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*)'
test.cpp:(.text+0x135): 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> >&)'
test.cpp:(.text+0x13d): 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> >&))'
/tmp/ccgd8lBO.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x16d): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccgd8lBO.o: In function `__tcf_0':
test.cpp:(.text+0x1ba): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccgd8lBO.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld 返回 1

不知道是怎么回事。好像是iostream的问题。可是在/usr/include/c++/4.2.3中明明存在iostream,并且可以正常预编译。
编译环境已经配置好了。
不知道是不是emacs的配置问题?[/img]
qq群45420595,欢迎加入,共同努力。
“上”或“下“,那得看你怎么看了。
me
帖子: 972
注册时间: 2007-09-14 19:51

#2

帖子 me » 2008-09-05 16:37

头像
mszjk
帖子: 94
注册时间: 2008-05-25 23:13

#3

帖子 mszjk » 2008-09-05 16:55

不行阿。。。
qq群45420595,欢迎加入,共同努力。
“上”或“下“,那得看你怎么看了。
头像
mszjk
帖子: 94
注册时间: 2008-05-25 23:13

#4

帖子 mszjk » 2008-09-05 17:03

晕倒了,我概念没搞清楚。。。对C++程序,gcc命令只能编译,不能完成库的链接。
qq群45420595,欢迎加入,共同努力。
“上”或“下“,那得看你怎么看了。
头像
mszjk
帖子: 94
注册时间: 2008-05-25 23:13

#5

帖子 mszjk » 2008-09-05 17:04

晕倒了,我概念没搞清楚。。。对C++程序,gcc命令只能编译,不能完成库的链接。太丢脸了。。。
qq群45420595,欢迎加入,共同努力。
“上”或“下“,那得看你怎么看了。
头像
自由建客
帖子: 13468
注册时间: 2008-07-30 23:21
系统: Debian stable AMD64

#6

帖子 自由建客 » 2008-09-05 17:59

用g++
keoki
帖子: 9
注册时间: 2007-04-22 0:56

#7

帖子 keoki » 2008-09-08 12:13

##To compile without using a makefile
# To compile an object file from a source file you could execute
# g++ -ansi -W -Wall -c filename.cc # produces filename.obj
# To compile an executable file from an object file, you would execute
# g++ -ansi -W -Wall filename.o # produces filename.exe
# To compile an executable file from a source file, you would execute
# g++ -ansi -W -Wall filename.cc # produces filename.exe
##
poet
帖子: 2841
注册时间: 2006-09-11 22:47

#8

帖子 poet » 2008-09-08 12:53

mszjk 写了:晕倒了,我概念没搞清楚。。。对C++程序,gcc命令只能编译,不能完成库的链接。太丢脸了。。。
谁说不能完成?你究竟看了给你的那个帖子没有?这三行代码任意一行都可以编译+链接,第二行就是 gcc。

本文是写给 gcc 新手的入门文章,所以内容比较简单。如果你知道下面3条命令都可以编译c++的话,就不用在本文浪费时间了
代码:
g++ -Wall hellocpp.cpp
gcc -Wall hellocpp.cpp -lstdc++
gfortran -Wall hellocpp.cpp -lstdc++
wstoneh
帖子: 38
注册时间: 2008-09-01 15:57

#9

帖子 wstoneh » 2008-09-16 21:35

gcc是对c的。用g++才可以,不管你是否能正确的编译,g++的连接和gcc的连接是不一样的,所以要使用g++
编译时也应该用g++才对
hailingege
帖子: 8
注册时间: 2009-08-27 11:03

Re: 不能用gcc编译cpp

#10

帖子 hailingege » 2009-08-27 11:05

楼主没看清啊 仔细点 :em02
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

Re:

#11

帖子 tusooa » 2009-08-29 16:23

wstoneh 写了:gcc是对c的。用g++才可以,不管你是否能正确的编译,g++的连接和gcc的连接是不一样的,所以要使用g++
编译时也应该用g++才对
晕倒,怎么不能啊 :em20
我的就可以啊

代码: 全选

>> gcc main.cc -lstdc++

代码: 全选

] ls -ld //
头像
wangdu2002
帖子: 13284
注册时间: 2008-12-13 19:39
来自: 物华天宝人杰地灵

Re: 不能用gcc编译cpp

#12

帖子 wangdu2002 » 2009-08-29 17:59

还是用gcc编译c程序,用g++编译c++程序为好。 :em09
行到水穷处,坐看云起时。
海内生明月,天涯共此夕。
--------------------吾本独!
回复