g++、gcc、GCC、make(makefile)、CMake(CMakeLists.txt)

GCC:GNU Compiler Collection(GUN 编译器集合),它可以编译C、C++、JAVA、Fortran、Pascal、Object-C、Ada等语言。
gcc是GCC中的GUN C Compiler(C 编译器),gcc调用了C compiler
g++是GCC中的GUN C++ Compiler(C++编译器),g++调用了C++ compiler

gcc和g++的主要区别

  1. 对于 .c和.cpp文件,gcc分别当做c和cpp文件编译(c和cpp的语法强度是不一样的)
  2. 对于 .c和.cpp文件,g++则统一当做cpp文件编译
  3. 使用g++编译文件时,g++会自动链接标准库STL,而gcc不会自动链接STL
  4. gcc在编译C文件时,可使用的预定义宏是比较少的
  5. gcc在编译cpp文件时/g++在编译c文件和cpp文件时(这时候gcc和g++调用的都是cpp文件的编译器),会加入一些额外的宏,这些宏如下:
#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern
  1. 在用gcc编译c++文件时,为了能够使用STL,需要加参数 –lstdc++ ,但这并不代表 gcc –lstdc++ 和 g++等价,它们的区别不仅仅是这个
    主要参数
-g - turn on debugging (so GDB gives morefriendly output)
-Wall - turns on most warnings
-O or -O2 - turn on optimizations
-o - name of the output file
-c - output an object file (.o)
-I - specify an includedirectory
-L - specify a libdirectory
-l - link with librarylib.a
使用示例:g++ -ohelloworld -I/homes/me/randomplace/include helloworld.C

gcc -c 后可跟多个输入源文件,最终输出的可执行文件以-o表示.
-o后紧着希望生成的可执行文件的名称。
-c 选项表示只编译源文件,而不进行链接,所以对于链接中的错误是无法发现的
如果不使用-c选项则仅仅生成一个可执行文件,没有目标文件。

# gcc -c hello.c -o hello

windows 安装MinGW64
https://nchc.dl.sourceforge.net/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-v7.0.0.zip
MinGW64\bin 加入系统环境变量path
MinGW64\bin下有mingw32-make.exe;重命名为make.exe以方便使用

make工具的定义是通过编写的makefile脚本文件描述整个工程的编译、链接规则;通过脚本文件,对于复杂的工程也可以只通过一个命令就完成整个编译过程。

// Main.c
#include <stdio.h>
#include <stdlib.h>
#include "max.h"
 
int main(void)
{
    printf("The bigger one of 3 and 5 is %d\n", max(3, 5));
    system("pause");
    return 0;
}
// max.h
int max(int a, int b);
// max.c
#include "max.h"
 
int max(int a, int b)
{
    return a > b ? a : b;
}
//makefile
Main.exe: Main.o max.o
    gcc -o Main.exe Main.o max.o
 
Main.o: Main.c max.h
    gcc -c Main.c
 
max.o: max.c max.h
    gcc -c max.c
//执行make,生成Main.exe 和目标文件
# make

CMakeLists.txt文件 --CMake命令--> makefile文件 --make命令-->目标文件/可执行文件

# CMakeLists.txt
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)

# 项目信息
project (Demo1)

# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)

# 指定生成目标
add_executable(Demo1 ${DIR_SRCS})

下载cmake,配置环境变量

//执行cmake,生成MinGW 的makefile
# cmake -G “MinGW Makefiles” .
# make
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 GCC是以GPL许可证所发行的自由软件,也是GNU计划的关键部分。GCC的初衷是为GNU操作系统专门编写...
    岁月不减深情阅读 1,256评论 0 0
  • 附上64位windows版本的安装包链接:https://pan.baidu.com/s/1a0nn6ATMV4v...
    七天七念阅读 2,859评论 3 2
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,727评论 0 3
  • MinGW-w64安装教程——著名C/C++编译器GCC的Windows版本 本文主要讲述如何安装 C语言 编译器...
    隐剑鬼吊阅读 105,473评论 12 64
  • 走读兴福寺,了解佛世界 樊登读书会 纪晓静 10月13号清晨,我们一路呼吸着桂花的香味来到兴福寺,这是我带着儿子...
    77纪晓静阅读 696评论 0 1