Feb 6, 2017 at 2:47am UTC
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 Feb 6, 2017 at 2:47am UTC
Feb 6, 2017 at 4:26am UTC
> so I think my problem is at Makefile
¿what problem?
¿why does Fraction.o depends on FractionTester.cpp?
Feb 6, 2017 at 4:44am UTC
i am new to Makefile dont know much what should i write?
Feb 6, 2017 at 4:17pm UTC
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.
Feb 6, 2017 at 5:57pm UTC
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 Feb 6, 2017 at 5:58pm UTC
Feb 6, 2017 at 6:19pm UTC
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 Feb 6, 2017 at 6:35pm UTC
Feb 6, 2017 at 9:00pm UTC
¿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.
Feb 7, 2017 at 12:40am UTC
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.
Feb 7, 2017 at 1:07am UTC
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?
Feb 7, 2017 at 4:45pm UTC
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 Feb 7, 2017 at 4:45pm UTC
Feb 8, 2017 at 4:14pm UTC
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 Feb 8, 2017 at 4:18pm UTC