使用gcc编译c语言程序,用GCC编译C ++程序

在这里,我们将看到如何使用GCC(GNU C编译器)编译C ++程序。让我们考虑一下,我们要编译这个程序。

示例#include

using namespace std;

main() {

cout <

}

如果这是一个C程序,我们可以像下面这样用GCC进行编译-gcc test.c

但是,如果我们将c ++文件名放在该区域中,则可能会产生一些错误。gcc test.cpp

输出结果/tmp/ccf1KGDi.o: In function `main':

1325.test.cpp:(.text+0xe): undefined reference to `std::cout'

1325.test.cpp:(.text+0x13): undefined reference to `std::basic_ostream >&

std::operator< >(std::basic_ostream >&, char const*)'

1325.test.cpp:(.text+0x1d): undefined reference to `std::basic_ostream >& std::endl

std::char_traits >(std::basic_ostream >&)'

1325.test.cpp:(.text+0x28): undefined reference to `std::ostream::operator<

/tmp/ccf1KGDi.o: In function `__static_initialization_and_destruction_0(int, int)':

1325.test.cpp:(.text+0x58): undefined reference to `std::ios_base::Init::Init()'

1325.test.cpp:(.text+0x6d): undefined reference to `std::ios_base::Init::~Init()'

collect2: error: ld returned 1 exit status

$

这不是编译错误。这是链接错误。要添加正确的链接器,我们必须使用–lstdc ++选项。gcc test.cpp -lstdc++

输出结果$ ./a.out

Hello World. This is C++ program

$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值