Home arrow C and C+ arrow How To Create a Makefile
How To Create a Makefile PDF
Thursday, 24 March 2011
In order to create a Makefile for your C/C++ project it is useful first to understand few principles of how make file works.

To do that, first test your compiling commands from command line, then proceed to create a Makefile.

Example of a Makefile used to build libraries. In the next example application.c is the source code for our library.

Makefile for building a FreeBSD / Linux Library
prefix=/usr/local
    BINDIR=${prefix}/bin
    MANDIR=${prefix}/man
    man1dir=$(MANDIR)/man1
     
    CC=gcc
    CFLAGS=-pipe -Wall -O
    LDFLAGS=-s
     
    all: libapplication.a
     
    libapplication.a: application.o
            @ar r libapplication.a $?
     
    application.o: application.c
            $(CC) application.c -shared -fPIC -c -o application.o
     
    clean:
            rm -f *.o core core.* *.core


Now to make use of the library we will write a short example of a function that reads data from a file:

We will have previous Makefile and also, application.cc (sourcecode for our library), application.h (header file for our library) and our main program that will make use of the library.

application.c
#include "app.h"

char * read_file(const char *filename)
{
  FILE *fp;
  char *buffer;
  size_t result;
  long file_size;

  if((fp = fopen(filename, "rb")) == NULL) {
    printf("\nCannot open file.\n");
    exit(1);
  }

  /* get file size */
  fseek(fp, 0, SEEK_END);
  file_size=ftell(fp);
  rewind(fp);

  /* allocate memory */
  buffer = (char *) malloc (sizeof(char)*file_size);
  if (buffer == NULL) {
    printf("\nMemory error.\n");
    exit(2);
  }

  /* copy the entire file into the buffer */
  result = fread(buffer, 1, file_size, fp);
  if (result != file_size) {
    printf("\nReading error.\n");
    exit(3);
  }

  fclose(fp);
  return buffer;
}


Then the header file is:

application.h
#include <stdlib.h>
#include <stdio.h>

char * read_file(const char *filename);


Then our main program that will make use of our library will be:

main.c
#include "app.h"

int main(int argc, char *argv[])
{
  char *buffer;
  buffer = NULL;
  buffer = read_file("plain.txt");

  printf("Buffer: \n%s", buffer);

  printf("\n");
  return 0;
}


Now we will also need plain.txt file. Just create this file with some random text in it.

To compile the library we will run:

  make

Then to compile our program using our library we will run:

  gcc main.c libapplication.a -o mainapp

If you want to use your library (written in C) with a C++ code see next example:

main.cc
#include <iostream>

using namespace std;

extern "C" {
  #include "app.h"
}

int main(int argc, char *argv[])
{
  char *buffer;
  buffer = NULL;
  buffer = read_file("plain.txt");

  cout << "Buffer" << buffer << endl;
  return 0;
}

Then to compile this code you will run:

  g++ main.cc libapplication.a -o mainapp2

Note that you will need to wrap your include to the C header file for your library using extern "C" and brackets.

Last Updated ( Wednesday, 02 May 2012 )
 
< Prev   Next >

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?