Permalink
Browse files

support for static libraries with LIBS

Also follows conventions better, using $(CC) and $(CXX) without
overwriting them and adding support for %(CFLAGS) and $(CXXFLAGS).
  • Loading branch information...
mortie committed Jun 6, 2017
1 parent 4c9a64b commit 410ef51d98842c4705bccf5e342c657a2f1467dd
Showing with 60 additions and 25 deletions.
  1. +24 −7 Makefile
  2. +12 −11 README.md
  3. +24 −7 example/Makefile
View
@@ -2,7 +2,8 @@
# TARGET: compilation target
#
# TOOLCHAIN: prefix for toolchain: TOOLCHAIN=aarch64-linux-gnu-
# CC: what compiler to use
# COMPILER: what compiler to use
# default: $(CC) for EXT=.c, $(CXX) for EXT=.cc
#
# FILES: list of input files
# default: all files with extension $(EXT) in $(SRC)
@@ -18,8 +19,9 @@
#
# WARN: warning flags
# default: all pedantic
# LINK: libraries to link with
# LINK: libraries to dynamically link with
# INCLUDE: include directories
# LIBS: libraries to statically link with: libfoo.a
#
# DEPS: additional targets to run before the $(TARGET) step
# JUNK: additional files to be cleaned by th clean target
@@ -44,7 +46,14 @@ endif
ifeq ($(FILES),)
FILES=$(shell find $(SRC) -name '*$(EXT)' | sed 's/^.\///')
endif
CC:=$(TOOLCHAIN)$(CC)
ifeq ($(COMPILER),)
ifeq ($(EXT),.cc)
COMPILER=$(CXX)
else
COMPILER=$(CC)
endif
endif
COMPILER:=$(TOOLCHAIN)$(COMPILER)
#
# Find .o and .d files
@@ -68,17 +77,23 @@ FLAGS:=$(FLAGS) \
$(patsubst %,-W%,$(WARN)) \
$(patsubst %,-l%,$(LINK)) \
$(patsubst %,-L%,$(INCLUDE))
ifeq ($(EXT),.cc)
FLAGS:=$(FLAGS) $(CXXFLAGS)
else
FLAGS:=$(FLAGS) $(CFLAGS)
endif
ifeq ($(DEBUG),1)
FLAGS:=$(FLAGS) $(FLAGS_DBG)
else
FLAGS:=$(FLAGS) $(FLAGS_NDBG)
endif
FLAGS:=$(strip $(FLAGS))
#
# Compile the binary
#
$(TARGET): $(OFILES) $(DEPS)
$(CC) -o $(TARGET) $(OFILES)
$(TARGET): $(OFILES) $(LIBS) $(DEPS)
$(COMPILER) $(FLAGS) -o $(TARGET) $(OFILES) $(LIBS)
@$(call log,"Created",$(TARGET))
#
@@ -94,14 +109,14 @@ clean:
dep/%.d: %$(EXT)
@mkdir -p $(@D)
@printf $(dir obj/$*) > $@
@$(CC) -MM $< -o - >> $@
@$(COMPILER) -MM $< -o - >> $@
#
# Create .o files
#
obj/%.o: %$(EXT)
@mkdir -p $(@D)
$(CC) $(strip $(FLAGS)) -o $@ -c $<
$(COMPILER) $(FLAGS) -o $@ -c $<
@$(call log,"Created",$@)
#
@@ -112,3 +127,5 @@ obj/%.o: %$(EXT)
ifneq ($(MAKECMDGOALS),clean)
-include $(DFILES)
endif
.PHONY: clean
View
@@ -33,14 +33,13 @@ To compile C++ instead of C:
`conf.mk`:
```
CC=$(CXX)
EXT=.cc
TARGET=example-program
```
Here, we set the file extension to .cc (the UNIX convention for C++ files), and
set the compiler to `$(CXX)`, which is the default C++ compiler (usually g++ on
GNU/Linux systems).
Here, we set the file extension to .cc (the UNIX convention for C++ files).
Instesad of `$(CC)`, the Makefile will now use `$(CXX)`, which is a C++
compiler (generally C++ on GNU/Linux systems).
## Slightly bigger example
@@ -75,14 +74,11 @@ Default value: `.c`
Default value: `src`
### CC
### COMPILER
`CC` is the compiler. For C, you can leave it as it is. For C++, set it to
`$(CXX)` - the `CXX` variable is predefined by make, and is generally the
preferred C++ compiler.
Default: a C compiler. Which one depends on your system, but it generally means
`cc`, which on GNU/Linux systems is a symlink to `gcc`.
`COMPILER` is the compiler. This defaults to `$(CXX)` if `EXT` is `.cc`, and
`$(CC)` otherwise. You generally don't need to change this yourself, as long as
you use the extension `.cc` for C++ and `.c` for C.
### WARN
@@ -101,6 +97,11 @@ library for example (`libm.so`): `LINK=m` (expands to `-lm`)
include a directory called `headers`: `INCLUDE=headers` (expands to
`-Lheaders`)
### LIBS
`LIBS` is a list of statically linked libraries (generally `libfoo.a`), which
the final binary will be linked with.
### FLAGS
`FLAGS` is general compiler flags.
View
@@ -2,7 +2,8 @@
# TARGET: compilation target
#
# TOOLCHAIN: prefix for toolchain: TOOLCHAIN=aarch64-linux-gnu-
# CC: what compiler to use
# COMPILER: what compiler to use
# default: $(CC) for EXT=.c, $(CXX) for EXT=.cc
#
# FILES: list of input files
# default: all files with extension $(EXT) in $(SRC)
@@ -18,8 +19,9 @@
#
# WARN: warning flags
# default: all pedantic
# LINK: libraries to link with
# LINK: libraries to dynamically link with
# INCLUDE: include directories
# LIBS: libraries to statically link with: libfoo.a
#
# DEPS: additional targets to run before the $(TARGET) step
# JUNK: additional files to be cleaned by th clean target
@@ -44,7 +46,14 @@ endif
ifeq ($(FILES),)
FILES=$(shell find $(SRC) -name '*$(EXT)' | sed 's/^.\///')
endif
CC:=$(TOOLCHAIN)$(CC)
ifeq ($(COMPILER),)
ifeq ($(EXT),.cc)
COMPILER=$(CXX)
else
COMPILER=$(CC)
endif
endif
COMPILER:=$(TOOLCHAIN)$(COMPILER)
#
# Find .o and .d files
@@ -68,17 +77,23 @@ FLAGS:=$(FLAGS) \
$(patsubst %,-W%,$(WARN)) \
$(patsubst %,-l%,$(LINK)) \
$(patsubst %,-L%,$(INCLUDE))
ifeq ($(EXT),.cc)
FLAGS:=$(FLAGS) $(CXXFLAGS)
else
FLAGS:=$(FLAGS) $(CFLAGS)
endif
ifeq ($(DEBUG),1)
FLAGS:=$(FLAGS) $(FLAGS_DBG)
else
FLAGS:=$(FLAGS) $(FLAGS_NDBG)
endif
FLAGS:=$(strip $(FLAGS))
#
# Compile the binary
#
$(TARGET): $(OFILES) $(DEPS)
$(CC) -o $(TARGET) $(OFILES)
$(TARGET): $(OFILES) $(LIBS) $(DEPS)
$(COMPILER) $(FLAGS) -o $(TARGET) $(OFILES) $(LIBS)
@$(call log,"Created",$(TARGET))
#
@@ -94,14 +109,14 @@ clean:
dep/%.d: %$(EXT)
@mkdir -p $(@D)
@printf $(dir obj/$*) > $@
@$(CC) -MM $< -o - >> $@
@$(COMPILER) -MM $< -o - >> $@
#
# Create .o files
#
obj/%.o: %$(EXT)
@mkdir -p $(@D)
$(CC) $(strip $(FLAGS)) -o $@ -c $<
$(COMPILER) $(FLAGS) -o $@ -c $<
@$(call log,"Created",$@)
#
@@ -112,3 +127,5 @@ obj/%.o: %$(EXT)
ifneq ($(MAKECMDGOALS),clean)
-include $(DFILES)
endif
.PHONY: clean

0 comments on commit 410ef51

Please sign in to comment.