diff options
| author | Shivesh Mandalia <mail@shivesh.org> | 2022-04-16 18:01:59 +0100 |
|---|---|---|
| committer | Shivesh Mandalia <mail@shivesh.org> | 2022-04-16 18:01:59 +0100 |
| commit | b01ea4aaf97a50c11c649be6e4dc10dcde5ebf17 (patch) | |
| tree | 3e440e7f832b3d12d0008f58728c738885104646 | |
| download | basic_makefile-b01ea4aaf97a50c11c649be6e4dc10dcde5ebf17.tar.gz basic_makefile-b01ea4aaf97a50c11c649be6e4dc10dcde5ebf17.zip | |
Initial commit
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 144 | ||||
| -rw-r--r-- | main.cpp | 14 | ||||
| -rw-r--r-- | math.cpp | 1 |
4 files changed, 161 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6142305 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +*.d diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bc4ee5d --- /dev/null +++ b/Makefile @@ -0,0 +1,144 @@ +### Settings: Modify the parameters below to configure your project +###============================================================================ + +# program name and source directories +BIN_NAME := +SRC_DIRS := + +# general program flags +# Debug +MY_CFLAGS = -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -g -fsanitize=address,undefined +# Release +# MY_CFLAGS = -O3 +MY_LIBS = +WARNINGS = -fstack-protector-strong -fpie -pedantic -Wall -Wextra + +# language specific options +CFLAGS = -std=c11 +CXXFLAGS = -std=c++17 + +# compiler defaults +CC = gcc +CXX = g++ + +# static analysis / formatting tools +CFORMAT = clang-format +CTIDY = clang-tidy + +### Advanced: You should generally not need to modify anything below this line +###============================================================================ + +## If no source dirs are specified use files in current working directory +##----------------------------------------------------------------------------- +ifeq ($(SRC_DIRS),) + SRC_DIRS := . +endif + +## Check if the software being built is a shared library +##----------------------------------------------------------------------------- +BIN_SHARED := $(filter -shared,$(MY_CFLAGS)) + +## If no program name is specified use current directory name or a.out +##----------------------------------------------------------------------------- +EMPTY := +SPACE := $(EMPTY) $(EMPTY) +ifeq ($(BIN_NAME),) + BIN_NAME := $(lastword $(subst /,$(SPACE),$(subst $(SPACE),_,$(CURDIR)))) + ifneq ($(BIN_SHARED),) + BIN_NAME := $(addsuffix .so,$(addprefix lib,$(BIN_NAME))) + endif +endif +ifeq ($(BIN_NAME),) + BIN_NAME := a.out +endif + +## Find source files in specified directories and name objects and dependencies +##----------------------------------------------------------------------------- +SRC_EXTS := .c .C .cc .cp .cpp .CPP .cxx .c++ +SRC_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(addprefix $(dir)/*,$(SRC_EXTS)))) +SRC_CXX := $(filter-out %.c,$(SRC_FILES)) +OBJECTS := $(addsuffix .o,$(basename $(SRC_FILES))) +DEPENDS := $(OBJECTS:.o=.d) + +## Compiler and linker related actions +##----------------------------------------------------------------------------- +CMP_C := $(CC) $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS) $(WARNINGS) -MD +LNK_C := $(CC) $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) + +CMP_CXX := $(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(WARNINGS) -MD +LNK_CXX := $(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) + +## Clear built-in .SUFFIXES and set .PHONY targets +##----------------------------------------------------------------------------- +.SUFFIXES: + +.PHONY: all check clean format objects + +## Debug values of makefile variables, to check use 'make print-VARNAME' +##----------------------------------------------------------------------------- +print-%: ; @echo $*=$($*) + +## Build everything +##----------------------------------------------------------------------------- +all: format check $(BIN_NAME) + +## Static analysis +##----------------------------------------------------------------------------- +check: $(SRC_FILES) + $(CTIDY) $(CPPFLAGS) $(SRC_FILES) + +## Formatting +##----------------------------------------------------------------------------- +format: $(SRC_FILES) + $(CFORMAT) -i $(SRC_FILES) + +## Clean up build +##----------------------------------------------------------------------------- +clean: + $(RM) $(DEPENDS) $(OBJECTS) $(BIN_NAME) $(BIN_NAME).exe + +## Build object files +##----------------------------------------------------------------------------- +objects: $(OBJECTS) + +%.o: %.c + $(CMP_C) -c $< -o $@ + +%.o: %.C + $(CMP_CXX) -c $< -o $@ + +%.o: %.cc + $(CMP_CXX) -c $< -o $@ + +%.o: %.cp + $(CMP_CXX) -c $< -o $@ + +%.o: %.cpp + $(CMP_CXX) -c $< -o $@ + +%.o: %.CPP + $(CMP_CXX) -c $< -o $@ + +%.o: %.cxx + $(CMP_CXX) -c $< -o $@ + +%.o: %.c++ + $(CMP_CXX) -c $< -o $@ + +## Build dependencies +##----------------------------------------------------------------------------- +-include $(DEPENDS) + +## Build executable +##----------------------------------------------------------------------------- +$(BIN_NAME): $(OBJECTS) +ifeq ($(SRC_CXX),) + $(LNK_C) $(OBJECTS) $(MY_LIBS) -o $@ +else + $(LNK_CXX) $(OBJECTS) $(MY_LIBS) -o $@ +endif +ifeq ($(BIN_SHARED),) + @echo To execute program, type ./$@ +else + @echo To link shared library, use $@ +endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..dd41600 --- /dev/null +++ b/main.cpp @@ -0,0 +1,14 @@ +#include <iostream> + +int add_one(int x); + +int main() { + std::cout << "hello world\n"; + int* x = nullptr; + int y = 2; + x = &y; + std::cout << *x << '\n'; + auto z = add_one(*x); + std::cout << z << '\n'; + return *x; +} diff --git a/math.cpp b/math.cpp new file mode 100644 index 0000000..de418d7 --- /dev/null +++ b/math.cpp @@ -0,0 +1 @@ +int add_one(int x) { return x + 1; } |
