如何使用 GCC 编译器编译 C++ 程序?
文件 信息.c
#include<iostream>
using std::cout;
using std::endl;
int main()
{
#ifdef __cplusplus
cout << "C++ compiler in use and version is " << __cplusplus << endl;
#endif
cout <<"Version is " << __STDC_VERSION__ << endl;
cout << "Hi" << __FILE__ << __LINE__ << endl;
}
当我尝试编译 info.c
时:
gcc info.C
Undefined first referenced
symbol in file
cout /var/tmp/ccPxLN2a.o
endl(ostream &) /var/tmp/ccPxLN2a.o
ostream::operator<<(ostream &(*)(ostream &))/var/tmp/ccPxLN2a.o
ostream::operator<<(int) /var/tmp/ccPxLN2a.o
ostream::operator<<(long) /var/tmp/ccPxLN2a.o
ostream::operator<<(char const *) /var/tmp/ccPxLN2a.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
GCC编译器不是能够编译C++程序吗?在相关说明中, gcc
和 g++
有什么区别?
原文由 xyz 发布,翻译遵循 CC BY-SA 4.0 许可协议
gcc
实际上可以编译C++代码就好了。您收到的错误是 链接器 错误,而不是编译器错误。奇怪的是,如果您将编译行更改为:
这使它链接到标准 C++ 库,然后它就可以正常工作了。
但是,您应该让您的生活更轻松并使用
g++
。Rup 在 他对另一个答案的评论 中说得最好: