]> vaikene.ee Git - evaf/blob - www/pswgen05.html
Finalized tutorial files.
[evaf] / www / pswgen05.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html lang="et" xmlns="http://www.w3.org/1999/xhtml" xml:lang="et">
3
4 <head>
5 <meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
6 <title>eVaf Tutorial - 05 - Building Generator Module</title>
7 <meta name="Author" content="Enar Väikene" />
8 <meta name="description" content="eVaf Tutorial" />
9 <meta name="keywords" content="evaf c++ application development framework tutorial password generator" />
10 <link rel="StyleSheet" href="evaf.css" type="text/css" media="all" />
11 <link rel="StyleSheet" href="highlight.css" type="text/css" media="all" />
12 </head>
13
14 <body>
15
16 <h1>eVaf Tutorial</h1>
17
18 <h2>05 - Building Generator Module</h2>
19
20 <h3>CMakeLists.txt</h3>
21
22 <p>eVaf uses <a href="http://www.cmake.org">CMake</a> as its build system and needs a file called <tt>CMakeLists.txt</tt>
23 in the <tt>src/apps/PswGen/Generator</tt> directory. Create the file and start editing it.</p>
24
25 <p>We use the <tt>TARGET</tt> variable to set the name of the module. This makes it easier to re-use the <tt>CMakeLists.txt</tt>
26 file in other modules and applications.</p>
27
28 <pre class="hl"><span class="hl kwa">set</span><span class="hl opt">(</span>TARGET PswGen<span class="hl opt">)</span></pre>
29
30 <p>Then we include Qt include files and libraries. We also specify, that we do not want to include the QtGui module as this
31 module does not any classes from the QtGui module. By removing the QtGui module, we remove any graphical libraries as
32 dependencies for this module and it can be used in headless systems.</p>
33
34 <pre class="hl"><span class="hl kwa">set</span><span class="hl opt">(</span>QT_DONT_USE_QTGUI TRUE<span class="hl opt">)</span>
35 <span class="hl kwa">include</span><span class="hl opt">(</span><span class="hl kwd">${QT_USE_FILE}</span><span class="hl opt">)</span></pre>
36
37 <p>Add all the eVaf include directories to the compiler. The variable <tt>eVaf_INCLUDE</tt> contains all the eVaf include
38 directories and is already initialized with proper values when this <tt>CMakeLists.txt</tt> file is processed.</p>
39
40 <pre class="hl"><span class="hl kwa">include_directories</span><span class="hl opt">(</span><span class="hl kwd">${eVaf_INCLUDE}<span class="hl opt">)</span></pre>
41
42 <p>Then we initialize a variable with the names of all the eVaf modules that this module needs to be linked with. We only
43 need to specify the names of libraries without prefixes or suffixes like ".dll" or ".so". These libraries also become dependencies
44 of this module and will be built whenever we build this module.</p>
45
46 <pre class="hl"><span class="hl kwa">set</span><span class="hl opt">(</span>eVaf_LIBRARIES CommonLib PluginsLib<span class="hl opt">)</span></pre>
47
48 <p>Collect all the source files that needs to be compiled:</p>
49
50 <pre class="hl"><span class="hl kwa">set</span><span class="hl opt">(</span>SRCS
51 module.cpp
52 <span class="hl opt">)</span></pre>
53
54 <p>Collect header files that need to be processed with the Qt meta-object compiler. Any header file that contains
55 class declarations with the Q_OBJECT keyword and/or signals and slots, needs to be included here. To avoid warnings
56 during the build, do not include here any other header files.</p>
57
58 <pre class="hl"><span class="hl kwa">set</span><span class="hl opt">(</span>MOC_HDRS
59 module.h
60 <span class="hl opt">)</span></pre>
61
62 <p>The following line adds the Windows version info resource file to the list of source files:</p>
63 <pre class="hl"><span class="hl kwa">if</span><span class="hl opt">(</span><span class="hl kwb">WIN32</span><span class="hl opt">)</span>
64 <span class="hl kwa">set</span><span class="hl opt">(</span>SRCS <span class="hl kwd">${SRCS}</span> version.rc<span class="hl opt">)</span>
65 <span class="hl kwa">endif</span><span class="hl opt">(</span><span class="hl kwb">WIN32</span><span class="hl opt">)</span></pre>
66
67 <p>Process specified header files with the Qt meta-object compiler:</p>
68
69 <pre class="hl"><span class="hl kwd">qt4_wrap_cpp</span><span class="hl opt">(</span>MOC_SRCS <span class="hl kwd">${MOC_HDRS}</span><span class="hl opt">)</span></pre>
70
71 <p>Put it all together and compile the module:</p>
72
73 <pre class="hl"><span class="hl kwa">add_library</span><span class="hl opt">(</span><span class="hl kwd">${TARGET}</span> <span class="hl kwb">SHARED</span> <span class="hl kwd">${SRCS} ${MOC_SRCS}</span><span class="hl opt">)</span></pre>
74
75 <p>Finally, link the module:</p>
76
77 <pre class="hl"><span class="hl kwa">target_link_libraries</span><span class="hl opt">(</span><span class="hl kwd">${TARGET} ${QT_LIBRARIES} ${eVaf_LIBRARIES}</span><span class="hl opt">)</span></pre>
78
79 <p>And the final <tt>CMakeLists.txt</tt> file looks the following:</p>
80
81 <pre class="hl"><span class="hl com"># src/apps/PswGen/Generator/CMakeLists.txt</span>
82
83 <span class="hl com"># Name of the target</span>
84 <span class="hl kwa">set</span><span class="hl opt">(</span>TARGET PswGen<span class="hl opt">)</span>
85
86 <span class="hl com"># Qt modules</span>
87 <span class="hl kwa">set</span><span class="hl opt">(</span>QT_DONT_USE_QTGUI TRUE<span class="hl opt">)</span>
88 <span class="hl kwa">include</span><span class="hl opt">(</span><span class="hl kwd">${QT_USE_FILE}</span><span class="hl opt">)</span>
89
90 <span class="hl com"># Include directories</span>
91 <span class="hl kwa">include_directories</span><span class="hl opt">(</span><span class="hl kwd">${eVaf_INCLUDE}<span class="hl opt">)</span>
92
93 <span class="hl com"># Required eVaf libraries</span>
94 <span class="hl kwa">set</span><span class="hl opt">(</span>eVaf_LIBRARIES CommonLib PluginsLib<span class="hl opt">)</span>
95
96 <span class="hl com"># Source files</span>
97 <span class="hl kwa">set</span><span class="hl opt">(</span>SRCS
98 module.cpp
99 <span class="hl opt">)</span>
100
101 <span class="hl com"># Header files for the Qt meta-object compiler</span>
102 <span class="hl kwa">set</span><span class="hl opt">(</span>MOC_HDRS
103 module.h
104 <span class="hl opt">)</span>
105
106 <span class="hl com"># Version info resource file for Windows builds</span>
107 <span class="hl kwa">if</span><span class="hl opt">(</span><span class="hl kwb">WIN32</span><span class="hl opt">)</span>
108 <span class="hl kwa">set</span><span class="hl opt">(</span>SRCS <span class="hl kwd">${SRCS}</span> version.rc<span class="hl opt">)</span>
109 <span class="hl kwa">endif</span><span class="hl opt">(</span><span class="hl kwb">WIN32</span><span class="hl opt">)</span>
110
111 <span class="hl com"># Run the Qt meta-object compiler</span>
112 <span class="hl kwd">qt4_wrap_cpp</span><span class="hl opt">(</span>MOC_SRCS <span class="hl kwd">${MOC_HDRS}</span><span class="hl opt">)</span>
113
114 <span class="hl com"># Compile the module</span>
115 <span class="hl kwa">add_library</span><span class="hl opt">(</span><span class="hl kwd">${TARGET}</span> <span class="hl kwb">SHARED</span> <span class="hl kwd">${SRCS} ${MOC_SRCS}</span><span class="hl opt">)</span>
116
117 <span class="hl com"># Link the module</span>
118 <span class="hl kwa">target_link_libraries</span><span class="hl opt">(</span><span class="hl kwd">${TARGET} ${QT_LIBRARIES} ${eVaf_LIBRARIES}</span><span class="hl opt">)</span></pre>
119
120 <p>We also need <tt>CMakeLists.txt</tt> files in parent directory <tt>src/apps/PswGen</tt>.
121 In this file we add the <tt>PswGen</tt> directory to the list of include directories, which makes it possible to use
122
123 <pre class="hl"><span class="hl com"># src/apps/PswGen/CMakeLists.txt</span>
124 <span class="hl kwa">set</span><span class="hl opt">(</span>eVaf_INCLUDE <span class="hl kwd">${eVaf_INCLUDE} ${SMAKE_SOURCE_DIR}</span>/src/apps/PswGen<span class="hl opt">)</span>
125 <span class="hl kwa">add_subdirectory</span><span class="hl opt">(</span>Generator<span class="hl opt">)</span></pre>
126
127 <p>Modify the <tt>CMakeLists.txt</tt> file in the <tt>src/apps</tt> directory and include the <tt>PswGen</tt> application:</p>
128
129 <pre class="hl"><span class="hl com"># src/apps/CMakeLists.txt</span>
130 <span class="hl com"># ...</span>
131 <span class="hl kwa">add_subdirectory</span><span class="hl opt">(</span>PswGen<span class="hl opt">)</span></pre>
132
133 <h3>Building the module</h3>
134
135 <p>Now our module is included in the build system and we can try to compile it. Go to the eVaf root directory and create
136 a build directory:</p>
137
138 <pre>evaf $ <code>mkdir build</code>
139 evaf $ <code>cd build</code></pre>
140
141 <p>In the build directory, run <tt>cmake</tt>:</p>
142
143 <pre>evaf/build $ <code>cmake ..</code></pre>
144
145 <p>If <tt>cmake</tt> finishes without errors, build the module with the <tt>make</tt> command:</p>
146
147 <pre>evaf/build $ <code>make PswGen</code></pre>
148
149 <p>If you get compiler errors during the build, fix them. If the build finishes without errors, check the content of the
150 <tt>bin</tt> directory:</p>
151
152 <pre>evaf/build $ <code>ls bin</code>
153 libCommonLib.so* libPluginsLib.so* libPswGen.so*
154 evaf/build $</pre>
155
156 <p>As you can see, there are three libraries now. The <tt>libPswGen.so</tt> is our module and others are eVaf libraries
157 that our module needs in order to be run.</p>
158
159 <p>In the next section <a href="pswgen06.html">06 - Storage Module</a> we write the Storage module.
160
161 </body>
162 </html>