2013-06-09 00:00:00 -0700
A good plan violently executed now is better than a perfect plan executed next week.
George S. Patton
While being able to read Makefiles is an important skill, using Make directly is probably not the most efficient use of time. Automation tools such as Ant, autotools, scons, and CMake provide another layer of abstraction over the build process. This layer of abstraction provides easier to use/read api’s, dependency checking, as well as cross platform and cross compilation support.
CMake has a slight learning curve to it, and some rather dense documentation. Here’s a quicksart guide for getting up and running with a basic CMakeLists.txt (config) file for a simple C project.
here’s a basic c project skeleton with cmake
for a trivial project, two lines is all you need
project (HelloWorld) add_executable(HelloWorld helloworld.c)next, run the following in the root of your project
$ cmake .this will generate a makefile build system a simple project. Afterward, running ‘make’ will build your project
specify an additional directory for the compiler to search for include files
include_directories("include")
specify an additional directory for the linker to serach for libraries
link_directories("lib")
adds a library to the compilation queue of this project
add_library(submoduleA asubmodule.c)much like add_executable, will generate object files for submoduleA. CMake works out source dependencies and linkage relationships.
target_link_libraries(HelloWorld asubmodule)
linking against both external and iternal libraries is the same, it just needs to be on the linkers search path.
generally, a single CMakeLists.txt specifies for a single executable. You can specify a subdirectory with:
add_subdirectory(asubmodule)
this example project shows all you need to setup a basic project
specify the minimum version of cmake required
cmake_minimum_required (VERSION 2.6)
we can set arguments to specific compilers with:
SET(GCC_LINK_FLAGS "-std=c11 -Wall") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GCC_LINK_FLAGS}")the fist SET declares a new variable “GCC_LINK_FLAGS” and initializes it with our string of flags we want to send to the linker
the second SET modifies the cmake environment variable CMAKE_C_FLAGS, by concatenating it and our custom flags