summaryrefslogtreecommitdiff
path: root/docs/tutorials/005/page06.html
blob: 95a341cf0eafc8fb4c03c544c766bbe9805c6a81 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (X11; I; Linux 2.0.32 i486) [Netscape]">
   <META NAME="Author" CONTENT="Billy Quinn">
   <META NAME="Description" CONTENT="A first step towards using ACE productively">
   <TITLE>ACE Tutorial 005</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000FFF" VLINK="#FF0F0F">

<CENTER><B><FONT SIZE=+2>ACE Tutorial 005</FONT></B></CENTER>

<CENTER><B><FONT SIZE=+2>On the road to a multithreaded server</FONT></B></CENTER>


<P>
<HR WIDTH="100%">

<P>Before we go, I wanted you to see the <A HREF="Makefile">Makefile</A>.

<P>
<HR WIDTH="100%">
<BR>&nbsp;
<UL><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>
<BR><FONT FACE="Arial,Helvetica">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
$Id$</FONT>
<BR><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>

<P><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>
<BR><FONT FACE="Arial,Helvetica">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Local macros</FONT>
<BR><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>

<P><FONT FACE="Arial,Helvetica"># You can generally find a Makefile in
the ACE examples, tests or the library</FONT>
<BR><FONT FACE="Arial,Helvetica"># itself that will satisfy our application
needs.&nbsp; This one was taken from</FONT>
<BR><FONT FACE="Arial,Helvetica"># one of the examples.</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# Define the name of the binary we want to create.&nbsp; There has to be</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# a CPP file $(BIN).cpp but it doesn't necessarily have to have your</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# main() in it.&nbsp; Most of the time, though, it will.</FONT>
<BR><FONT FACE="Arial,Helvetica">BIN&nbsp;&nbsp;&nbsp; = server</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# Few applications will have a single source file.&nbsp; We use the FILES</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# macro to build up a list of additional files to compile.&nbsp; Notice</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# that we leave off the extension just as with BIN</FONT>
<BR><FONT FACE="Arial,Helvetica">FILES&nbsp; =</FONT>
<BR><FONT FACE="Arial,Helvetica">FILES += client_handler</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# The BUILD macro is used by the ACE makefiles.&nbsp; Basically, it tells</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# the system what to build.&nbsp; I don't really know what VBIN is other</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# than it is constructed from the value of BIN.&nbsp; Just go with it...</FONT>
<BR><FONT FACE="Arial,Helvetica">BUILD&nbsp;&nbsp; = $(VBIN)</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# Here we use some GNU make extensions to build the SRC macro. Basically,</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# we're just adding .cpp to the value of BIN and for each entry of the</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# FILES macro.</FONT>
<BR><FONT FACE="Arial,Helvetica">SRC = $(addsuffix .cpp,$(BIN)) $(addsuffix
.cpp,$(FILES))</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# This is used by my Indent target below.&nbsp; It's not a part of standard</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# ACE and you don't need it yourself.</FONT>
<BR><FONT FACE="Arial,Helvetica">HDR = *.h</FONT>

<P><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>
<BR><FONT FACE="Arial,Helvetica">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Include macros and targets</FONT>
<BR><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# This is where the real power lies!&nbsp; These included makefile components</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# are similar to the C++ templates in ACE.&nbsp; That is, they do a tremendous</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# amount of work for you and all you have to do is include them.</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# As a matter of fact, in our project, I created a single file named</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# "app.mk" that includes all of these.&nbsp; Our project makefiles then
just</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# need to include app.mk to get everything they need.</FONT>

<P><FONT FACE="Arial,Helvetica">include $(ACE_ROOT)/include/makeinclude/wrapper_macros.GNU</FONT>
<BR><FONT FACE="Arial,Helvetica">include $(ACE_ROOT)/include/makeinclude/macros.GNU</FONT>
<BR><FONT FACE="Arial,Helvetica">include $(ACE_ROOT)/include/makeinclude/rules.common.GNU</FONT>
<BR><FONT FACE="Arial,Helvetica">include $(ACE_ROOT)/include/makeinclude/rules.nonested.GNU</FONT>
<BR><FONT FACE="Arial,Helvetica">include $(ACE_ROOT)/include/makeinclude/rules.lib.GNU</FONT>
<BR><FONT FACE="Arial,Helvetica">include $(ACE_ROOT)/include/makeinclude/rules.bin.GNU</FONT>
<BR><FONT FACE="Arial,Helvetica">include $(ACE_ROOT)/include/makeinclude/rules.local.GNU</FONT>

<P><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>
<BR><FONT FACE="Arial,Helvetica">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Local targets</FONT>
<BR><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# Sometimes I like to reformat my code to make it more readable.&nbsp;
This is</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# more useful for the comments than anything else.&nbsp; Unfortunately,
the</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# "indent" program doesn't quite grok C++ so I have to post-process it's</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# output just a bit.</FONT>
<BR><FONT FACE="Arial,Helvetica">Indent : #</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
for i in $(SRC) $(HDR) ; do \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
indent -npsl -l80 -fca -fc1 -cli0 -cdb &lt; $$i | \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
sed -e 's/: :/::/g'&nbsp; \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-e 's/^.*\(public:\)/\1/' \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-e 's/^.*\(protected:\)/\1/' \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-e 's/^.*\(private:\)/\1/' \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-e 's/:\(public\)/ : \1/' \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-e 's/:\(protected\)/ : \1/' \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-e 's/:\(private\)/ : \1/' \</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> $$i~ ;\</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
mv $$i~ $$i ;\</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
done</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# One of the targets in the ACE makefiles is "depend".&nbsp; It will invoke</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# your compiler in a way that will generate a list of dependencies for</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# you.&nbsp; This is a great thing!&nbsp; Unfortunately, it puts all of
that mess</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# directly into the Makefile.&nbsp; I prefer my Makefile to stay clean
and</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# uncluttered.&nbsp; The perl script referenced here pulls the dependency</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# stuff back out of the Makefile and into a file ".depend" which we then</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# include just like the makefile components above.</FONT>
<BR><FONT FACE="Arial,Helvetica">Depend : depend</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
perl fix.Makefile</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;</FONT>
<BR><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>
<BR><FONT FACE="Arial,Helvetica">#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Dependencies</FONT>
<BR><FONT FACE="Arial,Helvetica">#----------------------------------------------------------------------------</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# Don't put anything below here.&nbsp; Between the "depend" target and
fix.Makefile</FONT>
<BR><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# it's guaranteed to be lost!</FONT>

<P><FONT FACE="Arial,Helvetica">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
# This is inserted by the fix.Makefile script</FONT>
<BR><FONT FACE="Arial,Helvetica">include .depend</FONT></UL>

<HR WIDTH="100%">

<P>Remember, make wants to see tabs leading all of the directives.&nbsp;
If you do a cut/paste job you'll need to convert all leading spaces to
tabs or make will be very unhappy with you.

<P>
<HR WIDTH="100%">
<CENTER>[<A HREF="..">Tutorial
Index</A>] [<A HREF="page07.html">Continue This Tutorial</A>]</CENTER>

</BODY>
</HTML>