Compiling C and C++ together and automating with Gnu make ========================================================= Public domain ******************************************************************************** ### main.c #include #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 #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_