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">
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" />
16 <p>Next:
<a href=
"pswgen06.html">06 - Storage Module
</a>, Previous:
<a href=
"pswgen04.html">04 - Generator Module
</a></p>
18 <h1>eVaf Tutorial
</h1>
20 <h2>05 - Building Generator Module
</h2>
22 <h3>CMakeLists.txt
</h3>
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>
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>
30 <pre class=
"hl"><span class=
"hl kwa">set
</span><span class=
"hl opt">(
</span>TARGET PswGen
<span class=
"hl opt">)
</span></pre>
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>
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>
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>
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>
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>
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>
50 <p>Collect all the source files that needs to be compiled:
</p>
52 <pre class=
"hl"><span class=
"hl kwa">set
</span><span class=
"hl opt">(
</span>SRCS
54 <span class=
"hl opt">)
</span></pre>
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>
60 <pre class=
"hl"><span class=
"hl kwa">set
</span><span class=
"hl opt">(
</span>MOC_HDRS
62 <span class=
"hl opt">)
</span></pre>
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>
69 <p>Process specified header files with the Qt meta-object compiler:
</p>
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>
73 <p>Put it all together and compile the module:
</p>
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>
77 <p>Finally, link the module:
</p>
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>
81 <p>And the final
<tt>CMakeLists.txt
</tt> file looks the following:
</p>
83 <pre class=
"hl"><span class=
"hl com"># src/apps/PswGen/Generator/CMakeLists.txt
</span>
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>
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>
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>
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>
98 <span class=
"hl com"># Source files
</span>
99 <span class=
"hl kwa">set
</span><span class=
"hl opt">(
</span>SRCS
101 <span class=
"hl opt">)
</span>
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
106 <span class=
"hl opt">)
</span>
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>
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>
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>
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>
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
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>
129 <p>Modify the
<tt>CMakeLists.txt
</tt> file in the
<tt>src/apps
</tt> directory and include the
<tt>PswGen
</tt> application:
</p>
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>
135 <h3>Building the module
</h3>
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>
140 <pre>evaf $
<code>mkdir build
</code>
141 evaf $
<code>cd build
</code></pre>
143 <p>In the build directory, run
<tt>cmake
</tt>:
</p>
145 <pre>evaf/build $
<code>cmake ..
</code></pre>
147 <p>If
<tt>cmake
</tt> finishes without errors, build the module with the
<tt>make
</tt> command:
</p>
149 <pre>evaf/build $
<code>make PswGen
</code></pre>
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>
154 <pre>evaf/build $
<code>ls bin
</code>
155 libCommonLib.so* libPluginsLib.so* libPswGen.so*
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>
161 <p>In the next section
<a href=
"pswgen06.html">06 - Storage Module
</a> we write the Storage module.