leynos: (Default)
[personal profile] leynos
Right, I've seen too many Makefile tutorials that were clearly written by people who have been smoking copious amounts of skunk, so I'm going to post this in the hope that it will make life easier for anyone else writing a small C or C++ project.

It works with GNU make, but I'm sure most BSD people will already have their shit together anyway. Strangely, most of this came from the pmake FAQ, specifically, this chapter.

Here goes:

CFLAGS          = -Wall -I/usr/local/include
CPPFLAGS        =
LIBS            = -lfoo -L/usr/local/lib

PROGRAM         = myapp
OBJS            = main.o bar.o baz.o qux.o etc.o

$(PROGRAM)      : $(OBJS) 
    $(CXX) $(OBJS) $(CXXFLAGS) $(LIBS) -o $(PROGRAM)

include Dependencies.mk

Dependencies.mk :
        $(CXX) -MM *.cpp > Dependencies.mk

.PHONY          : clean
clean           :
    rm -f $(PROGRAM) *.o

Anything in bold and italic means "your stuff goes here", so your executable name goes under "PROGRAM" and the names of your object files go under "OBJ", corresponding to the names of your object source files.

Stuff in "CFLAGS" applies to C source only, "CXXFLAGS" applies to C++ only and "CPPFLAGS" is for preprocessor flags.

For C source only, of course, you would use "$(CC)" instead of "$(CXX)"

I anticipate that, for the C/C++ stuff at any rate, I won't have to make any real changes to this Makefile. At least until I start having multiple source directories.

Cheers to [livejournal.com profile] brucec for corrections and [livejournal.com profile] spacelem for the suggestion of using gcc -MM.

Date: 2010-06-21 06:54 am (UTC)
From: [identity profile] spacelem.livejournal.com
Jonathan Hogg wrote mine for me, and I've just added to it since then, keeping it tidy; it does resemble yours somewhat, although I use g++ -MM > Makefile to get the headers in.

I have seen some makefiles that I couldn't even begin to understand, and then seen them passed on to people who didn't know how to program.

Date: 2010-06-27 02:40 am (UTC)
ext_159540: (Default)
From: [identity profile] leynos.livejournal.com
Actually, that makes a lot of sense. Thanks.


leynos: (Default)

January 2015

456 78910

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 20th, 2017 02:10 am
Powered by Dreamwidth Studios