mkogg.py: Fix 'self.get_mp4_metadata(self, source)'
[blog.git] / posts / Linking / Makefile
1 # Copyright (C) 2012  W. Trevor King
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 # The variables CC, CXX, and AR are implicit variables.  See
17 #   http://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
18
19 SOURCE = Makefile hello_world.cpp hello_world_string.cpp hello_world_string.h \
20         print_hello_world.cpp print_hello_world.h simple.c
21 NM = nm -Pg
22
23 all: hello_world hello_world-static hello_world-dynamic simple
24
25 # directly link object files using $(CXX)
26 hello_world: hello_world.o print_hello_world.o hello_world_string.o
27         $(CXX) -o hello_world hello_world.o print_hello_world.o hello_world_string.o
28
29 # static linking using $(CXX)
30 hello_world-static: hello_world.o libhello_world.a
31         $(CXX) -static -o hello_world-static hello_world.o -L. -lhello_world
32
33 # dynamic linking using $(CXX)
34 hello_world-dynamic: hello_world.o libhello_world.so
35         $(CXX) -o hello_world-dynamic hello_world.o -L. -lhello_world
36
37
38 # compile using $(CXX)
39 hello_world.o: hello_world.cpp print_hello_world.h
40         $(CXX) -c hello_world.cpp
41 print_hello_world.o: print_hello_world.cpp print_hello_world.h
42         $(CXX) -c print_hello_world.cpp
43 hello_world_string.o: hello_world_string.cpp hello_world_string.h
44         $(CXX) -c hello_world_string.cpp
45
46 # to use them in shared object files, we'll want to compile them with PIC.
47 print_hello_world-PIC.o: print_hello_world.cpp print_hello_world.h
48         $(CXX) -fPIC -c -o print_hello_world-PIC.o print_hello_world.cpp
49 hello_world_string-PIC.o: hello_world_string.cpp hello_world_string.h
50         $(CXX) -fPIC -c -o hello_world_string-PIC.o hello_world_string.cpp
51
52
53 # create a static library with ar
54 libhello_world.a: print_hello_world.o hello_world_string.o
55         ar -cvr libhello_world.a print_hello_world.o hello_world_string.o
56
57 # create a dynamic library with $(CXX)
58 libhello_world.so: print_hello_world-PIC.o hello_world_string-PIC.o
59         $(CXX) -shared -Wl,-soname,libhello_world.so \
60                 -o libhello_world.so \
61                 print_hello_world-PIC.o hello_world_string-PIC.o
62
63
64 # optional stuff
65 # ==============
66
67 run:
68         ./hello_world
69         ./hello_world-static
70         LD_LIBRARY_PATH=. ./hello_world-dynamic
71
72 libs: libhello_world.a libhello_world.so
73
74 inspect-object-files: hello_world.o print_hello_world.o hello_world_string.o
75         $(NM) hello_world.o print_hello_world.o hello_world_string.o
76
77 inspect-executable: hello_world
78         $(NM) hello_world
79
80 inspect-static-library: libhello_world.a
81         $(NM) libhello_world.a
82
83 inspect-shared-libary: libhello_world.so
84         $(NM) --dynamic libhello_world.so
85
86 list-executable-shared-libraries: hello_world
87         ldd hello_world
88
89 # compile simple.c using static linking
90 simple.o: simple.c
91         $(CC) -c simple.c
92 # statically link simple
93 simple: simple.o
94         $(CC) -static -o simple simple.o
95
96 # convert the README to HTML using reStructuredText
97 README.html: README
98         rst2html.py README > README.html
99
100 # distribution
101 linking.tar.gz: $(SOURCE)
102         mkdir linking
103         cp -r $^ linking/
104         tar -czf $@ linking/
105         rm -rf linking/
106
107 # remove temporary files
108 clean:
109         rm -f *.o *.a *.so hello_world hello_world-* simple README.html