Cmake vs sample code?

I was wondering if there is any sample code for Makefile ( make ) and CMakeLists.txt ( cmake ) that do the same thing (the only difference is that it is written in make and the other in cmake ).

I tried looking for "cmake vs make" but never found any code comparisons. It would be very helpful to understand the differences, even if it were a simple case.

+86
cmake makefile
Jun 04 2018-12-12T00:
source share
2 answers

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:

 #Makefile CC = gcc CPP = g++ RANLIB = ar rcs RELEASE = -c -O3 DEBUG = -c -g -D_DEBUG INCDIR = -I./stuff/include LIBDIR = -L./stuff/lib -L. LIBS = -lstuff -lmystatlib -lmydynlib CFLAGS = $(RELEASE) PROGOBJS = prog1.o prog2.o prog3.o prog: main.o $(PROGOBJS) mystatlib mydynlib $(CC) main.o $(PROGOBJS) $(LIBDIR) $(LIBS) -o prog debug: CFLAGS=$(DEBUG) debug: prog mystatlib: mystatlib.o $(RANLIB) libmystatlib.a mystatlib.o mydynlib: mydynlib.o $(CPP) -shared mydynlib.o -o libmydynlib.so %.o: %.c $(CC) $(CFLAGS) $(INCDIR) $< -o $@ %.o: %.cpp $(CPP) $(CFLAGS) $(INCDIR) -fPIC $< -o $@ 

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 .

+91
Oct 09 '13 at 8:06 on
source share

Grab some software that uses CMake as its builds system (many open source projects are presented as an example). Get the source code and configure it with CMake. Read the received files and enjoy.

Keep in mind that these tools do not compare with each other. The most obvious difference is that CMake scans the dependencies between different files (for example, the C header and source files), while make leaves this to the file authors.

+5
Jun 11 2018-12-12T00:
source share



All Articles