Project

General

Profile

Using CMake » History » Version 9

Charles Brockman, 07/11/2012 04:41 AM
Corrected heading hierarchy. (h4 > h2 and h5 > h3)

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