My purpose is creating a program that can be compiled for Linux, OS X and Windows. If you have read older posts you already know that I was successful and I am already able to do it.
After some time spent developing a program for all these operating system it becomes obvious that handling the build files is a great problem. I have the Visual Studio project file for Windows, the KDevelop files for Linux and the XCode project for OS X. Maintaining all these files takes time and it is prone to errors. Moreover there are other kind of problems: I am still using Visual Studio 2003: if another developer checks out the source code and opens the project with a newer version of Visual Studio the project cannot be read by my older version any more. I could manually maintains different project files for different VS versions, but this is clearly a crazy thing to do.
Last but not least, creating the files for a new project is difficult: you have to copy the files from another project and carefully edit them.
In short, there must be a better way.
Some time ago I started looking for that way and I found a good solution: CMake. Maybe there are others, but I am very satisfied with this one.
The idea behind CMake is simple: use a single text file (CMakeLists.txt) in each folder to describe how the project must be built. Then let CMake create the build files for your preferred build tool.
You usually create a folder inside your project and instruct CMake to create the build files in that folder: all the build and compiled files will be in that folder, so your source code tree will be clean. You can even create different build folders for different build configurations.
There are plenty on information about CMake basic usage, so I will not write about it. I found little information about advanced usage, and I will post what I discovered with some work and experiments.
Using CMake is simple: just run the GUI program and it will show up its main window.
In the upper part you select your project's root folder (the upper one that contains a CMakeLists.txt file) and the build folder, where the build files will be created.
Below these paths there is grid that contains a list of couples: a variable and its value. You must check the values and set the missing or wrong ones. Here lies the magic: setting those variables tells CMake where to look for libraries or how to configure the build in your computer.
You must run the CMake GUI when you create the build folder and you will run it again when you change something, most often because you have added new source files.
In the lower part of the window there is a Configure button. When you press it CMake processes all the CMakeLists.txt files: if there are problems one or more rows in the grid will be colored in red. You need to fix the problem, for example editing the value of a variable, and press Configure again.
If everything is OK the Generate button is enabled: press it to create your build files, then move to the build folder and open the build files with your development tool.
Handling the project becomes a much easier task. For example, if you create some new source files just add the file names to the CMakeLists.txt file.
Then just run CMake in each operating system and configure again.
To create the build files CMake processes the CMakeLists.txt files found in each folder. Those files are written in the CMake language: the language is very powerful so the difficult part is learning it. I discovered that I used a relatively low number of features, but it took me a lot of work to learn how to use them correctly.
Thursday, December 2, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment