您的位置:首页 > 其它

gcc 与g++的区别

2014-08-01 11:43 239 查看
参考C++经典学习教材的C++ primer中文版中的例子:

filename: iotest.cpp

#include <iostream>

int main()

{

std::cout << "Enter two numbers:" << std::endl;

int v1, v2;

std::cin >> v1 >> v2;

std::cout << "The sum of " << v1 << " and " << v2

<< " is " << v1 + v2 << std::endl;

return 0;

}

编译时出现错误提示:

yuxin@T430s:~/my_cpp$ gcc iotest.cpp -o test1

/tmp/ccmiDiTk.o: In function `main':

iotest.cpp:(.text+0x13): undefined reference to `std::cout'

iotest.cpp:(.text+0x18): 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*)'

iotest.cpp:(.text+0x1d): 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> >&)'

iotest.cpp:(.text+0x25): 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> >&))'

iotest.cpp:(.text+0x31): undefined reference to `std::cin'

iotest.cpp:(.text+0x36): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'

iotest.cpp:(.text+0x45): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'

iotest.cpp:(.text+0x60): undefined reference to `std::cout'

iotest.cpp:(.text+0x65): 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*)'

iotest.cpp:(.text+0x70): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'

iotest.cpp:(.text+0x7d): 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*)'

iotest.cpp:(.text+0x87): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'

iotest.cpp:(.text+0x94): 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*)'

iotest.cpp:(.text+0x9f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'

iotest.cpp:(.text+0xa4): 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> >&)'

iotest.cpp:(.text+0xac): 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/ccmiDiTk.o: In function `__static_initialization_and_destruction_0(int, int)':

iotest.cpp:(.text+0xe3): undefined reference to `std::ios_base::Init::Init()'

iotest.cpp:(.text+0xe8): undefined reference to `std::ios_base::Init::~Init()'

collect2: ld returned 1 exit status

解决该问题:

正确的编译: g++ iotest.cpp
-o test1 或者 gcc
iotest.cpp -lstdc++
-o test2

下面就理清一下gcc和g++的关系(以下内容转自:http://www.52pojie.cn/thread-58109-1-1.html)

误区一: gcc只能编译c代码,g++只能编译c++代码

两者都可以,但是请注意:

1.后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的。C++的语法规则更加严谨一些。

2.编译阶段,g++会调用gcc,对于c++代码,两者是等价的,但是因为gcc命令不能自动和C++程序使用的库联接,所以通常用g++来完成链接,为了统一起见,干脆编译/链接统统用g++了,这就给人一种错觉,好像cpp程序只能用g++似的。

误区二:gcc不会定义__cplusplus宏,而g++会

实际上,这个宏只是标志着编译器将会把代码按C还是C++语法来解释,如上所述,如果后缀为.c,并且采用gcc编译器,则该宏就是未定义的,否则,就是已定义。

误区三:编译只能用gcc,链接只能用g++

严格来说,这句话不算错误,但是它混淆了概念,应该这样说:编译可以用gcc/g++,而链接可以用g++或者gcc -lstdc++。因为gcc命令不能自动和C++程序使用的库联接,所以通常使用g++来完成联接。但在编译阶段,g++会自动调用gcc,二者等价。
误区四:extern "C"与gcc/g++有关系

实际上并无关系,无论是gcc还是g++,用extern "c"时,都是以C的命名方式来为symbol命名,否则,都以c++方式命名。试验如下:

me.h:

extern "C" void CppPrintf(void);

me.cpp:

#include <iostream>

#include "me.h"

using namespace std;

void CppPrintf(void)

{

cout << "Hello\n";

}

test.cpp:

#include <stdlib.h>

#include <stdio.h>

#include "me.h"

int main(void)

{

CppPrintf();

return 0;

}

1. 先给me.h加上extern "C",看用gcc和g++命名有什么不同

[root@root G++]# g++ -S me.cpp

[root@root G++]# less me.s

.globl _Z9CppPrintfv //注意此函数的命名

.type CppPrintf, @function

[root@root GCC]# gcc -S me.cpp

[root@root GCC]# less me.s

.globl _Z9CppPrintfv //注意此函数的命名

.type CppPrintf, @function

完全相同!

2. 去掉me.h中extern "C",看用gcc和g++命名有什么不同

[root@root GCC]# gcc -S me.cpp

[root@root GCC]# less me.s

.globl _Z9CppPrintfv //注意此函数的命名

.type _Z9CppPrintfv, @function

[root@root G++]# g++ -S me.cpp

[root@root G++]# less me.s

.globl _Z9CppPrintfv //注意此函数的命名

.type _Z9CppPrintfv, @function

完全相同!

【结论】完全相同,可见extern "C"与采用gcc/g++并无关系,以上的试验还间接的印证了前面的说法:在编译阶段,g++是调用gcc的。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: