Project

General

Profile

Using CMake » History » Version 7

Maarten Mulders, 11/27/2011 12:38 PM
Removed unneeded <pre>

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