]> vaikene.ee Git - evaf/blob - www/pswgen05.html
Written more 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>The next line adds the <tt>PSWGEN_GENERATOR_LIBRARY</tt> definition to the compiler (remember the <tt>lib.h</tt> file and
38 the <tt>PSWGEN_GENERATOR_EXPORT</tt> macro in the <tt>igeneraror.h</tt> file?):
39
40 <pre class="hl"><span class="hl kwa">add_definitions</span><span class="hl opt">(</span>-DPSWGEN_GENERATOR_LIBRARY<span class="hl opt">)</span></pre>
41
42 <p>Add all the eVaf include directories to the compiler. The variable <tt>eVaf_INCLUDE</tt> contains all the eVaf include
43 directories and is already initialized with proper values when this <tt>CMakeLists.txt</tt> file is processed.</p>
44
45 <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>
46
47 <p>Then we initialize a variable with the names of all the eVaf modules that this module needs to be linked with. We only
48 need to specify the names of libraries without prefixes or suffixes like ".dll" or ".so". These libraries also become dependencies
49 of this module and will be built whenever we build this module.</p>
50
51 <pre class="hl"><span class="hl kwa">set</span><span class="hl opt">(</span>eVaf_LIBRARIES CommonLib PluginsLib<span class="hl opt">)</span></pre>
52
53 <p>Collect all the source files that needs to be compiled:</p>
54
55 <pre class="hl"><span class="hl kwa">set</span><span class="hl opt">(</span>SRCS
56 module.cpp
57 <span class="hl opt">)</span></pre>
58
59 <p>Collect header files that need to be processed with the Qt meta-object compiler. Any header file that contains
60 class declarations with the Q_OBJECT keyword and/or signals and slots, needs to be included here. To avoid warnings
61 during the build, do not include here any other header files.</p>
62
63 <pre class="hl"><span class="hl kwa">set</span><span class="hl opt">(</span>MOC_HDRS
64 igenerator.h
65 module.h
66 <span class="hl opt">)</span></pre>
67
68 <p>The following line adds the Windows version info resource file to the list of source files:</p>
69 <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>
70 <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>
71 <span class="hl kwa">endif</span><span class="hl opt">(</span><span class="hl kwb">WIN32</span><span class="hl opt">)</span></pre>
72
73 <p>Process specified header files with the Qt meta-object compiler:</p>
74
75 <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>
76
77 <p>Put it all together and compile the module:</p>
78
79 <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>
80
81 <p>Finally, link the module:</p>
82
83 <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>
84
85 <p>And the final <tt>CMakeLists.txt</tt> file looks the following:</p>
86
87 <pre class="hl"><span class="hl com"># src/apps/PswGen/Generator/CMakeLists.txt</span>
88
89 <span class="hl com"># Name of the target</span>
90 <span class="hl kwa">set</span><span class="hl opt">(</span>TARGET PswGen<span class="hl opt">)</span>
91
92 <span class="hl com"># Qt modules</span>
93 <span class="hl kwa">set</span><span class="hl opt">(</span>QT_DONT_USE_QTGUI TRUE<span class="hl opt">)</span>
94 <span class="hl kwa">include</span><span class="hl opt">(</span><span class="hl kwd">${QT_USE_FILE}</span><span class="hl opt">)</span>
95
96 <span class="hl com"># Needed for exporting symbols from this library</span>
97 <span class="hl kwa">add_definitions</span><span class="hl opt">(</span>-DPSWGEN_GENERATOR_LIBRARY<span class="hl opt">)</span>
98
99 <span class="hl com"># Include directories</span>
100 <span class="hl kwa">include_directories</span><span class="hl opt">(</span><span class="hl kwd">${eVaf_INCLUDE}<span class="hl opt">)</span>
101
102 <span class="hl com"># Required eVaf libraries</span>
103 <span class="hl kwa">set</span><span class="hl opt">(</span>eVaf_LIBRARIES CommonLib PluginsLib<span class="hl opt">)</span>
104
105 <span class="hl com"># Source files</span>
106 <span class="hl kwa">set</span><span class="hl opt">(</span>MOC_HDRS
107 igenerator.h
108 module.h
109 <span class="hl opt">)</span>
110
111 <span class="hl com"># Header files for the Qt meta-object compiler</span>
112 <span class="hl kwa">set</span><span class="hl opt">(</span>MOC_HDRS
113 igenerator.h
114 module.h
115 <span class="hl opt">)</span>
116
117 <span class="hl com"># Version info resource file for Windows builds</span>
118 <span class="hl kwa">if</span><span class="hl opt">(</span><span class="hl kwb">WIN32</span><span class="hl opt">)</span>
119 <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>
120 <span class="hl kwa">endif</span><span class="hl opt">(</span><span class="hl kwb">WIN32</span><span class="hl opt">)</span>
121
122 <span class="hl com"># Run the Qt meta-object compiler</span>
123 <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>
124
125 <span class="hl com"># Compile the module</span>
126 <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>
127
128 <span class="hl com"># Link the module</span>
129 <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>
130
131 <p>We also need <tt>CMakeLists.txt</tt> files in parent directory <tt>src/apps/PswGen</tt>.
132 In this file we add the <tt>PswGen</tt> directory to the list of include directories, which makes it possible to use
133
134 <pre class="hl"><span class="hl com"># src/apps/PswGen/CMakeLists.txt</span>
135 <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>
136 <span class="hl kwa">add_subdirectory</span><span class="hl opt">(</span>Generator<span class="hl opt">)</span></pre>
137
138 <p>Modify the <tt>CMakeLists.txt</tt> file in the <tt>src/apps</tt> directory and include the <tt>PswGen</tt> application:</p>
139
140 <pre class="hl"><span class="hl com"># src/apps/CMakeLists.txt</span>
141 <span class="hl com"># ...</span>
142 <span class="hl kwa">add_subdirectory</span><span class="hl opt">(</span>PswGen<span class="hl opt">)</span></pre>
143
144 <h3>Building the module</h3>
145
146 <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
147 a build directory:</p>
148
149 <pre>evaf $ <code>mkdir build</code>
150 evaf $ <code>cd build</code></pre>
151
152 <p>In the build directory, run <tt>cmake</tt>:</p>
153
154 <pre>evaf/build $ <code>cmake ..</code></pre>
155
156 <p>If <tt>cmake</tt> finishes without errors, build the module with the <tt>make</tt> command:</p>
157
158 <pre>evaf/build $ <code>make PswGen</code></pre>
159
160 <p>If you get compiler errors during the build, fix them. If the build finishes without errors, check the content of the
161 <tt>bin</tt> directory:</p>
162
163 <pre>evaf/build $ <code>ls bin</code>
164 libCommonLib.so* libPluginsLib.so* libPswGen.so*
165 evaf/build $</pre>
166
167 <p>As you can see, there are three libraries now. The <tt>libPswGen.so</tt> is our module and others are eVaf libraries
168 that our module needs in order to be run.</p>
169
170 <p>In the next section <a href="pswgen06.html">06 - Storage Module</a> we write the Storage module.
171
172 </body>
173 </html>