Following makefile works except cleaning the object files after compiling and linking. I tried make clean which does exactly what I want: deletes the executable and the object files in all folders. I also included the outputs of make and make clean. Any idea?
Makefile:
CC=g++
CFLAGS=-c -std=c++11 -O2 -O3
SOURCES=main.cpp\
BoundaryConditions/BoundaryConditions.cpp\
Cell/Cell.cpp\
Face/Face.cpp\
Formulation/Explicit/Explicit.cpp\
Formulation/Implicit/Implicit.cpp\
Grid/Grid.cpp\
Grid/ReadGrid.cpp\
Grid/SetGrid.cpp\
Init/Init.cpp\
InterFlux/Interflux.cpp\
InterFlux/Roe/Roe.cpp\
Matrix5/Operators.cpp\
Output/Output.cpp\
Solver/GaussSeidel.cpp\
Vector/Vector.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=codeBaku
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $#
.cpp.o:
$(CC) $(CFLAGS) $< -o $#
.PHONY: clean
clean:
rm -rf $(OBJECTS) $(EXECUTABLE)
Output of make:
g++ -c -std=c++11 -O2 -O3 main.cpp -o main.o
g++ -c -std=c++11 -O2 -O3 BoundaryConditions/BoundaryConditions.cpp -o BoundaryConditions/BoundaryConditions.o
g++ -c -std=c++11 -O2 -O3 Cell/Cell.cpp -o Cell/Cell.o
g++ -c -std=c++11 -O2 -O3 Face/Face.cpp -o Face/Face.o
g++ -c -std=c++11 -O2 -O3 Formulation/Explicit/Explicit.cpp -o Formulation/Explicit/Explicit.o
g++ -c -std=c++11 -O2 -O3 Formulation/Implicit/Implicit.cpp -o Formulation/Implicit/Implicit.o
g++ -c -std=c++11 -O2 -O3 Grid/Grid.cpp -o Grid/Grid.o
g++ -c -std=c++11 -O2 -O3 Grid/ReadGrid.cpp -o Grid/ReadGrid.o
g++ -c -std=c++11 -O2 -O3 Grid/SetGrid.cpp -o Grid/SetGrid.o
g++ -c -std=c++11 -O2 -O3 Init/Init.cpp -o Init/Init.o
g++ -c -std=c++11 -O2 -O3 InterFlux/Interflux.cpp -o InterFlux/Interflux.o
g++ -c -std=c++11 -O2 -O3 InterFlux/Roe/Roe.cpp -o InterFlux/Roe/Roe.o
g++ -c -std=c++11 -O2 -O3 Matrix5/Operators.cpp -o Matrix5/Operators.o
g++ -c -std=c++11 -O2 -O3 Output/Output.cpp -o Output/Output.o
g++ -c -std=c++11 -O2 -O3 Solver/GaussSeidel.cpp -o Solver/GaussSeidel.o
g++ -c -std=c++11 -O2 -O3 Vector/Vector.cpp -o Vector/Vector.o
g++ main.o BoundaryConditions/BoundaryConditions.o Cell/Cell.o Face/Face.o Formulation/Explicit/Explicit.o Formulation/Implicit/Implicit.o Grid/Grid.o Grid/ReadGrid.o Grid/SetGrid.o Init/Init.o InterFlux/Interflux.o InterFlux/Roe/Roe.o Matrix5/Operators.o Output/Output.o Solver/GaussSeidel.o Vector/Vector.o -o codeBaku
Output of make clean:
rm -rf main.o BoundaryConditions/BoundaryConditions.o Cell/Cell.o Face/Face.o Formulation/Explicit/Explicit.o Formulation/Implicit/Implicit.o Grid/Grid.o Grid/ReadGrid.o Grid/SetGrid.o Init/Init.o InterFlux/Interflux.o InterFlux/Roe/Roe.o Matrix5/Operators.o Output/Output.o Solver/GaussSeidel.o Vector/Vector.o codeBaku
It is unusual to automatically remove the object files, since that would mean everything would have to be recompiled each time, even if you only change one source file. However, if you really want to do it, you could do something like this:
all: $(SOURCES) $(EXECUTABLE)
rm $(OBJECTS)
Related
I want to create a makefile which runs program in C++ once with "CXXFLAGS = -std=c++11 -g -O3 -DTEST -fopenmp" and one time with: "CXXFLAGS = -std=c++11 -g -O3 -fopenmp"
at the end outputs two different files like P1-Test and P1. how can I edit this file?
CXX = g++
CXXFLAGS = -std=c++11 -g -O3 -fopenmp
ifdef code_coverage
GCOV_FLAG := -DTEST
else
GCOV_FLAG :=
endif
all: P1
#echo The program has been compiled
# implicit rule: create x from x.cpp
.cpp:
$(CXX) $(CXXFLAGS) $? -o $#
$(CXX) $(CXXFLAGS) $(GCOV_FLAG) $? -o $#
.PHONY: clean
clean:
$(RM) -r P1 *.dSYM
My suggestion:
CXX = g++
CXXFLAGS = -std=c++11 -g -O3 -fopenmp
all: P1 P1-Test
#echo The program has been compiled
# implicit rule: create x from x.cpp
.cpp:
$(CXX) $(CXXFLAGS) $? -o $#
.PHONY: clean
clean:
$(RM) -r P1 *.dSYM
P1: main.o second.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o "$#" $^
P1-Test: CXXFLAGS+=-DTEST
P1-Test: main.o second.o
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o "$#" $^
With sample sources:
File main.cpp
extern void foo(); // should be in second.h or something
int main() { foo(); }
File second.cpp
#include <cstdio>
void foo() {
#ifdef TEST
puts("TEST defined");
#else
puts("TEST not defined");
#endif
}
Results in
$ make -B
g++ -std=c++11 -g -O3 -fopenmp -o "P1" main.cpp second.cpp
g++ -std=c++11 -g -O3 -fopenmp -DTEST -o "P1-Test" main.cpp second.cpp
The program has been compiled
And of course the outputs:
./P1; ./P1-Test
TEST not defined
TEST defined
Alternative
If your .o files are really .PRECIOUS, you might want to build separate copies. Here I split into release/main.o and test/main.o:
CXX = g++
CXXFLAGS = -std=c++11 -g -O3 -fopenmp
all: P1 P1-Test
#echo The program has been compiled
test/%.o: CXXFLAGS+=-DTEST
test/%.o: %.cpp
mkdir -pv $(#D)
$(CXX) $(CXXFLAGS) $? -c -o $#
release/%.o: %.cpp
mkdir -pv $(#D)
$(CXX) $(CXXFLAGS) $? -c -o $#
.PHONY: clean
clean:
$(RM) -rfv P1 P1-Test *.dSYM release/ test/
P1: release/main.o release/second.o
P1-Test: test/main.o test/second.o
P1 P1-Test:
$(CXX) $(CXXFLAGS) -o "$#" $^ $(LDFLAGS)
Which gives:
mkdir -pv release
g++ -std=c++11 -g -O3 -fopenmp main.cpp -c -o release/main.o
mkdir -pv release
g++ -std=c++11 -g -O3 -fopenmp second.cpp -c -o release/second.o
g++ -std=c++11 -g -O3 -fopenmp -o "P1" release/main.o release/second.o
mkdir -pv test
g++ -std=c++11 -g -O3 -fopenmp -DTEST main.cpp -c -o test/main.o
mkdir -pv test
g++ -std=c++11 -g -O3 -fopenmp -DTEST second.cpp -c -o test/second.o
g++ -std=c++11 -g -O3 -fopenmp -o "P1-Test" test/main.o test/second.o
echo The program has been compiled
I'm getting errors when trying to run the HTTP server example that comes with the source of the boost library, under the path: boost_1_59_0/libs/asio/example/cpp11/http/server/.
I already ran this following commands in the boost_1_59_0 directory:
$ ./bootstrap.sh
$ sudo ./bjam install
$ sudo ./b2 install
After installing all targets, i tried to compile the main.cpp and the server.cpp with this command: g++ -std=c++0x -o main main.cpp -I "/home/user/Desktop/boost_1_59_0" -L "/home/user/Desktop/boost_1_59_0/libs/" -lboost_system.
Any suggestion on how to compile this server example?
I linked all files from the boost_1_59_0/libs/asio/example/cpp11/http/server/ folder after the main.cpp, as #Richard Hodges suggested. It still didn't work, i got errors concerning lpthread, so i added it to the compiling options. The program compiled but it failed the execution, returning an error saying that it didn't find the library libboost_system.so.1.59.0. I tried linking the folders with -L /path/to/library but it didn't work.
Solution:
My compilation command:
g++ -std=gnu++0x -o main main.cpp server.cpp connection.cpp connection_manager.cpp reply.cpp mime_types.cpp request_handler.cpp request_parser.cpp -I "/home/user/Desktop/boost_1_59_0" -lboost_system -lpthread
I solved it with this commands:
$ LD_LIBRARY_PATH="/usr/local/lib/"
$ sudo ldconfig
And then I just ran the executable and it worked!
Here's a simple makefile I just concocted that works:
all:server
CPPFLAGS+=-std=c++11 -Wall -pedantic
CPPFLAGS+=-g -O2
CPPFLAGS+=-pthread
LDFLAGS+=-lboost_system
%.o:%.cpp
$(CXX) $(CPPFLAGS) $^ -c -o $#
server:$(patsubst %.cpp,%.o,$(wildcard *.cpp))
$(CXX) $(CPPFLAGS) $^ -o $# $(LDFLAGS)
It runs make:
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection.cpp -c -o connection.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection_manager.cpp -c -o connection_manager.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread main.cpp -c -o main.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread mime_types.cpp -c -o mime_types.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread reply.cpp -c -o reply.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread request_handler.cpp -c -o request_handler.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread request_parser.cpp -c -o request_parser.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread server.cpp -c -o server.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection.o connection_manager.o main.o mime_types.o reply.o request_handler.o request_parser.o server.o -o server -lboost_system
And the test program runs:
$ ./server 0.0.0.0 9889 . &
$ GET http://localhost:9889/main.cpp > main.cpp.0
Check the files
$ md5sum main.cpp*
be5dc1c26b5942101a7895de6baedcee main.cpp
be5dc1c26b5942101a7895de6baedcee main.cpp.0
Don't forget to kill the server when you're done
I am doing a c++ project with multiple source files and trying to get used to using makefiles. I want to be able to debug this program with gdb. If I use the following command in Terminal to compile, it works fine:
g++ -o main -g *.cpp
But if I just call make it doesn't generate a debug file (the .dSYM file) even though it compiles the program fine. I assume this has something to do with creating the individual object files first. Here is my makefile:
all: main.o sort.o bubble.o quickSort.o rbs.o
g++ -g -o main *.o -Wall -O2
main.o: main.cpp
g++ -c main.cpp
sort.o: sort.cpp sort.h
g++ -c sort.cpp
bubble.o: bubble.cpp bubble.h
g++ -c bubble.cpp
quickSort.o: quickSort.cpp quickSort.h
g++ -c quickSort.cpp
rbs.o: rbs.cpp rbs.h
g++ -c rbs.cpp
clean:
rm *.o
How do I create the main.dSYM debug file when using a makefile like this?
If you want the debug files, you must compile all of the components with -g.
The crude way to do this would be to add -g to every object rule:
all: main.o sort.o bubble.o quickSort.o rbs.o
g++ -g -o main *.o -Wall -O2
main.o: main.cpp
g++ -c -g main.cpp
sort.o: sort.cpp sort.h
g++ -c -g sort.cpp
bubble.o: bubble.cpp bubble.h
g++ -c -g bubble.cpp
quickSort.o: quickSort.cpp quickSort.h
g++ -c -g quickSort.cpp
rbs.o: rbs.cpp rbs.h
g++ -c -g rbs.cpp
But that doesn't leave you the option of building without debug information. And there's a lot of redundancy in this makefile. Let's take this in stages. First, we put in automatic variables to simplify the rules:
all: main.o sort.o bubble.o quickSort.o rbs.o
g++ -g -o main $^ -Wall -O2
main.o: main.cpp
g++ -c -g $<
sort.o: sort.cpp sort.h
g++ -c -g $<
bubble.o: bubble.cpp bubble.h
g++ -c -g $<
quickSort.o: quickSort.cpp quickSort.h
g++ -c -g $<
rbs.o: rbs.cpp rbs.h
g++ -c -g $<
Now we see that all of the *.o rules have the same command, which reminds us that Make already knows how to build foo.o from foo.cpp, with a command that looks like:
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c
So all we have to do is add -g to CXXFLAGS, and we can omit the commands entirely:
CXXFLAGS += -g
all: main.o sort.o bubble.o quickSort.o rbs.o
g++ -g -o main $^ -Wall -O2
sort.o: sort.h
bubble.o: bubble.h
quickSort.o: quickSort.h
rbs.o: rbs.h
Now that that's in order, we can set up two top-level targets, main and debug, and change CXXFLAGS only for the latter:
debug: CXXFLAGS += -g
main debug: main.o sort.o bubble.o quickSort.o rbs.o
g++ -g -o $# $^ -Wall -O2
sort.o: sort.h
bubble.o: bubble.h
quickSort.o: quickSort.h
rbs.o: rbs.h
You can improve this even more, but that should get you started.
I'm trying to compile my project using a Makefile, but somehow the -fopenmp flag won't work.
Here's the Makefile:
TARGET=isaac
CC=g++
CFLAGS=-Wall -O2 -fopenmp
LDFLAGS=-lm -lpthread -lrt
OBJ=src/main.o src/bhtree.o src/body.o src/configreader.o src/diagnostics.o src/output.o src/quad.o src/timing.o src/vector2.o
isaac: $(OBJ)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJ) $(LDFLAGS)
%.o: src/%.cpp
$(CC) $(CFLAGS) -c $<
clean:
rm src/*.o src/*~ isaac
and here is the output when calling "make"
g++ -c -o src/main.o src/main.cpp
g++ -c -o src/bhtree.o src/bhtree.cpp
g++ -c -o src/body.o src/body.cpp
g++ -c -o src/configreader.o src/configreader.cpp
g++ -c -o src/diagnostics.o src/diagnostics.cpp
g++ -c -o src/output.o src/output.cpp
g++ -c -o src/quad.o src/quad.cpp
g++ -c -o src/timing.o src/timing.cpp
g++ -c -o src/vector2.o src/vector2.cpp
g++ -Wall -O2 -fopenmp -o isaac src/main.o src/bhtree.o src/body.o src/configreader.o src/diagnostics.o src/output.o src/quad.o src/timing.o src/vector2.o -lm -lpthread -lrt
the -fopenmp flag is missing when the source files are compiled, so the finished executable is serial, not parallel.
How can I fix this?
The problem is that your rule does not apply at all. You are free to remove
%.o: src/%.cpp
$(CC) $(CFLAGS) -c $<
and you'll get the same result as before. That's because some predefined rule is used instead of yours (I'm not great makefile expert though).
The core of the problem is that your rule is for ./*.o files, but you need ./src/*.o for isaac. You can change your rule
src/%.o: src/%.cpp
$(CC) $(CFLAGS) -c $<
Or (better) move all autogenerated staff somewhere from src.
I got a C++ program for which someone else made a make file. I want to compile the program with flag -g, but I don't know where to add it. Below is the make file.
CC = g++
LOADLIBES = -lm
CFLAGS = -Wall -O2
SRC1 = Agent.cpp Breeder.cpp CandidateSolution.cpp \
Cupid.cpp FateAgent.cpp Grid.cpp Reaper.cpp \
fitness.cpp
SRC2 = main.cpp
SRC = $(SRC1) $(SRC2)
OBJS = $(SRC1:.cpp = .o)
AUX = $(SRC1:.c = .h)
main: $(OBJS)
# $(CC) $(CFLAGS) -o $(SRC) $(AUX)
.PHONY: clean
clean:
rm -f *.o main
Where should I add that I want to use -g?
$(CC) is used for compiling C programs. $(CXX) is used for compiling C++ programs. Similarly $(CFLAGS) is used for C programs, $(CXXFLAGS) is used for compiling C++.
Change the first few lines to this:
#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g
(But see others' notes about incompatibilities between -O2 and -g.)
Get rid of the spaces inside the parentheses in this line:
OBJS = $(SRC1:.cpp=.o)
Change the main lines to this:
main: $(OBJS) $(SRC2)
# Built by implicit rules
The resulting makefile should look like this:
#CC = g++
LOADLIBES = -lm
CXXFLAGS = -Wall -O2 -g
SRC1 = Agent.cpp Breeder.cpp CandidateSolution.cpp \
Cupid.cpp FateAgent.cpp Grid.cpp Reaper.cpp \
fitness.cpp
SRC2 = main.cpp
SRC = $(SRC1) $(SRC2)
OBJS = $(SRC1:.cpp=.o)
AUX = $(SRC1:.c=.h)
main: $(OBJS) $(SRC2)
# Built by implicit rules
.PHONY: clean
clean:
rm -f *.o main
and the output should look like this:
$ make
g++ -Wall -O2 -g -c -o Agent.o Agent.cpp
g++ -Wall -O2 -g -c -o Breeder.o Breeder.cpp
g++ -Wall -O2 -g -c -o CandidateSolution.o CandidateSolution.cpp
g++ -Wall -O2 -g -c -o Cupid.o Cupid.cpp
g++ -Wall -O2 -g -c -o FateAgent.o FateAgent.cpp
g++ -Wall -O2 -g -c -o Grid.o Grid.cpp
g++ -Wall -O2 -g -c -o Reaper.o Reaper.cpp
g++ -Wall -O2 -g -c -o fitness.o fitness.cpp
g++ -Wall -O2 -g main.cpp Agent.o Breeder.o CandidateSolution.o Cupid.o FateAgent.o Grid.o Reaper.o fitness.o -lm -o main
For completeness, this is the version of make I am using on Ubuntu 10.04:
$ make -v
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for i486-pc-linux-gnu
You need to uncomment the line:
# $(CC) $(CFLAGS) -o $(SRC) $(AUX)
(remove the hash sigh):
$(CC) $(CFLAGS) -o $(SRC) $(AUX)
And change
CFLAGS = -Wall -O2
to
CFLAGS = -Wall -O2 -g
But you may find debugging easier if you disable optimization by removing -O2:
CFLAGS = -Wall -g