准备
- gcc: 需要参考windows+cpp(二):安装cpp开发环境 MinGW 或 msys2 或 Cygwin 安装 MinGW 或 msys2 (推荐后者)
- vscode编写cpp的基本配置tasks.json, launch.json, .json, 可参考windows+cpp(三): VS code + MinGW, windows上编写调试cpp
Makefile
- 创建一个main.cpp
#include <iostream> int main() { std::cout << "hello\n"; system("pause"); return 0; }
- 编写Makefile
生成的可执行文件名为外部定义的变量target# 1. 获得外部传入的参数, in Makefile $(var) is same as ${var} TARGET = $(target) CCFLAGS = ${CFLAG} # 2. 定义编译相关的参数 CC = g++ CCFLAGS += -Wall # include dep .h file header_path = -I . # 3. 获得所有的.h .cpp文件以及要编译生成的.o文件名 # run shell command mkdir $(buildFolder), window not support such systax, need msys2 buildFolder=Build/ $(shell mkdir $(buildFolder)) # get all *.h and *.cpp file deps = $(wildcard *.h) src = $(wildcard *.cpp) # replace suffix .cpp to .o, if xxx.cpp in src, get xxx.o obj = $(patsubst %.cpp, $(buildFolder)%.o, $(src)) # print on terminal $(info 'deps' $(deps)) $(info 'src' $(src)) $(info 'obj' $(obj)) # 4. 定义每个task对应的要执行的命令 $(TARGET): $(obj) $(CC) ${CCFLAGS} -o $(TARGET) $(obj) $(header_path) $(buildFolder)%.o: %.cpp $(deps) $(CC) ${CCFLAGS} -c $< -o $@ $(header_path) .PHONY: clean clean: rm -rf $(buildFolder) $(target)
- 编译
make target='main.exe' # 编译 ./main.exe make target='main.exe' clean # 清除
Build with Makefile on VSCode
-
Terminal > Configure Default Build Task > C/C++: g++ build active file,产生tasks.json;或者之间创建.vscode/tasks.json, 编辑如下,配置了build_debug / build_release / clean三种:
通过设置target=${fileDirname}\${fileBasenameNoExtension}.exe将生成的可执行文件名定义出来,target将会传给Makefile中,定义对应的变量。
{ "version": "2.0.0", "tasks": [ { "label": "build_debug", "type": "shell", "command": "make", // run command "args": ["target=${fileDirname}\\${fileBasenameNoExtension}.exe", "CFLAG=-g"], // pass to Makefile "group": { "kind": "build", // add to Run Build Task "isDefault": true }, "problemMatcher": ["$gcc"] }, { "label": "build_release", "type": "shell", "command": "make", "args": ["target=${fileDirname}\\${fileBasenameNoExtension}.exe", "CFLAG=-O2"], // pass to Makefile "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] }, { "label": "clean", "type": "shell", "command": "make clean", "args": ["target=${fileDirname}\\${fileBasenameNoExtension}.exe"], // pass to Makefile "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$eslint-compact"] } ] }
-
选择Terminal > Run Task / Run Build Task (Ctrl +shift + B) , 选择build_debug / build_release / clean运行
Debug with Makefile on VSCode
-
选择Run->Add Configuration->C++ (GDB/LLDB)->g++.exe build and debug active file. 生成launch.json
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "g++.exe - 生成和调试活动文件", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "E:\\compiler\\ide\\msys2\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build_debug" } ] }
保证要调试的program设置要与Makefile生成的可执行文件名一致,前面生成的可执行文件名为target=${fileDirname}\${fileBasenameNoExtension}.exe,因此这里也要设置为这个名字。
-
回到main.cpp, 设置断点,F5开始debug.