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