版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/88410794
一 点睛
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