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,二者等价。
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,二者等价。
|