vscode环境配置(更新版)

一个适用于多文件编译的环境配置方法,可能因系统不同而有差别,不保证通用,我的是Win10。搞了很久才弄出来,希望能帮到大家。

前提条件:安装MinGW

下面开始配置,主要是准备两类文件,粘贴过去就好了
1.makefile
 

#
# 'make'        build executable file 'main'
# 'make clean'  removes all .o and executable files
#

# define the Cpp compiler to use
CXX = g++

# define any compile-time flags
CXXFLAGS	:= -std=c++17 -Wall -Wextra -g

# define library paths in addition to /usrb
#   if I wanted to include libraries not in /usrb I'd specify
#   their path using -Lpath, something like:
LFLAGS =

# define output directory
OUTPUT	:= output

# define source directory
SRC		:= src

# define include directory
INCLUDE	:= include

# define lib directory
LIB		:= lib

ifeq ($(OS),Windows_NT)
MAIN	:= main.exe
SOURCEDIRS	:= $(SRC)/* $(SRC)
INCLUDEDIRS	:= $(INCLUDE)
LIBDIRS		:= $(LIB)
FIXPATH = $(subst /,\,$1)
RM			:= del /q /f
MD	:= mkdir
else
MAIN	:= main
SOURCEDIRS	:= $(shell find $(SRC) -type d)
INCLUDEDIRS	:= $(shell find $(INCLUDE) -type d)
LIBDIRS		:= $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -f
MD	:= mkdir -p
endif

# define any directories containing header files other than /usr/include
INCLUDES	:= $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))

# define the C libs
LIBS		:= $(patsubst %,-L%, $(LIBDIRS:%/=%))

# define the C source files
SOURCES		:= $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS)))

# define the C object files 
OBJECTS		:= $(SOURCES:.cpp=.o)

#
# The following part of the makefile is generic; it can be used to 
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#

OUTPUTMAIN	:= $(call FIXPATH,$(OUTPUT)/$(MAIN))

all: $(OUTPUT) $(MAIN)

$(OUTPUT):
	$(MD) $(OUTPUT)

$(MAIN): $(OBJECTS) 
	$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS)

# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file) 
# (see the gnu make manual section about automatic variables)
.cpp.o:
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $<  -o $@

.PHONY: clean
clean:
	$(RM) $(call FIXPATH,$(OBJECTS))
	$(RM) $(OUTPUTMAIN)
	@echo Cleanup complete!

run: all
	./$(OUTPUTMAIN)

2.c_cpp_properties.json, tasks.json, launch.json, settings.json

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",    
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\MinGW\\mingw64\\bin\\gcc.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

tasks.json

// {
//     "tasks": [
//         {
//             "type": "cppbuild",
//             "label": "C/C++: gcc.exe build active file",
//             "command": "gcc",
//             "args": [
//                 "-fdiagnostics-color=always",
//                 "-g",
//                 "main.c",
//                 "-o",
//                 "main.exe"
//             ],
//             "options": {
//                 "cwd": "${fileDirname}"
//             },
//             "problemMatcher": [
//                 "$gcc"
//             ],
//             "group": {
//                 "kind": "build",
//                 "isDefault": true
//             },
//             "detail": "Task generated by Debugger."
//         }
//     ],
//     "version": "2.0.0"
// }
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "windows": {
                "command": "powershell",
                "args": [
                    "-c",
                    "mingw32-make"
                ]
            },
            "linux": {
                "command": "bash",
                "args": [
                    "-c",
                    "make"
                ]
            },
            "osx": {
                "command": "bash",
                "args": [
                    "-c",
                    "make"
                ]
            }
        },
        {
            "label": "build & run",
            "type": "shell",
            "windows": {
                "command": "powershell",
                "args": [
                    "-c",
                    "'mingw32-make run'"
                ]
            },
            "linux": {
                "command": "bash",
                "args": [
                    "-c",
                    "'make run'"
                ]
            },
            "osx": {
                "command": "bash",
                "args": [
                    "-c",
                    "'make run'"
                ]
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "windows": {
                "command": "powershell",
                "args": [
                    "-c",
                    "'mingw32-make clean'"
                ]
            },
            "linux": {
                "command": "bash",
                "args": [
                    "-c",
                    "'make clean'"
                ]
            },
            "osx": {
                "command": "bash",
                "args": [
                    "-c",
                    "'make clean'"
                ]
            }
        }
    ]
}

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": "gcc.exe - Build and debug active file",
//             "type": "cppdbg",
//             "request": "launch",
//             "program": "main.exe",
//             "args": [],
//             "stopAtEntry": false,
//             "cwd": "${fileDirname}",
//             "environment": [],
//             "externalConsole": false,
//             "MIMode": "gdb",
//             "miDebuggerPath": "D:\\MinGW\\mingw64\\bin\\gdb.exe",
//             "setupCommands": [
//                 {
//                     "description": "Enable pretty-printing for gdb",
//                     "text": "-enable-pretty-printing",
//                     "ignoreFailures": true
//                 },
//                 {
//                     "description": "Set Disassembly Flavor to Intel",
//                     "text": "-gdb-set disassembly-flavor intel",
//                     "ignoreFailures": true
//                 }
//             ],
//             "preLaunchTask": "gcc"
//         }
//     ]
// }

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "cppdbg",
            "request": "launch",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "linux": {
                "MIMode": "gdb",
                "miDebuggerPath": "gdb",
                "program": "${workspaceFolder}/output/main"
            },
            "osx": {
                "MIMode": "lldb",
                "miDebuggerPath": "lldb-mi",
                "program": "${workspaceFolder}/output/main"
            },
            "windows": {
                "MIMode": "gdb",
                "miDebuggerPath": "gdb.exe",
                "program": "${workspaceFolder}/output/main.exe"
            },
            "preLaunchTask": "build"
        }
    ]
}

settings.json

{
    "files.associations": {
        "ostream": "cpp"
    }
}

3.使用

使用自带库不需要配置,使用外部库写到-L那里

4.命令
mingw32-make clean,清理编译中出现的中间文件

mingw32-make run, 运行程序

mingw32-make, 编译
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yq要变成百万富翁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值