一. Make
在 認識編譯器和C/C++編譯 一文中介紹過,一個 .c/.cpp 文件從源文件到目標文件的過程叫做編譯,但是一個項目中不可能只存在一個文件,這就涉及到多個文件的編譯問題,在編譯的過程中必然涉及某個文件的先編譯,某個文件的后編譯。構建過程就是安排文件的編譯先后關系。
Make 就是一種構建工具,屬于 GNU 項目。在 Mac 上輸入 make -version 可查看 make 工具的版本。
>> 執行
make -version
>> 輸出
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for i386-apple-darwin11.3.0
二. Makefile
make 命令執行時,需要一個 makefile 文件,以告訴 make 命令如何去編譯和鏈接程序。makefile 規則的編寫可參考 跟我一起寫Makefile
舉個例子,現在有四個文件
>> add.cpp
int add (int num1,int num2) {
return num1 + num2 + 200;
}
>> div.cpp
int div(int num1,int num2) {
return num1 / num2;
}
>> sub.cpp
int sub(int num1,int num2) {
return num1 - num2;
}
>> hello.cpp
#include <stdio.h>
int add(int num1,int num2);
int sub(int num1,int num2);
int div(int num1,int num2);
int main(int argc,char* argcv[]) {
int a = 20;
int b = 10;
printf("%d+%d=%d",a,b,add(a,b));
printf("%d-%d=%d",a,b,sub(a,b));
printf("%d/%d=%d",a,b,div(a,b));
}
由于 hello.cpp 依賴 add.cpp 、div.cpp 、sub.cpp , 所以按照正常的邊緣步驟是:
gcc hello.cpp div.cpp sub.cpp add.cpp -o hello
>> 拆分來就是
先生成所有的 .o 文件
gcc -c add.cpp -o add.o
gcc -c div.cpp -o div.o
gcc -c sub.cpp -o sub.o
gcc -c hello.cpp -o hello.o
gcc hello.o div.o sub.o add.o -o hello (沒有 -c)
使用 Make 就需要編寫 Makefile 文件,在源文件目錄下添加 Makefile 文件
hello.out:hello.o sub.o div.o add.o
gcc hello.o sub.o div.o add.o -o hello.out
div.o:div.cpp
gcc -c div.cpp -o div.o
sub.o:sub.cpp
gcc -c sub.cpp -o sub.o
add.o:add.cpp
gcc -c add.cpp -o add.o
hello.o:hello.cpp
gcc -c hello.cpp -o hello.o
執行 make 命令,make 會自動查找 Makefile 文件并執行。這樣就免去了一步步手動編譯文件。
三. CMake和CMakeLists.txt
雖然 Make 和 Makefile 簡化了手動構建的過程,但是編寫 Makefile 文件仍然是一個麻煩的工作,因此就有了 CMake 工具。CMake 工具用于生成 Makefile 文件,而如何生成 Makefile 文件,則由 CMakeLists.txt 文件指定。
舉例:通過 CMakeLists.txt 編譯 hello.cpp
>> hello.cpp
#include <stdio.h>
int main(int argc,char* argcv[]) {
int a = 20;
int b = 10;
printf("%d+%d",a,b);
return 0;
}
>> 在同目錄下編寫 CMakeLists.txt
PROJECT (HELLO)
SET(SRC_LIST hello.cpp)
MESSAGE(STATUS "this is BINARY dir" ${HELLO_BINDARY_DIR})
MESSAGE(STATUS "this is SOURCE dir" ${HELLO_SOURCE_DIR})
MESSAGE(STATUS "this is PRPOJECT_SOURCE" ${PRPOJECT_SOURCE_DIR})
ADD_EXECUTABLE(hello.out ${SRC_LIST})
>> 執行 cmake CMakeLists.txt 生成 Makefile 文件
>> 執行 make 命令編譯 hello.cpp 生成 hello.o
最后生成產物如下:
image.png
總的來說,Make、Makefile、CMake 和 CMakeLists.txt 的關系可總結為下圖:
image.png