How to create a simple make file and compile c++ project in linux?
Rate this article
5.00 out of 5

    Introduction

    Make file is used to compile and link C++ header files, cpp files and associated libraries in Linux platform. 

    Description

     Look at the bellow example.

     Assume that we have two cpp files that includes 2 header files and one Library file, to write a make file for this C++      project, do the following things.

    "Example1.cpp" associated to "example1.h"

     "Example2.cpp" associated to "exaple2.h" and a lib file called "example2LIB"

  • open a text file and name as "makefile" (Normally no extension)
  • open the file in any text editor
  • Wrtite the bellow expression

      g++  -c  example1.h Example1.cpp exaple2.h Example2.cpp -lexample2LIB -o example

  1. g++ - For calling Linux based c++ compiler
  2. -c - Compile the codes
  3. -l - Link to libraries
  4. -o - Final binary out put

  • Save the file.
  • Open Terminal and move to the project path
  • Type make to compile and link the code , If you got any error in code type vi space cpp or .h file name , 
  • Press i and edit the code correctly press ESC then press :wq to save ["q! to exit without save]
  • If any conflict in object files and intermediate files just add one more line in make file to clean up 

      clean: rm -f <Foldername>/*.o

  • To execute the clean command  type "make clean" in terminal

To avoid re-usage of same keywords or commands in make file creation and make some generality to your make file , just use variables like the following way

Syntax: Variable name=<value>

  • Eg:  CC=gcc
  • CPP=g++
  • To retrive the values from variables use "$" sign
  • Eg : $(CPP) -c example.h example.cpp -o example
  • To display a message to screen when making process use @echo
  • Eg: @echo "Cleaning up directory."



Articles
Posted
8
Messages
Posted
23
I am Johnson Augustine [My friends calls me - J.A), Completed my Master of Computer Applications degree, and Currently working as a Software Engineer at KTS Infotech PVT LTD and Server Admin and security Engineer at Scibero Hosting and Cyber Solutions. I have expertise in  C/C++/Visual C++/G++/QT++,Com,Device Driver and embedded System Development. I have 4 Year Experience in ASP.net and Visual studio 2005,2008,2010 and MSSQL Server 2000,2005,2008 ,I have also deep knowledge in Oracle database,MySQL Database,C++ with SQLite, Android mobile development , PHP , Joomla,Word Press ,html,Javascript,Jquery,Ajax.CSS , Networking ,Cyber security, Ethical Hacking and familiar with most of the web hosting control panels like Plesk 9.5,Plesk 10.4.4,Plesk 11.0.9 . Cpanel/WHM,Hsphere,Dotnet panel , and have experience in Windows server 2003,2008,Cent Os , RHEL,Fedora etc .and also have some work experience in cloud environment. 

You can see my professional profile at http://linkedin.com/in/johnsontaugustine

Tektip profile [Popular Technical Discussion Website ] : http://www.tektips.in/AuthorDetails.aspx?AID=technogreenworld@gmail.com

See my personal technical discussion website http://www.memcpy.in

   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments
   Design  HTML

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud