Project

General

Profile

Using CMake » History » Version 8

Charles Brockman, 07/08/2012 10:49 PM
Corrected some grammar, spelling and punctuation.

1 1 Pieter Libin
h1. Using CMake
2
3 4 Anonymous
{{toc}}
4
5 8 Charles Brockman
In this example we are going to see how to use CMake to compile a very simple Wt project where you have a *source* directory with all your files and a *build* directory to generate your application.
6 1 Pieter Libin
7 8 Charles Brockman
If you don't care where your files are located or you are looking for an even simpler example, go to [[Frequently_Asked_Questions#Q-How-do-I-build-my-newly-written-Hello-World-application]].
8 1 Pieter Libin
9
10
h4. Directory Structure 
11
12 8 Charles Brockman
Before starting, make sure that your project main directory (In this example the main directory is called main) looks like this:
13 1 Pieter Libin
14
<pre>
15
main
16
|
17
+--source..................you have all your files here
18
    |
19
    + -- CMakeLists.txt .. a text file where we will write CMake commands (empty) 
20
+--build.................. build directory (initially, this directory is empty)
21
+--CMakeLists.txt ........ a text file where we will write CMake commands (empty)
22
</pre>
23
24
25
h4. CMakeLists.txt in the main directory 
26
27 8 Charles Brockman
These are the contents of the CMakeLists.txt file that you should write on the main directory:
28 1 Pieter Libin
29
<pre>
30
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
31
32
PROJECT(WT_EXAMPLE)
33
34
SET (WT_CONNECTOR "wtfcgi" CACHE STRING "Connector used (wthttp or wtfcgi)")
35
36
ADD_SUBDIRECTORY(source)
37
</pre>
38
39
* The first two instructions are self descriptive. CMAKE_MINIMUM_REQUIRED checks that you have the indicated version. If you don't include this instruction in your CMakeLists.txt you will be able to continue but you will see a warning message. The second option, PROJECT, simply assigns a name to your CMake project.
40
41 8 Charles Brockman
* The third instruction is a bit more interesting. SET allows you to create variables inside a CMake script. In this case we are creating a variable named WT_CONNECTOR. It will contain a string value that, by default, will be "wtfcgi". The syntax used here is:
42 1 Pieter Libin
43
<pre>
44
SET(VARIABLE_NAME default_value CACHE type "help message")
45
</pre>
46
47 8 Charles Brockman
* The CACHE options allow us to save the value. The next time you run CMake (as you will see later), the variable will contain the last value that you entered.
48 1 Pieter Libin
49
* The last instruction in this CMake script is ADD_SUBDIRECTORY. This instruction will delegate the execution of the script to the subdirectory enclosed in parenthesis. 
50
51
The syntax is:
52
53
<pre> ADD_SUBDIRECTORY(subdir)</pre>
54
55 8 Charles Brockman
Make sure that there is a CMakeLists.txt file in the subdirectory that you are referencing, so that sub CMakeLists.txt will be executed.
56 1 Pieter Libin
57 8 Charles Brockman
In our example, as we have one subdirectory (source), we will have a corresponding CMakeLists.txt file in there.
58 1 Pieter Libin
59
h4. CMakeLists.txt in the source directory 
60
61 8 Charles Brockman
This is an example of the CMakeLists.txt file that goes in the source directory:
62 1 Pieter Libin
63
<pre>
64
SET(WT_PROJECT_SOURCE
65
File1.h
66
File1.cpp
67
File2.h
68
File2.cpp
69
Main.C
70
)
71
72
SET(WT_PROJECT_TARGET wt_project.wt)
73
74
ADD_EXECUTABLE(${WT_PROJECT_TARGET} ${WT_PROJECT_SOURCE})
75
76
TARGET_LINK_LIBRARIES(${WT_PROJECT_TARGET} ${WT_CONNECTOR} wt)
77
78
INCLUDE_DIRECTORIES(/usr/local/include/Wt)
79
</pre>
80
81
Let's break this down:
82
83
h5. Variables in a CMake script 
84
85
WT_PROJECT_SOURCE and WT_PROJECT_TARGET are two variables and you can change the names as you like as long as you make sure that you change them everywhere in your script.
86
87
h5. SET instruction 
88
89 8 Charles Brockman
The CMake SET instruction allows us to associate any number of strings with a CMake variable. The syntax is:
90 1 Pieter Libin
91
<pre>
92
SET(CMAKE_VARIABLE string1 string2 ... stringN)
93
</pre>
94
95 8 Charles Brockman
In our example we have two SET instructions. The first one allows us to associate all our source file names with WT_PROJECT_SOURCE. The second SET instruction associates the string wt_project.wt with WT_PROJECT_TARGET.
96 1 Pieter Libin
97
h5. ADD_EXECUTABLE instruction 
98
99
The ADD_EXECUTABLE instruction configures the executable file you are about to compile. The syntax is:
100
101
<pre>
102
ADD_EXECUTABLE(EXECUTABLE_NAME file1 file2 ... fileN)
103
</pre>
104
105
However given that we have our source list in the variable WT_PROJECT_SOURCE, we can write:
106
107
<pre>
108
ADD_EXECUTABLE(EXECUTABLE_NAME ${WT_PROJECT_SOURCE})
109
</pre>
110
111 8 Charles Brockman
In this case, we see the way CMake variables can be used. Every time that you want to refer to any variable contents, just use the ${} syntax around the variable name.
112 1 Pieter Libin
113
h5. TARGET_LINK_LIBRARIES instruction 
114
115 8 Charles Brockman
The TARGET_LINK_LIBRARIES instruction allows us to link our executable file with the dependent libraries. The syntax is:
116 1 Pieter Libin
<pre>
117
TARGET_LINK_LIBRARIES(EXECUTABLE_NAME lib1 lib2 ... libN)
118
</pre>
119
120 8 Charles Brockman
In our case we need to make sure that the our executable links to Wt (wt library) and one of the connectors (wthttp library or wtfcgi library). In our example, the connector name is stored in the variable WT_CONNECTOR.
121 1 Pieter Libin
122
h5. INCLUDE_DIRECTORIES instruction 
123
124
The INCLUDE_DIRECTORIES instruction adds the given directories to those searched by the compiler for include files. The syntax is:
125
<pre>
126
INCLUDE_DIRECTORIES(dir1 dir2 ... dirN)
127
</pre>
128
129
For the sake of the example, I have added the directory /usr/local/include/Wt. This is where all the Wt header files live in my Ubuntu installation. However this might not be the case for you and you might want to decipher where these files are in your system. The general solution for this is to use 
130
131
TODO: Talk about FIND_PACKAGE
132
133
h4. Project files/Makefiles generation 
134
135 8 Charles Brockman
Once you are finished editing the source/CMakeLists.txt file, go to the build directory and type:
136 1 Pieter Libin
<pre>cmake ..</pre>
137
138 8 Charles Brockman
You will see the initial CMake screen on your terminal console:
139 1 Pieter Libin
140
<pre>
141 4 Anonymous
<pre>
142 1 Pieter Libin
                                                     Page 0 of 1
143
 EMPTY CACHE
144 4 Anonymous
</pre>
145 1 Pieter Libin
146
147
EMPTY CACHE:
148
Press [enter] to edit option                         CMake Version 2.6 - patch 4
149
Press [c] to configure
150
Press [h] for help         Press [q] to quit without generating
151
Press [t] to toggle advanced mode (Currently Off)
152
</pre>
153
154
Here you need to run the configuration option by pressing *[c]*
155
156
This will take you to the next screen:
157
158
<pre>
159 4 Anonymous
<pre>
160 1 Pieter Libin
                                                     Page 1 of 1
161
 CMAKE_BUILD_TYPE                                                         
162
 CMAKE_INSTALL_PREFIX             /usr/local                                   
163
 WT_CONNECTOR                     wtfcgi                                       
164 4 Anonymous
</pre>
165 1 Pieter Libin
166 4 Anonymous
CMAKE_BUILD_TYPE: Choose the type of build, options are: None(CMAKE_CXX_FLAGS or
167 1 Pieter Libin
Press [enter] to edit option                         CMake Version 2.6 - patch 4
168
Press [c] to configure
169
Press [h] for help         Press [q] to quit without generating
170
Press [t] to toggle advanced mode (Currently Off)
171
</pre>
172
173 8 Charles Brockman
At this point, define the type of build (CMAKE_BUILD_TYPE) you are going to do. Options are:
174 1 Pieter Libin
175
* Debug
176
* Release
177
* RelWithDebInfo
178
* MinSizeRel
179
180
Next we see that the variable WT_CONNECTOR that was defined in the main directory CMakeLists.txt file appears. As stated before, options for this variable are:
181
182
* wthttp (Wt connector)
183
* wtfcgi (FastCGI connector)
184
185 8 Charles Brockman
Once you have set these properties (for instance, selecting Debug and wtfcgi respectively), configure again by pressing *c*.  This time an additional option *g* appears: 
186 1 Pieter Libin
187
<pre>
188 4 Anonymous
<pre>
189 1 Pieter Libin
                                                     Page 1 of 1
190
 CMAKE_BUILD_TYPE                 Debug                                        
191
 CMAKE_INSTALL_PREFIX             /usr/local                                   
192
 WT_CONNECTOR                     wtfcgi                                       
193 4 Anonymous
</pre>
194
195 1 Pieter Libin
CMAKE_BUILD_TYPE: Choose the type of build, options are: None(CMAKE_CXX_FLAGS or
196
Press [enter] to edit option                         CMake Version 2.6 - patch 4
197
Press [c] to configure     Press [g] to generate and exit
198
Press [h] for help         Press [q] to quit without generating
199
Press [t] to toggle advanced mode (Currently Off)
200
</pre>
201
202 8 Charles Brockman
At this point you press [g] to generate your project. This step takes care of the files that need to be generated in order to compile your project on your platform. For example, if you were working on Visual Studio under Windows, then the project and solution files would be generated. On Linux, you will see that there is a set of Makefiles that you will use to compile your project.
203 1 Pieter Libin
204
After pressing [g] you will end up in the build directory again. This time we just do a regular *make*. The output should be something similar to this:
205
cg
206
<pre>
207
make
208
[ 50%] Building CXX object src/CMakeFiles/App.wt.dir/App.cpp.o
209
[100%] Building CXX object src/CMakeFiles/App.wt.dir/Main.C.o
210
Linking CXX executable App.wt
211
[100%] Built target App.wt
212
</pre>
213
214 8 Charles Brockman
Now your application is ready to go. To execute your application, follow the guidelines for the case where you are using the built-in Wt server or any FastCGI supporting web server.
215 1 Pieter Libin
216 8 Charles Brockman
h4. Summary
217 1 Pieter Libin
218
Well that is basically it. I hope this had been helpful to understand a little bit more what CMake is about. As this is the first version of this document please feel free to make any comments, suggestion, or corrections.
219
220
If you want to learn more about the different instructions that CMake offers I advise you to visit:
221
222
* Pau Garcia's CMake slides (http://www.elpauer.org/stuff/learning_cmake.pdf)
223
* The CMake documentation webpage (http://www.cmake.org/cmake/help/cmake2.6docs.html)
224 6 Roman Tabulov
* CMake with Eclipse (http://www.cmake.org/Wiki/CMake:Eclipse_UNIX_Tutorial)