本人使用的系统为 xubuntu_20.04
建立一个C项目
一个原始完整文件
首先创建文件夹 /cmake_tutorials/module1
,
新建文件 main.cpp
#include <iostream>
using namespace std;
float addition(float num1, float num2)
{
return num1 + num2;
}
float division(float num1, float num2)
{
return num1 / num2;
}
void print_result(string result_type, float result_value)
{
cout << result_type << " result:\t" << result_value << "\n";
}
int main(int argc, char const *argv[])
{
float first_no, second_no, result_add, result_div;
cout << "Enter first number\t";
cin >> first_no;
cout << "Enter second number\t";
cin >> second_no;
result_add = addition(first_no, second_no);
result_div = division(first_no, second_no);
print_result("Addition", result_add);
print_result("Division", result_div);
// cout << "Addition result:\t" << result_add
// << "\n Division result:\t" << result_div << endl;
return 0;
}
终端执行g++ main.cpp -o calculator
,./calculator
Enter first number 90
Enter second number 17
Addition result: 107
Division result: 5.29412
拆分成为多文件
main.cpp
#include <iostream>
using namespace std;
#include "addition.h"
#include "division.h"
#include "print_result.h"
int main(int argc, char const *argv[])
{
float first_no, second_no, result_add, result_div;
cout << "Enter first number\t";
cin >> first_no;
cout << "Enter second number\t";
cin >> second_no;
result_add = addition(first_no, second_no);
result_div = division(first_no, second_no);
print_result("Addition", result_add);
print_result("Division", result_div);
// cout << "Addition result:\t" << result_add
// << "\n Division result:\t" << result_div << endl;
return 0;
}
addition.h
float addition(float, float);
addition.cpp
float addition(float num1, float num2){return num1 + num2;}
division.h
float division(float, float);
division.cpp
float division(float num1, float num2){return num1 / num2;}
print_result.h
void print_result(string, float);
print_result.cpp
#include <iostream>
using namespace std;
void print_result(string result_type, float result_value)
{cout << result_type << " result:\t" << result_value << "\n";}
构建系统的需求-make
创建makefile
calculator: main.o addition.o division.o print_result.o
g++ main.o addition.o division.o print_result.o -o calculator
main.o: main.cpp
g++ -c main.cpp
addition.o: addition.cpp
g++ -c addition.cpp
division.o: division.cpp
g++ -c division.cpp
print_result.o: print_result.cpp
g++ -c print_result.cpp
执行 make,make命令将寻找makefile, 然后构建项目
g++ -c main.cpp
g++ -c addition.cpp
g++ -c division.cpp
g++ -c print_result.cpp
g++ main.o addition.o division.o print_result.o
CMake登场
make:使用生成系统文件生成可执行文件
cmake:用于生成生成系统文件
cmake 通过创建基于平台的系统文件来解决跨平台问题。
安装cmake
方法一: sudo apt install cmake
,查看版本cmake --version
方法二:官网下载编译二进制文件 binary distributions
chmod +x cmake-xxx-linux-xxx.sh
给与运行权限后执行- 直接进入 xxx/bin文件夹,可以看到 cmake 可执行文件
- 添加
system path
:~/.bashrc
文件最下方添加export PATH=/home/user/xxx/cmake-xxx-linux-xxx/bin:$PATH
方法三:下载源代码 source code
- 解压:
tar xvf xxx.tar
- 依次执行以下指令:
./bootstrap
如果遇到错误运行:./bootstrap -- -DCMAKE_USE_OPENSSL=OFF
make
sudo make install
安装完成
使用CMake生成第一个可执行文件
复制一份文件夹cp -r module1 module2
,删除目标文件 rm *.o
构建项目目录:
module2
---my_build_dir
---Other Files
---CMakeLists.txt (CMAKE文件)
编辑CMakeLists.txt
,顺序不重要,但可执行文件名必须放在第一位
cmake_minimum_required(VERSION 3.0.0)
project(Calculator_Project VERSION 1.0.0)
add_executable(calculator
main.cpp
addition.cpp
division.cpp
print_result.cpp)
进入my_build_dir
,执行 cmake ..
生成CMake项目
/module2/my_build_dir$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
执行 make
/module2/my_build_dir$ make
Scanning dependencies of target calculator
[ 20%] Building CXX object CMakeFiles/calculator.dir/main.cpp.o
[ 40%] Building CXX object CMakeFiles/calculator.dir/addition.cpp.o
[ 60%] Building CXX object CMakeFiles/calculator.dir/division.cpp.o
[ 80%] Building CXX object CMakeFiles/calculator.dir/print_result.cpp.o
[100%] Linking CXX executable calculator
[100%] Built target calculator
执行ls
,可以看到生成的可执行文件calculator
/module2/my_build_dir$ ls
calculator CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
生成第一个库
编辑CMakeCache.txt
cmake_minimum_required(VERSION 3.0.0)
project(Calculator_Project VERSION 1.0.0)
# 两个库
add_library(my_math
addition.cpp
division.cpp)
add_library(my_print
print_result.cpp)
# target 可执行文件
add_executable(calculator
main.cpp)
# 链接库与可执行文件
target_link_libraries(calculator my_math my_print)
进入my_build_dir
,执行cmake ..
, make
/module2/my_build_dir$ make
Scanning dependencies of target my_print
[ 14%] Building CXX object CMakeFiles/my_print.dir/print_result.cpp.o
[ 28%] Linking CXX static library libmy_print.a
[ 28%] Built target my_print
Scanning dependencies of target my_math
[ 42%] Building CXX object CMakeFiles/my_math.dir/addition.cpp.o
[ 57%] Building CXX object CMakeFiles/my_math.dir/division.cpp.o
[ 71%] Linking CXX static library libmy_math.a
[ 71%] Built target my_math
Scanning dependencies of target calculator
[ 85%] Linking CXX executable calculator
[100%] Built target calculator
可以看到 库和可执行文件已成功创建
/module2/my_build_dir$ ls
calculator CMakeFiles libmy_math.a Makefile
CMakeCache.txt cmake_install.cmake libmy_print.a