一个适用于多文件编译的环境配置方法,可能因系统不同而有差别,不保证通用,我的是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, 编译