1 Getting started with CMake
3 (These instructions apply to Unix-like systems, including Cygwin and Mac. To
4 build using Visual Studio or some other IDE supported by CMake, most of the
5 information below still applies)
7 Always compile in a separate directory to the code. For example, if the
8 code (eg, from Git) is at /home/curt/projects/flightgear, you might create
9 /home/curt/projects/fgbuild. Change into the new directory, and run
13 To generate standard Unix Makefiles in fgbuild.
15 Probably you want to specify an install prefix:
17 cmake ../flightgear -DCMAKE_INSTALL_PREFIX=/usr
19 Note the install prefix is automatically searched for required libraries
20 and header files, so if you install PLIB, OpenSceneGraph and SimGear to the
21 same prefix, most configuration options are unnecessary.
23 To specify that a particular dependency is in a non-standard location, most
24 libraries support an environment variable - eg PLIBDIR or OSG_ROOT - to
25 allow precise selection.
27 By default, we select a release build. To create a debug build, use
29 cmake ../flightgear -DCMAKE_BUILD_TYPE=Debug
31 (or MinSizeRel, or RelWithDbg)
33 Debug builds will automatically use corresponding debug builds of required
34 libraries, if they are available. For example you can install debug builds of
35 SimGear and OpenSceneGraph, and a debug FlightGear build will use them.
37 (Debug builds of libraries have the 'd' suffix by default)
39 Note most IDE projects (eg Xcode and Visual Studio) support building all the
40 build types from the same project, so you can omit the CMAKE_BUILD_TYPE option
41 when running cmake, and simply pick the build configuration as normal in the
44 It's common to have several build directories with different build
47 /home/curt/projects/flightgear (the git clone)
48 /home/curt/projects/fgdebug
49 /home/curt/projects/fgrelease
50 /home/curt/projects/fg-with-svn-osg
52 To set an optional feature, do
54 cmake ../flightgear -DFEATURE_NAME=ON
56 The easiest way to see the features that can be configured, is by running one
57 of the cmake GUIs - either ncurses or graphical. You can also check the root
62 For a Unix makefile build, 'make dist', 'make uninstall' and 'make test' are
63 all available and should work as expected. 'make clean' is also as normal,
64 but there is *no* 'make distclean' target. The equivalent is to completely
65 remove your build directory, and start with a fresh one.
67 Adding new files to the build
69 Add source files to the SOURCES list, and headers to the HEADERS list. Note
70 technically you only need to add source files, but omitting headers confuses
71 project generation and distribution / packaging targets.
73 For target conditional files, you can append to the SOURCES or HEADERS lists
74 inside an if() test, for example:
77 list(APPEND SOURCES extraFile1.cxx extraFile2.cxx)
80 Setting include directories
82 In any CMakeList.txt, you can do the following:
84 include_directories(${PROJECT_SOURCE_DIR}/some/path)
86 For example, this can be done in particular subdirectory, or at the project
87 root, or an intermediate level.
89 Setting target specific compile flags, includes or defines
91 Use set_target_property(), for example
93 set_target_property(fgfs PROPERTIES
94 COMPILE_DEFINITIONS FOO BAR=1)
96 You can set a property on an individual source file:
98 set_property(SOURCE myfile.cxx PROPERTY COMPILE_FLAGS "-Wno-unsigned-compare")
100 Detecting Features / Libraries
102 For most standard libraries (Gtk, wxWidget, Python, GDAL, Qt, libXml, Boost),
103 cmake provides a standard helper. Check your cmake 'modules' directory to see
106 In the root CMakeLists file, use a statement like:
108 find_package(OpenGL REQUIRED)
110 Each package helper sets various variables such aaa_FOUND, aaa_INCLUDE_DIR,
111 and aaa_LIBRARY. Depending on the complexity of the package, these variables
112 might have different names (eg, OPENSCENEGRAPH_LIBRARIES).
114 If there's no standard helper for a library you need, find a similar one, copy
115 it to CMakeModules/FindABC.cmake, and modify the code to fit. Generally this
116 is pretty straightforward.
118 Note libraries support by pkg-config can be handled directly, with no need
119 to create a custom FindABC helper.
121 Adding a new executable target
123 add_executable(myexecutable ${SOURCES} ${HEADERS})
124 target_link_libraries(myexecutable .... libraries ... )
125 install(TARGETS myexecutable RUNTIME DESTINATION bin)
127 (If the executable should not be installed, omit the final line above)
129 If you add an additional line
131 add_test(testname ${EXECUTABLE_OUTPUT_PATH}/myexecutable)
133 Then running 'make test' will run your executable as a unit test. The
134 executable should return either a success or failure result code.