PDA

View Full Version : creating a makefile for C++



micahpage
December 29th, 2012, 11:05 AM
If you have multiple cpp files, h files, or even some in dubdirectories, what do you put in the makefile?

For example,

metulburr@ubuntu:~/Documents/cplusplus/composition$ ls
Birthday.cpp Birthday.h main.cpp People.cpp People.h
metulburr@ubuntu:~/Documents/cplusplus/composition$

dwhitney67
December 29th, 2012, 12:21 PM
Start by reading/researching: http://www.gnu.org/software/make/manual/make.html


For the simple project layout you posted, the following basic/simple Makefile should suffice:


# Application name; change if needed.
APP = bday

# Listing of source code files
SRCS = Birthday.cpp main.cpp People.cpp

# Formulate the names of the respective object files
OBJS = $(SRCS:.cpp=.o)

# Define appropriate compiler options
CXXFLAGS = -Wall -pedantic

# Define appropriate link options and libraries
LDFLAGS =
LIBS =


.PHONY = all clean


all : $(APP)


$(APP) : $(OBJS)
$(CXX) $^ $(LDFLAGS) $(LIBS) -o $@


clean :
$(RM) $(OBJS) $(APP)



Note:


$(APP) : $(OBJS)
<tab-space>$(CXX) $^ $(LDFLAGS) $(LIBS) -o $@


clean :
<tab-space>$(RM) $(OBJS) $(APP)

bird1500
December 30th, 2012, 09:54 AM
It's slightly off topic, but if you haven't considered cmake yet, imo you should. If you go the make route you'll have to learn/use autotools, also known as autohell, cmake is much simpler (and crossplatform), more modern and faster. (And yes, nothing is ideal, but some things are generally (much) better than others).

dwhitney67
December 30th, 2012, 12:16 PM
... If you go the make route you'll have to learn/use autotools...
Your assumption is incorrect. I use Makefiles all the time; never once have I had to use autotools.

bird1500
December 30th, 2012, 12:37 PM
Your assumption is incorrect. I use Makefiles all the time; never once have I had to use autotools.
Please check my spelling too. When people say something they don't mean it in absolute terms, unless explicitly told so.