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
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.
From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

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. 28th, 2017 06:50 am
Powered by Dreamwidth Studios