Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Makefile for mixed C/C++ sources and vscode project configuration (https://code.visualstudio.com/docs/languages/cpp)
{
"env": {},
"configurations": [
{
"name": "Mac",
"includePath": [
"/usr/local/include",
"/usr/local/Cellar/gcc/9.1.0/include/c++/9.1.0/",
"/usr/local/Cellar/gcc/9.1.0/include/c++/9.1.0/x86_64-apple-darwin18",
"${workspaceFolder}/**"
],
"defines": [
"DEBUG",
"ENABLE_LOGGING"
],
"compilerPath": "/usr/local/bin/g++-9",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"databaseFilename": "",
"limitSymbolsToIncludedHeaders": true,
"path": [
"${workspaceRoot}"
]
},
"macFrameworkPath": []
}
],
"version": 4
}
[
{
"key": "cmd+f9",
"command": "workbench.action.tasks.runTask",
"args": "make"
},
{
"key": "f9",
"command": "workbench.action.tasks.runTask",
"args": "make run"
},
{
"key": "f8",
"command": "workbench.action.tasks.runTask",
},
]
{
// 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
// If gdb doesn't work, please check https://forward-in-code.blogspot.com/2018/11/mojave-vs-gdb.html?m=1
"version": "0.2.0",
"configurations": [
{
"name": "Launch (gdb)",
"type": "cppdbg",
"request": "launch",
"preLaunchTask": "make",
"program": "${workspaceFolder}/out/${workspaceFolderBasename}",
"args": [],
"stopAtEntry": true,
"logging": {
"trace": true,
"traceResponse": true
},
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/local/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Launch (lldb)",
"type": "lldb",
"request": "launch",
"preLaunchTask": "make",
"program": "${workspaceFolder}/out/${workspaceFolderBasename}",
"args": [],
"cwd": "${workspaceFolder}"
},
]
}
#======================================================================
# Makefile for mixed C/C++ sources (igoticecream 2019)
#======================================================================
#----------------------------------------------------------------------
# Project Structure
#----------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# OUT is the directory where target files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing header files
# LIBRARIES is a list of directories containing libraries, this must be the top level containing include and lib
# DATA is a list of directories containing data files
# RESOURCES is a list of directories containing other files
#----------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
OUT := out
SOURCES := source
INCLUDES := include
LIBRARIES := /usr/local
RESOURCES :=
DATA :=
#----------------------------------------------------------------------
# Languages Standard
#----------------------------------------------------------------------
C_STANDARD := -std=c11
CXX_STANDARD := -std=c++17
#----------------------------------------------------------------------
# Defined Symbols
#----------------------------------------------------------------------
DEFINES += -DDEBUG
DEFINES += -DENABLE_LOGGING
#----------------------------------------------------------------------
# Sources & Files
#----------------------------------------------------------------------
OUTPUT := $(CURDIR)/$(OUT)/$(TARGET)
SYMBOLS := $(CURDIR)/$(BUILD)/$(TARGET).out
CSOURCES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.c))
CXXSOURCES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.cpp))
ASMSOURCES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.s))
OBJS := $(patsubst %,$(BUILD)/%.o,$(basename $(CSOURCES)) $(basename $(CXXSOURCES)) $(basename $(ASMSOURCES)))
DEPS := $(patsubst %,$(BUILD)/%.d,$(basename $(CSOURCES)) $(basename $(CXXSOURCES)) $(basename $(ASMSOURCES)))
INCLUDE += $(addprefix -I,$(foreach dir,$(INCLUDES), $(wildcard $(dir))))
INCLUDE += $(addprefix -I,$(foreach dir,$(LIBRARIES),$(wildcard $(dir)/include)))
LDLIBS += $(addprefix -L,$(foreach dir,$(LIBRARIES),$(wildcard $(dir)/lib)))
LDLIBS += $(addprefix -L,$(foreach dir,$(LIBRARIES),$(wildcard $(dir)/library)))
#----------------------------------------------------------------------
# Compiler & Linker
#----------------------------------------------------------------------
CC := /usr/local/bin/gcc-9
CXX := /usr/local/bin/g++-9
AS := /usr/local/bin/gcc-9
AR := /usr/local/bin/gcc-ar-9 # or /usr/local/opt/binutils/bin/ar
NM := /usr/local/bin/gcc-nm-9 # or /usr/local/opt/binutils/bin/nm
#----------------------------------------------------------------------
# Compiler & Linker Flags
#----------------------------------------------------------------------
# CPPFLAGS C and C++ Compiler Flags
# CFLAGS C Compiler Flags
# CXXFLAGS C++ Compiler Flags
# ASFLAGS ASM Compiler Flags
# LDFLAGS Linker Flags
#----------------------------------------------------------------------
CPPFLAGS += $(DEFINES) $(INCLUDE)
CPPFLAGS += -ggdb
CPPFLAGS += -g3
CPPFLAGS += -Og
#CPPFLAGS += -O3
CPPFLAGS += -fmessage-length=0
CPPFLAGS += -fsigned-char
CPPFLAGS += -fPIC
CFLAGS += $(CPPFLAGS)
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -Werror
CFLAGS += -pedantic
CFLAGS += -pedantic-errors
CFLAGS += -Wfatal-errors
CFLAGS += -Wpacked
CFLAGS += -Winline
CFLAGS += -Wfloat-equal
CFLAGS += -Wconversion
CFLAGS += -Wpointer-arith
CFLAGS += -Wdisabled-optimization
CFLAGS += -Wunknown-pragmas
CFLAGS += -Wno-unused-parameter
CFLAGS += -Wno-unused-function
CFLAGS += -Wno-unused-variable
CXXFLAGS += $(CFLAGS)
CXXFLAGS += -Weffc++
CXXFLAGS += -Wfloat-equal
CXXFLAGS += -Wsign-promo
CXXFLAGS += -Wmissing-declarations
CXXFLAGS += -Woverloaded-virtual
CXXFLAGS += -Wmissing-format-attribute
CXXFLAGS += -Wold-style-cast
CXXFLAGS += -Wshadow
CXXFLAGS += -Wctor-dtor-privacy
ASFLAGS += $(CPPFLAGS)
ASFLAGS += -x assembler-with-cpp
LDFLAGS += -lmbedtls
LDFLAGS += -lmbedx509
LDFLAGS += -lmbedcrypto
#----------------------------------------------------------------------
# Compiler & Linker Commands
#----------------------------------------------------------------------
# LINK.o link object files to binary
# COMPILE.c compile C source files
# COMPILE.cpp compile C++ source files
#----------------------------------------------------------------------
ifeq ($(strip $(CXXSOURCES)),)
LD := $(CC)
else
LD := $(CXX)
endif
DEPFLAGS += -MT $@
DEPFLAGS += -MMD
DEPFLAGS += -MP
DEPFLAGS += -MF $(BUILD)/$*.d
LINK.o = $(LD) $(LDFLAGS) $(LDLIBS) $^ -o $@
COMPILE.c = $(CC) $(C_STANDARD) $(CFLAGS) $(DEPFLAGS) -c $< -o $@
COMPILE.cpp = $(CXX) $(CXX_STANDARD) $(CXXFLAGS) $(DEPFLAGS) -c $< -o $@
COMPILE.s = $(AS) $(ASFLAGS) $(DEPFLAGS) -c $< -o $@
SYMBOLS.out = $(NM) -CSn $@ > $(SYMBOLS)
#----------------------------------------------------------------------
# Special Built-in Target
#----------------------------------------------------------------------
# .SUFFIXES disable built-in wildcard rules
# .INTERMEDIATE make will treat targets as intermediate files, and delete them
# .PRECIOUS make will not be deleted after it is no longer needed. Keep objects to speed up recompilation
# .PHONY make will run this targets unconditionally, regardless of whether a file with that name exists or what its last-modification time is
#----------------------------------------------------------------------
.SUFFIXES:
.INTERMEDIATE:
.PRECIOUS: $(OBJS) $(DEPS)
.PHONY: all clean help
#----------------------------------------------------------------------
# Targets
#----------------------------------------------------------------------
all: $(OUTPUT)
$(OUTPUT): $(OBJS)
@mkdir -p $(@D)
$(LINK.o)
$(SYMBOLS.out)
$(BUILD)/%.o: %.c
@mkdir -p $(@D)
$(COMPILE.c)
$(BUILD)/%.o: %.cpp
@mkdir -p $(@D)
$(COMPILE.cpp)
$(BUILD)/%.o: %.s
@mkdir -p $(@D)
$(COMPILE.s)
run: $(OUTPUT)
@$<
clean:
@$(RM) -r $(BUILD) $(OUT)
help:
@echo available targets: all run clean
-include $(DEPS)
{
"files.associations": {
"*.h": "c",
"*.c": "c",
"*.hpp": "cpp",
"*.cpp": "cpp",
},
"files.exclude": {
"**/.envrc": true,
"**/.DS_Store": true,
"**/out": true,
"**/build": true,
},
"code-runner.cwd": "$workspaceRoot",
"code-runner.executorMap": {
"c": "make -j4 run",
"cpp": "make -j4 run",
},
"C_Cpp.autoAddFileAssociations": false,
"C_Cpp.intelliSenseEngine": "Default",
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, ContinuationIndentWidth: 4, ConstructorInitializerIndentWidth: 4, AccessModifierOffset: -4, NamespaceIndentation: All, AlignConsecutiveAssignments: true, AlignConsecutiveDeclarations: true, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, TabWidth: 4, FixNamespaceComments: false, IndentPPDirectives: AfterHash }",
}
// https://clang.llvm.org/docs/ClangFormatStyleOptions.html
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"shell": {
"executable": "/bin/zsh",
"args": ["-c"]
},
"env": {},
"cwd": "${workspaceFolder}",
},
"presentation": {
"clear": true,
"echo": false,
"reveal": "always",
"revealProblems": "onProblem",
"focus": true,
"panel": "shared",
"showReuseMessage": true
},
"problemMatcher": [
"$gcc"
],
"tasks": [
{
"label": "make",
"type": "shell",
"command": "make -j4",
"group": {
"isDefault": true,
"kind": "build"
},
"presentation": {
"reveal": "silent",
"focus": false,
"showReuseMessage": false
}
},
{
"label": "make run",
"type": "shell",
"command": "make -j4 run",
"group": "build",
"dependsOn": ["make"]
},
{
"label": "make clean",
"type": "shell",
"command": "make -j4 clean",
"group": "none",
"presentation": {
"reveal": "silent",
"focus": false,
"showReuseMessage": false
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment