-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathMakefile
65 lines (54 loc) · 1.95 KB
/
Makefile
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
55
56
57
58
59
60
61
62
63
64
65
CC_DL = gcc -ggdb3 -std=c89 -Wall -Wextra
CC = $(CC_DL) -pedantic-errors
.PHONY: all clean test
all: maina.out mainso.out mainso_fullpath.out dlopen.out
test: all
./maina.out
LD_LIBRARY_PATH=. ./mainso.out
./mainso_fullpath.out
LD_LIBRARY_PATH=. ./dlopen.out
mainso.out: main.o libcirosantilli_ab.so
@# Will look for lib with basename *exactly* `libcirosantilli_ab.so`,
@# `libcirosantilli_ab.so.1` will not do!
$(CC) -L'.' main.o -o mainso.out -lcirosantilli_ab
@#
@# With ':' uses full basename.
@# Only works for basename, not absolute path.
@# Application: select an specific version such as `libcirosantilli_ab.so.1`
@##$(CC) -L"." main.o -o mainso.out -l:libcirosantilli_ab.so
@#env LIBRARY_PATH=$LIBRARY_PATH:. $(CC) main.c -o mainso.out -lcirosantilli_ab
@#
@# TODO how to use rpath?
@#$(CC) -Wl,-rpath,. main.o -o mainso.out -lcirosantilli_ab
# This is not recommended.
#
# Better use linker path as in mainso.out.
#
# readout -d shows that the ouptut stores the full path.
#
mainso_fullpath.out: main.o libcirosantilli_ab.so
$(CC) main.o '$(shell printf "`pwd`/libcirosantilli_ab.so")' -o mainso_fullpath.out
@# Does not work
@#$(CC) main.o -o mainso_fullpath.out -l"$(shell realpath libcirosantilli_ab.so)"
maina.out: main.o ab.a
$(CC) main.o ab.a -o maina.out
# If we use -pedantic-errors, build fails:
#
# ISO C forbids conversion of object pointer to function pointer type
#
# There seems to be no no undefined way of doing it:
# https://stackoverflow.com/questions/14134245/iso-c-void-and-function-pointers
#
# -dl required otherwise: https://stackoverflow.com/questions/956640/linux-c-error-undefined-reference-to-dlopen
dlopen.out: dlopen.c
$(CC_DL) -o '$@' '$<' -ldl
libcirosantilli_ab.so: a.o b.o
$(CC) -shared a.o b.o -o libcirosantilli_ab.so
@# Same?
@#$(CC) -shared -Wl,-soname,libcirosantilli_ab.so a.o b.o -o libcirosantilli_ab.so
ab.a: a.o b.o
ar rcs ab.a a.o b.o
%.o: %.c
$(CC) -fPIC -c '$<' -o '$@'
clean:
rm -rf *.o *.a *.so *.out