I would have made a Makefile
that looks like this, so it use the
rules that are already in make(1)
.
Try make -f /dev/null --print-data-base
to see them. Suggest grep
-ing COMPILE.c
and LINK.c
to get the important stuff. COMPILE.cpp
, COMPILE.C
and COMPILE.cxx
are for c++ and COMPILE.c
are for c.
#!/bin/make
# Makefile
# 2020-02-10 Anders Jackson
# Basic setup
CXX = g++
CXXFLAGS = -c -g -Wall -Wextra
CXXFLAGS += -I../../DrAPI/
LDFLAGS =
LDLIBS = -ldl -lrt
# `pkg-config --list-all | grep -e mongo` will find if installed
# Here is the libmongoc-1.0 setup.
CXXFLAGS += `pkg-config --cflags libmongoc-1.0`
LDFLAGS += `pkg-config --libs-only-L libmongoc-1.0`
LDLIBS += `pgk-config --libs-only-other libmongoc-1.0`
LDLIBS += `pkg-config --libs-only-l libmongoc-1.0`
# Adjusting for this project
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECS = main
all: $(EXECS) # Link exec from objects
$(OBJECTS): # Make all .o from .cpp (no local header files)
clean:
-rm $(OBJECTS)
-rm $(EXECS)
# eof
Notice that here there that object files only has dependencies on the source file. If a source file has some local header file dependencies, you need to add them manually.
This file also see to that when linking, those -L
switches are before object files and -l
switches are after.
If you try to just use just one pkg-config --libs
in LDFLAGS
, it will not work.
I also just add one thing at the time to the variables with +=
, like for LDLIBS
, as it makes it easier to see what is added.
The built in rules are:
OUTPUT_OPTION = -o $@
# Compilation for C and C++
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $(OUTPUT_OPTION) $<
COMPILE.cpp = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $(OUTPUT_OPTION) $<
# Linking for C and C++
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@
LINK.cpp = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@
It helps to understand which variable goes where.