Makefile Help

Hi guys. I have three files. One of them is Fraction.h which has the class.
Fraction.cpp has the methods,constructors etc. And FractionTester just for testing. I need a makefile which runs the FractionTester.

When I compile it manually everything is working so I think my problem is at Makefile. Here is my makefile:

1
2
3
4
5
6
7
8
FractionTester: FractionTester.o Fraction.o
	g++ FractionTester.o Fraction.o -o FractionTester -g
	
FractionTester.o: FractionTester.cpp Fraction.h
	g++ FractionTester.cpp -c -g
	
Fraction.o: FractionTester.cpp Fraction.h
	g++ Fraction.cpp -c -g


Last edited on
> so I think my problem is at Makefile
¿what problem?


¿why does Fraction.o depends on FractionTester.cpp?
i am new to Makefile dont know much what should i write?
You should describe the problem that you are having. ¿does not build? ¿is it giving you error messages? ¿Does it refuse to do anything? ¿does it hang? ¿what?


You said that you can do it manually. Then you should do a --dry-run and check the commands for differences.
1
2
3
4
5
6
7
PROG = FractionTester
SRCS = FractionTester.cpp

all: $(PROG)

$(PROG): $(SRCS:.cpp=.o) Fraction.h
	$(CXX) -o $@ $^ $(LDFLAGS)
Last edited on
Hello kbw,

I am getting this error what I did write your code

clang: error: cannot specify -o when generating multiple output files
make: *** [FractionTester] Error 1
Last edited on
HERE IS MY SOLUTION:

1
2
3
4
5
FractionCompile: FractionTester.cpp Fraction.h Fraction.cpp
	g++ -Wall -o FractionTester FractionTester.cpp
	
test: FractionCompile
	./FractionTester
¿Why do you even have Fraction.cpp if never going to use it?


Also, run make several times and notice how it will always try to build, even if there is no changes in the files.
I am using the Fraction.cpp but It is a file that has the methods of the class in Fraction.h files.

Should I compile it too? I tried that but get an error.
If you didn't compile and didn't link it, then you are not using it.

> I tried that but get an error.
for the third time, ¿what error?
dublicate symbol error
1
2
3
4
5
6
7
8
FractionProgram.exe: Fraction.o FractionTester.o
	g++ Fraction.o FractionTester.o -o FractionProgram.exe

Fraction.o: Fraction.cpp Fraction.h
	g++ -c -std=c++11 Fraction.cpp
	
FractionTester.o: FractionTester.cpp Fraction.h
	g++ -c -std=c++11 FractionTester.cpp


i think its correct now
Last edited on
I ran my makefile with your filenames. It generated the right stuff.

If you want to specify compiler options, specify them with CPPFLAGS (for C/C++) or CXXFLAGS (for C++ only).

Similarly, add link options to LDFLAGS.

Let GNU Make do the heavy lifting. Don't tell it how to compile code, it already knows.
1
2
3
4
5
6
7
8
9
PROG = FractionTester
SRCS = FractionTester.cpp Fraction.cpp

CXXFLAGS += -std=c++11

all: $(PROG)

$(PROG): $(SRCS:.cpp=.o) Fraction.h
	$(CXX) -o $@ $^ $(LDFLAGS)
Last edited on
Topic archived. No new replies allowed.