Project

General

Profile

Using CMake » History » Version 3

Pieter Libin, 10/29/2009 02:31 PM

1 1 Pieter Libin
h1. Using CMake
2
3
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
4
and a *build* directory to generate your application.
5
6
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_.22Hello_World.21.22_application.3F|here]].
7
8
9
h4. Directory Structure 
10
11
Before starting make sure that your project main directory (In this example the main directory is called main) looks like this:
12
13
<pre>
14
main
15
|
16
+--source..................you have all your files here
17
    |
18
    + -- CMakeLists.txt .. a text file where we will write CMake commands (empty) 
19
+--build.................. build directory (initially, this directory is empty)
20
+--CMakeLists.txt ........ a text file where we will write CMake commands (empty)
21
</pre>
22
23
24
h4. CMakeLists.txt in the main directory 
25
26
These are the contents of the CMakeLists.txt file that you should write on the main directory
27
28
<pre>
29
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
30
31
PROJECT(WT_EXAMPLE)
32
33
SET (WT_CONNECTOR "wtfcgi" CACHE STRING "Connector used (wthttp or wtfcgi)")
34
35
ADD_SUBDIRECTORY(source)
36
</pre>
37
38
* 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.
39
40
* 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:
41
42
<pre>
43
SET(VARIABLE_NAME default_value CACHE type "help message")
44
</pre>
45
46
* 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.
47
48
* 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. 
49
50
The syntax is:
51
52
<pre> ADD_SUBDIRECTORY(subdir)</pre>
53
54
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.
55
56
In our example as we have one subdirectory (source) we will have a correspondant CMakeLists.txt file in there.
57
58
59
h4. CMakeLists.txt in the source directory 
60
61
This is an example of the CMakeLists.txt file that goes in the source directory
62
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
84
h5. Variables in a CMake script 
85
86
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.
87
88
89
h5. SET instruction 
90
91
The CMake SET instruction allows to associate any number of strings with a CMake variable. The syntax is:
92
93
<pre>
94
SET(CMAKE_VARIABLE string1 string2 ... stringN)
95
</pre>
96
97
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.
98
99
100
h5. ADD_EXECUTABLE instruction 
101
102
The ADD_EXECUTABLE instruction configures the executable file you are about to compile. The syntax is:
103
104
<pre>
105
ADD_EXECUTABLE(EXECUTABLE_NAME file1 file2 ... fileN)
106
</pre>
107
108
However given that we have our source list in the variable WT_PROJECT_SOURCE, we can write:
109
110
<pre>
111
ADD_EXECUTABLE(EXECUTABLE_NAME ${WT_PROJECT_SOURCE})
112
</pre>
113
114
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.
115
116
117
h5. TARGET_LINK_LIBRARIES instruction 
118
119
The TARGET_LINK_LIBRARIES instruction allows to link our executable file with the depending libraries. The syntax is:
120
<pre>
121
TARGET_LINK_LIBRARIES(EXECUTABLE_NAME lib1 lib2 ... libN)
122
</pre>
123
124
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.
125
126
127
h5. INCLUDE_DIRECTORIES instruction 
128
129
The INCLUDE_DIRECTORIES instruction adds the given directories to those searched by the compiler for include files. The syntax is:
130
<pre>
131
INCLUDE_DIRECTORIES(dir1 dir2 ... dirN)
132
</pre>
133
134
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 
135
136
TODO: Talk about FIND_PACKAGE
137
138
139
h4. Project files/Makefiles generation 
140
141
Once you are finished editing the source/CMakeLists.txt file you should go to the build directory and type:
142
<pre>cmake ..</pre>
143
144
145
You will see the initial CCMake screen on your terminal console:
146
147
<pre>
148
                                                     Page 0 of 1
149
 EMPTY CACHE
150
151
152
EMPTY CACHE:
153
Press [enter] to edit option                         CMake Version 2.6 - patch 4
154
Press [c] to configure
155
Press [h] for help         Press [q] to quit without generating
156
Press [t] to toggle advanced mode (Currently Off)
157
</pre>
158
159
Here you need to run the configuration option by pressing *[c]*
160
161
This will take you to the next screen:
162
163
<pre>
164
                                                     Page 1 of 1
165
 CMAKE_BUILD_TYPE                                                         
166
 CMAKE_INSTALL_PREFIX             /usr/local                                   
167
 WT_CONNECTOR                     wtfcgi                                       
168
169
CMAKE_BUILD_TYPE: Choose the type of build, options are: None(CMAKE_CXX_FLAGS or
170
Press [enter] to edit option                         CMake Version 2.6 - patch 4
171
Press [c] to configure
172
Press [h] for help         Press [q] to quit without generating
173
Press [t] to toggle advanced mode (Currently Off)
174
</pre>
175
176
At this point, you need to define the type of build (CMAKE_BUILD_TYPE) you are going to do. Options are:
177
178
* Debug
179
* Release
180
* RelWithDebInfo
181
* MinSizeRel
182
183
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:
184
185
* wthttp (Wt connector)
186
* wtfcgi (FastCGI connector)
187
188
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: 
189
190
<pre>
191
                                                     Page 1 of 1
192
 CMAKE_BUILD_TYPE                 Debug                                        
193
 CMAKE_INSTALL_PREFIX             /usr/local                                   
194
 WT_CONNECTOR                     wtfcgi                                       
195
196
197
CMAKE_BUILD_TYPE: Choose the type of build, options are: None(CMAKE_CXX_FLAGS or
198
Press [enter] to edit option                         CMake Version 2.6 - patch 4
199
Press [c] to configure     Press [g] to generate and exit
200
Press [h] for help         Press [q] to quit without generating
201
Press [t] to toggle advanced mode (Currently Off)
202
</pre>
203
204
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.
205
206
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:
207
cg
208
<pre>
209
make
210
[ 50%] Building CXX object src/CMakeFiles/App.wt.dir/App.cpp.o
211
[100%] Building CXX object src/CMakeFiles/App.wt.dir/Main.C.o
212
Linking CXX executable App.wt
213
[100%] Built target App.wt
214
</pre>
215
216
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.
217
218
219
h4. Summary 
220
221
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.
222
223
If you want to learn more about the different instructions that CMake offers I advise you to visit:
224
225
* Pau Garcia's CMake slides (http://www.elpauer.org/stuff/learning_cmake.pdf)
226
* The CMake documentation webpage (http://www.cmake.org/cmake/help/cmake2.6docs.html)