Monday, March 23, 2015

Installation of CMake and using cmake for simple C program for creating of makefile

Installation of makefile and  using cmake for simple C program for  creating of makefile

Makefile is a file which tells how to compile and link a program. The instruction of compiling a source file , including library file and linking the file to produce the final binary file is written in Makefile. In Linux based system , If we are using an IDE then makefile is created by IDE. However if we are not using IDE and simply compiling and linking through command line, in such case we need to create MakeFile. CMake is a tool used to easy generation of cross platform Makefile.


here I have presented the use of Cmake to create a MakeFile in simple ways. The following procedure are tested in Linux/Mac OS.

1) Installation of CMake:

first install CMake. It is available in all the platforms. In mac, linux Cmake-gui is also available with GUI feature available. Be sure that command line feature is also installed along with GUI in linux and Mac OS.

Just type cmake to ensure that cmake command line is available in linux and mac.


2) creation of simple C project file  named cmake.c

 first let us create a simple C file as
#include <stdio.h>



int main()


printf("this  is the test file");


2) create CMakeLists.txt along with source file

After creation of source file , next we need a CMakeLists.txt file. This file is used by the Cmake to create a Makefile.

cmake_minimum_required (VERSION 2.6)

set(TARGET learn)

set(SOURCE cmake.c)



add_executable(${TARGET} ${SOURCE})

message("Success")




here we set learn as TARGET and cmake.c as SOURCE  and we want our makefile to create a executable file named as learn .

3) Generation of Makefile

first create a new folder and name it "build" so that all the file generated by Cmake are placed here. This new folder is not a compulsary one but is created to manage the files.

This makefile can be generated in two ways . Either by cmake-gui or command line

a) using GUI

 first open the gui of cmake . then there is input fields as "where is the source code"
 and where ti build the binaries. Just browse to the source code folder in the first input field and browse to the recently created build folder in the later input box.

 then finally press the Configure and Generate button of the GUI.

 then after this makefile is created in the build directory. make sure to see it.




 b) using command line

 first move the the build directory.
 then  input the command
   cmake ..

   this will create the makefile in the build folder.



4) running of MakeFile recently created

   then move to the folder build as MakeFile created is placed here.

   run the command

    make

    this will create the binary file learn as we have named our binary to be learn in earlier CMakeListes.txt at the start.

    5) so run the executable as

      finally move to the folder inside build where learn executables is created and run the command

      ./learn

      wow we get the output.




No comments:

Post a Comment