The following Makefile creates an executable file named prog from the sources prog1.c, prog2.c, prog3.c and main.c prog is associated with libmystatlib.a and libmydynlib.so , which are also built from the source. In addition, prog uses the libstuff.a library in stuff/lib and its title in stuff/include . The default Makefile creates a release target, but also offers a debug target:
Here is CMakeLists.txt that does (almost) the same thing, with some comments to emphasize the similarities to the Makefile:
#CMakeLists.txt cmake_minimum_required(VERSION 2.8) # stuff not directly project(example) # related to building include_directories(${CMAKE_SOURCE_DIR}/stuff/include) # -I flags for compiler link_directories(${CMAKE_SOURCE_DIR}/stuff/lib) # -L flags for linker set(PROGSRC prog1.c prog2.c prog3.c) # define variable add_executable(prog main.c ${PROGSRC}) # define executable target prog, specify sources target_link_libraries(prog mystatlib mydynlib stuff) # -l flags for linking prog target add_library(mystatlib STATIC mystatlib.c) # define static library target mystatlib, specify sources add_library(mydynlib SHARED mydynlib.cpp) # define shared library target mydynlib, specify sources #extra flags for linking mydynlib set_target_properties(mydynlib PROPERTIES POSITION_INDEPENDENT_CODE TRUE) #alternatively: #set_target_properties(mydynlib PROPERTIES COMPILE_FLAGS "-fPIC")
In this simple example, the most important differences are:
CMake recognizes which compilers to use for what type of source. In addition, it causes the correct sequence of commands for each type of target. Therefore, there is no explicit specification of commands such as $ (CC) ..., $ (RANLIB) ... etc.
All the usual compiler / linker flags related to including header files, libraries, etc. replaced by independent / platform independent independent teams.
Debugging flags are enabled by setting the CMAKE_BUILD_TYPE variable to "Debug", or passing it to CMake when the program is called: cmake -DCMAKE_BUILD_TYPE:STRING=Debug .
CMake also offers platform-independent inclusion of the -fPIC flag (via the POSITION_INDEPENDENT_CODE property) and many others. However, more obscure settings can be implemented manually in CMake in the same way as in the Makefile (using COMPILE_FLAGS and similar properties). Of course, CMake really begins to shine when third-party libraries (such as OpenGL) are included in a portable manner.
The build process has one step if you use a Makefile, namely: type make at the command line. There are two steps for CMake: first, you need to configure the build environment (either by typing cmake <source_dir> in the build directory, or by running some GUI client). This creates a Makefile or something equivalent, depending on the build system you choose (for example, make on Unixes or VC ++ or MinGW + Msys on Windows). The build system can be passed to CMake as a parameter; however, CMake makes reasonable default options depending on your system configuration. Secondly, you are actually building in the selected build system.
Sources and creation instructions are available at https://github.com/rhoelzel/make_cmake .
Roberto Oct 09 '13 at 8:06 on 2013-10-09 08:06
source share