g++的基本使用

一 点睛

g++是GNU组织推出的C++编译器。它不但可以用来编译传统的C++程序,也可以用来编译现代C++,比如C++11/14等。

g++的用法和gcc类似,编译C++的时候比gcc更简单,因为它会自动链接到C++标准库,而不像gcc需要手工指定。

g++编译程序的内部过程和gcc一样,也要经过4个阶段:预处理、编译、汇编和链接。

g++的基本语法格式如下:

g++ [选项] 准备编译的文件 [选项] [目标文件]

二 用g++编译一个C++程序

1 代码

#include <stdio.h>
int main()
{
        bool b = false;
        printf("hello, boy \n" );  
        return 0;
}

2 编译运行

[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
hello, boy

3 说明

用g++编译的C++程序不用和gcc那样手工去链接stdc++,所以用g++编译C++程序更加方便。

三 用g++编译多个C++程序

1 speaker.h

class speaker
{
    public:
        void sayHello(const char *);
};

2 speaker.cpp

#include "speaker.h"
#include <iostream>
using namespace std;
void speaker::sayHello(const char *str)
{
    cout << "Hello " << str << "\n";
}

3 testspeaker.cpp

#include "speaker.h"
int main(int argc,char *argv[])
{
    speaker speak;  //定义对象speak
    speak.sayHello("world"); //调用成员函数sayHello
    return 0;
}

4 编译运行

[root@localhost test]# g++ testspeaker.cpp speaker.cpp -o testspeaker
[root@localhost test]# ./testspeaker
Hello world

 

  • 24
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值