Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for the perfect Makefile canvas

Tags:

c

gcc

makefile

I'm a student in computer engineering and I have yet another C program to implement. Until now I always create a new Makefile from my previous projects and this take time to setup every time. Is there a Makefile canvas I could use that is so powerful that all I would need to provide is the name of the c files containing a main function?

My idea would be that for each new project, I create a folder containing that Makefile, a bin folder and an src folder. I would then edit some variables to the Makefile (the C files containing a main() function) and then make would automatically build everything up, taking dependencies into account.

Do you know if such a Makefile exists?

[edit via Alexandre C.] : Automake/Autoconf are overkills for these kind of projects which use only standards libraries and run on standard unix os'. For the projects we need to implement, dependencies (for the files to be linked) can always be deduces from the ".h" includes and there is generally very few files involved.

like image 218
Mat Avatar asked Oct 10 '12 14:10

Mat


1 Answers

The closest to a magical and perfect Makefile is to use the de facto standard for portable C programs and libraries: Automake.

As an example lifted from this page, this is the example of what you would add to Makefile.am:

bin_PROGRAMS = zardoz
zardoz_SOURCES = main.c head.c float.c vortex9.c gun.c
zardoz_LDADD = $(LIBOBJS)

The equivalent of a make target which we are adding here is called zardoz. The sources are specified under zardoz_SOURCES and any additional libraries which need to be linked are specified under zardoz_LDADD. You do not need to specify where the main function lives, because the linker will automatically find this at the linking stage. If it is not present, the link will simply fail.

like image 56
Mike Kwan Avatar answered Sep 19 '22 09:09

Mike Kwan