windows+cpp(四): vscode + Makefile开发cpp

准备

Makefile

  1. 创建一个main.cpp
    #include <iostream>
    
    int main()
    {
        std::cout << "hello\n";
        system("pause");
        return 0;
    }
    
  2. 编写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)
    
    
  3. 编译
    make target='main.exe' # 编译
    ./main.exe
    make target='main.exe' clean # 清除 
    

Build with Makefile on VSCode

  1. 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"]
            }
    	]
    }
    
  2. 选择Terminal > Run Task / Run Build Task (Ctrl +shift + B) , 选择build_debug / build_release / clean运行

Debug with Makefile on VSCode

  1. 选择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,因此这里也要设置为这个名字。

  2. 回到main.cpp, 设置断点,F5开始debug.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值