makefile - Cmake vs make sample codes? -
i wondering if there sample code makefiles (make) , cmakelists.txt (cmake) both same thing (the difference being 1 written in make , other in cmake).
i tried looking 'cmake vs make', never found code comparisons. helpful understand differences, if simple case.
the following makefile builds executable named prog sources prog1.c, prog2.c, prog3.c , main.c. prog linked against libmystatlib.a , libmydynlib.so both built source. additionally, prog uses library libstuff.a in stuff/lib , header in stuff/include. makefile default builds release target, offers 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 cmakelists.txtthat (almost) same, comments underline similarities makefile:
#cmakelists.txt cmake_minimum_required(version 2.8) # stuff not directly project(example) # related building include_directories(${cmake_source_dir}/stuff/include) # -i flags compiler link_directories(${cmake_source_dir}/stuff/lib) # -l flags 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 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 linking mydynlib set_target_properties(mydynlib properties position_independent_code true) #alternatively: #set_target_properties(mydynlib properties compile_flags "-fpic") in simple example, important differences are:
cmake recognizes compilers use kind of source. also, invokes right sequence of commands each type of target. therefore, there no explicit specification of commands $(cc)..., $(ranlib)... , on.
all usual compiler/linker flags dealing inclusion of header files, libraries, etc. replaced platform independent / build system independent commands.
debugging flags included either setting variable cmake_build_type "debug", or passing cmake when invoking program:
cmake -dcmake_build_type:string=debug.cmake offers platform independent inclusion of '-fpic' flag (via position_independent_code property) , many others. still, more obscure settings can implemented hand in cmake in makefile (by using
compile_flags, similar properties). of course cmake starts shine when third party libraries (like opengl) included in portable manner.the build process has 1 step if use makefile, namely typing
make@ command line. cmake, there 2 steps: first, need setup build environment (either typingcmake <source_dir>in build directory or running gui client). creates makefile or equivalent, depending on build system of choice (e.g. make on unixes or vc++ or mingw + msys on windows). build system can passed cmake parameter; however, cmake makes reasonable default choices depending on system configuration. second, perform actual build in selected build system.
sources , build instructions available @ https://github.com/rhoelzel/make_cmake.
Comments
Post a Comment