Pejman Moghadam / C-programming

Compiling C and C++ together and automating with Gnu make

Public domain


main.c

#include <stdio.h>
#include "reciprocal.hpp"

int main (int argc, char **argv)
{
        int i;
        i = atoi (argv[1]);
        printf ("The reciprocal of %d is %g \n", i, reciprocal(i));
        return 0;
}

reciprocal.cpp

#include <cassert>
#include "reciprocal.hpp"

double reciprocal (int i) {
        // i should be none zero
        assert (i!=0);
        return 1.0/i;
}

reciprocal.hpp

#ifdef __cplusplus
extern "C" {
#endif

extern double reciprocal (int i);

#ifdef __cplusplus
}
#endif

manual compile

gcc -c main.c
g++ -c reciprocal.cpp
g++ -o reciprocal main.o reciprocal.o

Makefile

reciprocal: main.o reciprocal.o
        g++ $(CFLAGS) -o reciprocal main.o reciprocal.o
main.o: main.c reciprocal.hpp
        gcc $(CFLAGS) -c main.c
reciprocal.o: reciprocal.cpp reciprocal.hpp
        g++ $(CFLAGS) -c reciprocal.cpp
clean:
        rm -f *.o reciprocal

Using make

make
make clean
make CFLAGS=-O2
make clean
make CFLAGS=-g

BY: Pejman Moghadam
TAG: c, cpp, make
DATE: 2011-05-28 14:50:39


Pejman Moghadam / C-programming [ TXT ]