Jun. 20th, 2010

leynos: (Default)
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
CXXFLAGS        = $(CFLAGS)
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.

Profile

leynos: (Default)
leynos

January 2015

S M T W T F S
    123
456 78910
11121314151617
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 21st, 2017 04:36 pm
Powered by Dreamwidth Studios