Linux环境下配置vscode的C/C++ 的make编译环境(编写makefile方式)
主要前提内容参照前文:
===========================================================
代码结构:
main.cpp 代码:
#include <iostream> #include "swap.h" int main() { Swap myswap(10, 20); myswap.printInfo(); myswap.run(); myswap.printInfo(); cout << "After swap" << endl; return 0; }
swap.cpp 代码:
#include "swap.h" void Swap::run(){ int temp; temp = _a; _a = _b; _b = temp; } void Swap::printInfo() { cout << "_a = " << _a << endl; cout << "_b = " << _b << endl; }
swap.h 代码:
#pragma once #include<iostream> using namespace std; class Swap { public: Swap(int a, int b){ this->_a = a; this->_b = b; } void run(); void printInfo(); private: int _a; int _b; };
======================================
配置文件:
.vscode/tasks.json
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++-7 生成活动文件", "command": "/usr/bin/g++-7", "args": [ "-fdiagnostics-color=always", "-g", "${workspaceFolder}/main.cpp", "${workspaceFolder}/src/swap.cpp", "-o", "${workspaceFolder}/mainX", "-I", "${workspaceFolder}/include" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$g++" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" }, { "type": "shell", "label": "make", "command": "make", "options": { "cwd": "${workspaceFolder}", }, "detail": "make调试器生成的任务。" } ], "version": "2.0.0" }
.vscode/launch.json
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "g++-7 - 生成和调试活动文件", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/build/mainX", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "make", "miDebuggerPath": "/usr/bin/gdb" } ] }
Makefile 文件内容:
# C compiler options
CC = g++
# C compiler flags
CFLAGS = -g -O2 -ggdb3
#CFLAGS = -g -O2
CLEAN = clean
#
RELEASE = mainX
DEBUG = mainX.debug
LIBS =
INC = include
# Make everything
#all: $(RELEASE) $(DEBUG)
all: $(RELEASE) $(CLEAN)
# Make the application
$(RELEASE): main.o swap.o
$(CC) -o build/$(RELEASE) $(CFLAGS) src/main.o src/swap.o
main.o: main.cpp
$(CC) -o src/main.o $(CFLAGS) -c main.cpp -I $(INC)
swap.o: src/swap.cpp
$(CC) -o src/swap.o $(CFLAGS) -c src/swap.cpp -I $(INC)
# Clean all object files...
clean:
rm src/*.o
# rm $(RELEASE)
# rm $(DEBUG)
给出更简洁的Makefile :
CC = gcc -I include CXX = g++ -I include CFLAGS = -g -O2 -ggdb3 #CFLAGS = -g -O2 CLEAN = clean BUILD = build SRC = $(shell find -name '*.cpp') OBJECT = $(SRC:.cpp=.o) # RELEASE = mainX DEBUG = mainX.debug LIBS = INC = -I include # Make everything all: $(RELEASE) $(CLEAN) # Make the application $(RELEASE): $(OBJECT) $(CXX) -o $@ $^ $(CFLAGS) # @echo $(CXX) # @echo $(SRC) # @echo $(OBJECT) @echo $@ @echo $< @echo $^ @echo $(@D) @echo $(<D) @echo $(^D) @echo $(@F) @echo $(<F) @echo $(^F) $(OBJECT): # Clean all object files... .PHONY: clean clean: rm $(OBJECT) # rm $(RELEASE) # rm $(DEBUG)
================================================
主要关键点,配置tasks.json中的make任务:
核心部分:
{
"type": "shell",
"label": "make",
"command": "make",
"options": {
"cwd": "${workspaceFolder}",
},
"detail": "make调试器生成的任务。"
}
其中最为关键的是 make 任务的 type要改为 shell ,否则会报错。
==========================================
本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址,还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注处,如有侵权请与博主联系。
如果未特殊标注则为原创,遵循 CC 4.0 BY-SA 版权协议。
posted on 2021-12-02 15:52 Angry_Panda 阅读(1318) 评论(0) 编辑 收藏 举报