1. VSCode
下载:https://code.visualstudio.com/
此处我用Ubuntu虚拟机,下载.deb类型
新建项目文件夹,test
在文件夹中打开终端输入 code .
以下三款插件是在Linux下开发C/C++的三款必备插件~
C/C++
CMake
CMake Tools
新建源码文件test.cpp
编辑测试代码:
#include<iostream>
using namespace std;
int main()
{
cout << "This is a test file!" << endl;
return 0;
}
测试:
2. CMake简单使用
在 linux 平台下使用 CMake 构建C/C++工程的流程如下:
- 手动编写 CMakeLists.txt
- 执行命令 cmake PATH 生成 Makefile ( PATH 是顶层CMakeLists.txt 所在的目录 )。
- 执行命令 make 进行编译。
2.1 手动编写 CMakeLists.txt
新建CMakeLists.txt
文件,名字必须是CMakeLists.txt,大小写区分
查看cmake版本:cmake --version
这里输出3.10.2版本
最简单的CMakeLists.txt,等价于g++ test.cpp -o main
,复杂的参考:CMakeLists.txt 语法介绍与实例演练 增加就行
cmake_minimum_required(VERSION 3.0) # 最低版本要求设置为3.0
project(test) # 工程名称
add_executable(main test.cpp) # 生成可执行文件 编译test.cpp生成可执行文件main
2.2 执行命令 cmake PATH 生成 Makefile ( PATH 是顶层CMakeLists.txt 所在的目录 )。
编译操作:分为内部编译和外部编译,推荐用更为广泛使用的外部编译法
首先说一下内部编译法:cmake .
,即生成的文件都在当前.cpp同级目录下
## 内部构建
# 在当前目录下,编译本目录的CMakeLists.txt,生成Makefile和其他文件
cmake .
# 执行make命令,生成target
make
(base) joke@ubuntu:~/桌面/test$ cmake .
// ----------检测编译器-------------
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
// -----------------------------------------------------------------
-- Configuring done // 生成配置信息
-- Generating done // 生成完成
-- Build files have been written to: /home/joke/桌面/test // 文件输出位置
生成的新文件
Makefile文件是什么?
Makefile可以简单的认为是一个工程文件的编译规则,描述了整个工程的编译和链接等规则。其中包含了那些文件需要编译,那些文件不需要编译,那些文件需要先编译,那些文件需要后编译,那些文件需要重建等等。编译整个工程需要涉及到的,在 Makefile 中都可以进行描述。换句话说,Makefile 可以使得我们的项目工程的编译变得自动化,不需要每次都手动输入一堆源文件和参数。
2.3 执行命令 make 进行编译
接下来就是make 这个Makefile文件
输入指令make
(base) joke@ubuntu:~/桌面/test$ make
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/test.cpp.o // 正在构建test.cpp.o
[100%] Linking CXX executable main // 链接 main 文件
[100%] Built target main // 生成目标文件,即main
执行一下可执行文件main
(base) joke@ubuntu:~/桌面/test$ ./main
This is a test file!
good!!!没问题
是时候开始外部构建
## 外部构建
# 1. 在当前目录下,创建build文件夹
mkdir build
# 2. 进入到build文件夹
cd build
# 3. 编译上级目录的CMakeLists.txt,生成Makefile和其他文件
cmake ..
# 4. 执行make命令,生成target
make
外部构建可以即将生成的冗余文件放入一个build文件夹中,
以下为实际操作实例
(base) joke@ubuntu:~/桌面/test$ mkdir build //新建文件夹
(base) joke@ubuntu:~/桌面/test$ cd build
(base) joke@ubuntu:~/桌面/test/build$ cmake .. //编译
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/joke/桌面/test/build
(base) joke@ubuntu:~/桌面/test/build$ make //生成可执行文件 main
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/test.cpp.o
[100%] Linking CXX executable main
[100%] Built target main
(base) joke@ubuntu:~/桌面/test/build$ ./main // 执行main
This is a test file!
文件目录结构如下:
2.4 多目录工程
修改CMakeLists文件即可,其他操作照旧
test.h
#pragma once
#include<iostream>
void show();
int test();
test.cpp
#include"test.h"
using namespace std;
void show()
{
cout << "This is show function!" << endl;
}
int test()
{
cout << "This is a test file!" << endl;
show();
return 0;
}
main.cpp
#include"test.h"
int main()
{
test();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.0) # 最低版本要求设置为3.0
project(main) # 工程名称
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -o2 -Wall") # 设置g++编译参数 -g生成带调试信息的可执行文件,-o2优化源代码,-Wall 打印警告信息
include_directories(include) # 向工程添加多个特定的头文件搜索路径 --->相当于指定g++编译器的-I参数
add_executable(main main.cpp src/test.cpp) # 生成可执行文件 编译main.cpp生成可执行文件main
致谢UP主xiaobing1016:基于VSCode和CMake实现C/C++开发 | Linux篇