LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2009, 09:17 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
How to automatically generate makefiles?


I wonder if there is some software that can generate a makefile for a project, that automatically figures out on what .h files the source files depend?
 
Old 12-26-2009, 03:13 PM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Depending on why you want to automate that:
If your project become too big to handle makefiles by hand, take a look to autotools, it helps you to generate makefile depending on what libraries your project need, then your project will be buildable with the magic "./configure" and "make" couple.

If your project is not so big but you're too lazy (no blame there), any reasonable ide will do the job by just adding files to your project.
You can try eclipse, kdevelopp, code::blocks, anjuta...
 
Old 12-26-2009, 04:32 PM   #3
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Autotools .. makefiles ..
http://www.gnu.org/software/autoconf/
http://www.gnu.org/software/automake/
http://sourceware.org/autobook/

http://www.freesoftwaremagazine.com/..._gnu_autotools
http://www.freesoftwaremagazine.com/...with_autotools
http://www.freesoftwaremagazine.com/..._with_autoconf

http://www.wlug.org.nz/MakefileHowto
http://www.eng.hawaii.edu/Tutor/Make/
.....
.....

A Makefile template, please see post # 3 here
http://www.linuxquestions.org/questi...stop-709818/#3
.....
 
1 members found this post helpful.
Old 12-26-2009, 05:02 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
About the IDE suggestion, I was kinda considering them but I am reluctant because when I used Windows I used Visual C++ and it was a nasty experience. Also i tried code::blockas under Windows and it isolates you from what's going on and seemed hard to understand. Maybe some of the other ones are better.

I tries autotools before, but I just could not understand it and it just seems so over-complicated that you can break your head trying to understand it.

Why not just have a file that lists all the .c files, and a program that automatically figures out the dependencies and creates a Makefile?
 
Old 12-27-2009, 12:16 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I was thinking about IDEs, and I thought what do you think would be the best IDE for plain C projects?

I looked at Code::Blocks, Eclipse, and KDevelop, and it seems like Code::Blocks and Eclipse are just too bloated and fancy, and KDevelop is too oriented for C++/Qt projects.
 
Old 12-27-2009, 05:03 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'm looking at KDevelop 4 and it looks like all the templates are either KDE4 or Qt. I remember there being choices for non-GUI apps before. I guess it's been a while since I looked at it! For non-GUI apps, I'd much rather set up autotools and edit my sources with a text editor. I don't really think an IDE is necessary otherwise.
Kevin Barry
 
Old 12-27-2009, 05:09 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by MTK358 View Post
Why not just have a file that lists all the .c files, and a program that automatically figures out the dependencies and creates a Makefile?
If your project only consists of .c and .h files and only makes one executable, the following Makefile suffices:
Code:
#########################
# customise these
CFILES := foo.c bar.c
PROG := prog
CFLAGS := -Wall -Wextra -g
LDFLAGS :=
########################

# -MMD generates dependencies while compiling
CFLAGS += -MMD
CC := gcc

OBJFILES := $(CFILES:.c=.o)
DEPFILES := $(CFILES:.c=.d)

$(PROG) : $(OBJFILES)
        $(LINK.o) $(LDFLAGS) -o $@ $^

clean :
        rm -f $(PROG) $(OBJFILES) $(DEPFILES)

-include $(DEPFILES)
 
Old 12-27-2009, 05:14 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Will it check for dependencies recursively (i.e. if main.c includes foo.h, and foo.h includes bar.h, will changing bar.h cause main.c to be recompiled)?

Seems to me that I saw somewhere that make does not do this.

Last edited by MTK358; 12-27-2009 at 05:16 PM.
 
Old 12-27-2009, 05:27 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
That's the whole point of make..
 
Old 12-27-2009, 06:42 PM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by tuxdev View Post
That's the whole point of make..
The point of make is to check dependencies for changes (i.e. change in mod date.) The point of automake and autoconf is to generate the makefiles by determining those dependencies.
Kevin Barry
 
Old 12-27-2009, 07:18 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I thought that the Makefile that ntubski posted just needs you to input your .c files and it does the rest automatically. (by the way, is it possible to just say "src/*.c"?)

I also wonder if it is possible to have the .o files go into a different directory?

Why would I want something like automake and autoconf then?

Last edited by MTK358; 12-27-2009 at 07:20 PM.
 
Old 12-27-2009, 07:24 PM   #12
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
A simple Makefile using GCC's dependency generation works for a lot of simple cases, but isn't portable and doesn't work nicely for complex build needs.

You can use $(wildcard src/*.cpp)
http://www.gnu.org/software/make/manual/

I personally prefer CMake to generate my makefiles (and VS solutions, and..)

Last edited by tuxdev; 12-27-2009 at 07:25 PM.
 
Old 12-27-2009, 07:34 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
What exactly is CMake and VS, and how does gcc's dependency generation work and why is it not portable? I thought that make figures out the dependencies.
 
Old 12-27-2009, 07:53 PM   #14
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by MTK358 View Post
What exactly is CMake and VS, and how does gcc's dependency generation work and why is it not portable? I thought that make figures out the dependencies.
make doesn't figure out dependencies, rather, given a set of depencies it will regenerate files with the minimum needed work. To figure out depencies between C files you need a tool that understands preprocessor #directives, eg: gcc (see Preprocessor Options). It's not portable in the sense that other compilers might not have an option to do this.

CMake.
VS = Visual Studio?
 
Old 12-27-2009, 08:06 PM   #15
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So basically gcc should be capable of something like this, right?:

Code:
$ gcc -M main.c
        main.o: main.c foo.h bar.h
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Automatically writing makefiles with Automake LXer Syndicated Linux News 0 06-04-2008 04:11 PM
LXer: Automatically generate PHP documentation from Subversion with phpDocumentor LXer Syndicated Linux News 0 11-05-2007 11:41 AM
How to generate Prerequisites automatically for makefile Greatrebel Programming 1 03-07-2007 09:32 AM
(bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically jimieee Programming 9 05-03-2006 10:24 AM
Dynamically generate a webage and update it automatically using RSS xbaez Programming 7 01-31-2006 10:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration