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++的主要區別
- 對于 .c和.cpp文件,gcc分別當做c和cpp文件編譯(c和cpp的語法強度是不一樣的)
- 對于 .c和.cpp文件,g++則統一當做cpp文件編譯
- 使用g++編譯文件時,g++會自動鏈接標準庫STL,而gcc不會自動鏈接STL
- gcc在編譯C文件時,可使用的預定義宏是比較少的
- 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
- 在用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