Skip to content

Commit a7696e6

Browse files
committed
Working makefile snippet
1 parent be24655 commit a7696e6

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

Makefile

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Easy Sockets Makefile Implementation v 1.0
2+
# ********************************0. INCLUDE SECTION***********************************
3+
# include Makefile.extra
4+
# *****************************END OF INCLUDE SECTION**********************************
5+
# ********************************1. VARIABLES SECTION*********************************
6+
# 1 C/C++ Compiler
7+
CXX = g++
8+
CXXFLAGS = -g -O2 -Wall
9+
# 2. Compiler And Linker Keys (man gcc)
10+
LIB_NAME = libMySharedLib.so
11+
LIB_VERSION_OPTION = 1.0
12+
LIB_BUILD_DIRECTORY = .
13+
14+
LIB_COMPILE_OPTION = -fPIC
15+
LIB_LINK_OPTION = -shared
16+
17+
LANG_OPTION = -std=c++11
18+
# 3. Linking libraries
19+
LIBPATH = -L. -L..
20+
# -L/lib64/
21+
# -L/lib/
22+
LIBS =
23+
# -lc -lrt
24+
# -lrt -lpthread
25+
# 4. DEFINITIONS (PREPROCESSOR DEFINE)
26+
ROOT_DIR := ./src
27+
DEFS := -D__GXX_EXPERIMENTAL_CXX0X__
28+
# 5. INCLUDES PATH
29+
INCLUDES := -I$(ROOT_DIR)/ -I$(ROOT_DIR)/dir1 -I$(ROOT_DIR)/dir2 -I$(ROOT_DIR)/dir3 -I$(ROOT_DIR)/dir4
30+
# 6. SOURCES FILES
31+
# Directories with sources
32+
DIRS = $(ROOT_DIR)/dir1 $(ROOT_DIR)/dir2 $(ROOT_DIR)/dir3 $(ROOT_DIR)/dir4
33+
SRC_DIRS := $(addprefix / , $(DIRS))
34+
# Obtaining source files list
35+
C_SRC_FILES := $(foreach sdir, $(SRC_DIRS), $(wildcard $(sdir)/*.c))
36+
CPP_SRC_FILES := $(foreach sdir, $(SRC_DIRS), $(wildcard $(sdir)/*.cpp))
37+
# 7. OBJECTS FILE NAME
38+
C_OBJFILES = $(C_SRC_FILES:.c=.o)
39+
CPP_OBJFILES = $(CPP_SRC_FILES:.cpp=.o) $(C_OBJFILES)
40+
# $(SRCFILES:.cpp=.o)
41+
# 8. TARGETS OR RESULTING OBJ-FILE
42+
# LIB_VERSION := 1.0
43+
CPP_SHARED_LIB = $(LIB_BUILD_DIRECTORY)/$(LIB_NAME).$(LIB_VERSION_OPTION)
44+
DEFAULT_TARGET = shared-lib
45+
# *****************************END OF VARIABLES SECTION********************************
46+
# ****************************2. BUILDING TARGETS SECTION******************************
47+
# BY DEFAULT BUILDS first target (make without parameters)
48+
# TO BUILD SPECIFIC TARGETS TYPE make "target name" (without quotation marks)
49+
# each target MUST BE WRITTEN AS: Dependencies Tab(press tab key) Command
50+
# MAKEFILE SPECIAL MACROSES (STARTS WITH $):
51+
# $@ name of target
52+
# $? list of dependancies before that macro
53+
# $^ list of dependancies which independent of wheather they met before or after
54+
# $+ similar to $^ but doesn't exclude dublicates
55+
# $< first dependancy
56+
57+
# PHONY TARGET ARE TARGETS WITHOUT OUTPUT FILES
58+
.PHONY: depend clean finish create-build-dir copy-include
59+
60+
all: clean create-build-dir $(DEFAULT_TARGET) finish
61+
62+
shared-lib: clean create-build-dir $(CPP_SHARED_LIB) copy-include finish
63+
64+
create-build-dir:
65+
@ -mkdir -p $(LIB_BUILD_DIRECTORY)
66+
67+
$(CPP_SHARED_LIB):$(C_OBJFILES) $(CPP_OBJFILES)
68+
$(CXX) $(CXXFLAGS) $(LANG_OPTION) $(LIB_LINK_OPTION) $(DEFS) $(INCLUDES) $(LIBPATH) $(LIBS) -o $(CPP_SHARED_LIB) $(CPP_OBJFILES)
69+
70+
# These are the suffix replacement rules
71+
%.o : %.c
72+
$(CXX) -c $(CXXFLAGS) $(LANG_OPTION) $(LIB_COMPILE_OPTION) $(DEFS) $(INCLUDES) $< -o $@
73+
74+
%.o : %.cpp
75+
$(CXX) -c $(CXXFLAGS) $(LANG_OPTION) $(LIB_COMPILE_OPTION) $(DEFS) $(INCLUDES) $< -o $@
76+
77+
clean:
78+
@ -rm -f $(CPP_OBJFILES)
79+
@ -rm -f $(CPP_SHARED_LIB)
80+
@ -rm -rf $(LIB_BUILD_DIRECTORY)/include
81+
82+
copy-include:
83+
@ mkdir -p $(LIB_BUILD_DIRECTORY)/include
84+
@ find $(ROOT_DIR) -name *.h -exec cp {} $(LIB_BUILD_DIRECTORY)/include \;
85+
#@ cp -R $(ROOT_DIR)/*.h $(LIB_BUILD_DIRECTORY)/include
86+
87+
#remove intermediate obj files
88+
finish:
89+
@ -rm $(CPP_OBJFILES)
90+
91+
depend: $(C_SRC_FILES) $(CPP_SRC_FILES)
92+
makedepend $(INCLUDES) $^
93+
94+
# make depend needs this line
95+
# ****************************END OF BUILDING TARGETS SECTION**************************
96+

0 commit comments

Comments
 (0)