1 # Copyright (C) 2012 W. Trevor King
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.
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.
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/>.
16 # The variables CC, CXX, and AR are implicit variables. See
17 # http://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
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
23 all: hello_world hello_world-static hello_world-dynamic simple
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
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
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
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
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
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
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
70 LD_LIBRARY_PATH=. ./hello_world-dynamic
72 libs: libhello_world.a libhello_world.so
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
77 inspect-executable: hello_world
80 inspect-static-library: libhello_world.a
81 $(NM) libhello_world.a
83 inspect-shared-libary: libhello_world.so
84 $(NM) --dynamic libhello_world.so
86 list-executable-shared-libraries: hello_world
89 # compile simple.c using static linking
92 # statically link simple
94 $(CC) -static -o simple simple.o
96 # convert the README to HTML using reStructuredText
98 rst2html.py README > README.html
101 linking.tar.gz: $(SOURCE)
107 # remove temporary files
109 rm -f *.o *.a *.so hello_world hello_world-* simple README.html