-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
54 lines (43 loc) · 1.31 KB
/
Makefile
File metadata and controls
54 lines (43 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Lesson 10: Shared Library
#
# A shared library (.so on Linux, .dylib on macOS) is loaded
# at runtime. The executable doesn't contain the library code —
# it contains a reference to it.
#
# Key flags:
# -fPIC Position Independent Code — required for shared libs
# -shared tell the linker to produce a shared library
# -rpath embed the library search path in the executable
# so it finds the .dylib/.so at runtime without
# setting LD_LIBRARY_PATH / DYLD_LIBRARY_PATH
#
# On macOS the extension is .dylib, on Linux it's .so.
# We detect the platform with uname.
CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -Iinclude -MMD -MP -fPIC
TARGET = app
UNAME := $(shell uname)
ifeq ($(UNAME),Darwin)
LIB = libstrutil.dylib
RPATH_FLAG = -Wl,-rpath,@executable_path
else
LIB = libstrutil.so
RPATH_FLAG = -Wl,-rpath,'$$ORIGIN'
endif
LIB_SRCS = strutil.cpp
LIB_OBJS = $(LIB_SRCS:.cpp=.o)
APP_SRCS = main.cpp
APP_OBJS = $(APP_SRCS:.cpp=.o)
DEPS = $(LIB_OBJS:.o=.d) $(APP_OBJS:.o=.d)
VPATH = src
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(APP_OBJS) $(LIB)
$(CXX) -o $@ $(APP_OBJS) -L. -lstrutil $(RPATH_FLAG)
$(LIB): $(LIB_OBJS)
$(CXX) -shared -o $@ $^
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
clean:
rm -f $(TARGET) $(LIB) $(LIB_OBJS) $(APP_OBJS) $(DEPS)
-include $(DEPS)