trying to create g++ makefile

I am trying to create a makefile using example.h and example.cpp with 2 test files test1.cpp and test2.cpp. The code for each file is created and ready but I am clueless on how to create the makefile for this thing. This is what I have so far:

I have a directory name "example" with the following items:

product.h
product.cpp
test1.cpp
test2.cpp

I want to use the sequence of cammands: g++47 -std=c++11 -c -Wall -Wextra -I.

So I am lost, what do I do?
Assuming your environment is set up correctly, there's actually very little for you to do as make has most of the required rules pre-defined.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# example makefile
#
# Notes:
# - the indents must be tabs
# - if you need to specify a non-standard compiler, then set CXX
#   (it defaults to g++)
# - if you need to pass flags to the linker, set LDFLAGS rather
#   than alter the ${TARGET}: ${OBJECTS} rule
# - to see make's built-in rules use make -p

# target executable name
TARGET = example

# source files
SOURCES = product.cpp test1.cpp test2.cpp

# work out names of object files from sources
OBJECTS = $(SOURCES:.cpp=.o)

# compiler flags (do not include -c here as it's dealt with by the
# appropriate rules; CXXFLAGS gets passed as part of command
# invocation for both compilation (where -c is needed) and linking
# (where it's not.)
CXXFLAGS = -std=c++11 -Wall -Wextra -I.

# default target (to build all)
all: ${TARGET}

# clean target
clean:
	rm ${OBJECTS} ${TARGET}

# rule to link object files to create target executable
# $@ is the target, here $(TARGET), and $^ is all the
# dependencies separated by spaces (duplicates removed),
# here ${OBJECTS}
${TARGET}: ${OBJECTS}
	${LINK.cc} -o $@ $^

# no rule is needed here for compilation as make already
# knows how to do it

# header dependencies (in more complicated cases, you can
# look into generating these automatically.)

product.o : product.h test1.h test2.h

test1.o : product.h test1.h test2.h

test2.o : product.h test2.h


PS I started off from this post

Basic Compiling...
http://www.cplusplus.com/forum/unices/109203/2/
Last edited on
This is what I have so far... Its still not right, any suggestions?

CCo = g++47 -std=c++11 -c -Wall -Wextra -I.
CCx = g++47 \

all: test1.x test2.x

test1.x: test1.o product.o
$(CCx) test1.x product.o test1.o

test2.x: test2.o product.o
$(CCx) test2.x product.o test2.o \

product.o: product.h product.cpp
$(CCo) product.cpp

test1.o: product.h product.cpp
$(CCo) test1.cpp

test2.o: product.h product.cpp
$(CCo) test2.cpp
Last edited on
Hi, there are many solutions to this.

Here is yours corrected:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CCo = g++47 -std=c++11 -c -Wall -Wextra -I. 
CCx = g++47 

all: test1 test2

test1: test1.o product.o 
	$(CCx) test1.x product.o test1.o 

test2.x: test2.o product.o 
	$(CCx) test2.x product.o test2.o

product.o: product.h product.cpp 
	$(CCo) product.cpp 

test1.o: product.h product.cpp 
	$(CCo) test1.cpp 

test2.o: product.h product.cpp 
	$(CCo) test2.cpp


Note that lines 7, 10,13,16 and 19 have TABs at their start.

Here is another one that makes your two targets:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

.SUFFIXES:
.SUFFIXES: .c .cpp .o .y

LINK=g++47 $^ -o $@

.cpp.o:
	g++47 -std=c++11 -Wall -Wextra -I. -c $< -o $@

TARGETS =
TARGETS += test1
TARGETS += test2

all:	$(TARGETS)

test1: test1.o product.o
	$(LINK)

test2: test2.o product.o
	$(LINK)


Just go:
make


Here is one that makes your two targets, keeps track of dependencies, and does Doxygen documentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
###########################################################################
##                                                                       ##
## Project: C++ investigation.                                           ##
##                                                                       ##
## Description: Build for this directory.                                ##
##                                                                       ##
## Date: September 26, 2013.                                             ##
##                                                                       ##
###########################################################################

##
## $Id$
##
## $Log$

.SUFFIXES:
.SUFFIXES: .c .cpp .o .y

MAKEFILE_DEPEND = .depend

LINK=g++47 $^ -o $@

.cpp.o:
	g++47 -std=c++11 -Wall -Wextra -I. -c $< -o $@

TARGETS =
TARGETS += test1
TARGETS += test2

all:	$(TARGETS)

depend:
	g++47 -MM *.cpp > $(MAKEFILE_DEPEND)

clean:
	rm -rf *.o $(TARGETS) $(MAKEFILE_DEPEND) html latex

doc:
	doxygen -g
	doxygen

test1: test1.o product.o
	$(LINK)

test2: test2.o product.o
	$(LINK)

-include $(MAKEFILE_DEPEND)

## End of file 


For the first time go:
make clean depend all doc


Then go:
make
Last edited on
great! thank you!
Topic archived. No new replies allowed.