Specify alternate compiler and linker to make - c++

I have both a default C/C++ tool set (gcc 4.8) and have recently built and installed 5.2 in its own directory. I want to build some C++ programs using a makefile that uses CC and CXX explicity to compile but uses the implicit built-in rule to link the .o files to build the executable.
When I run make on the makefile, I use the command
make all CC=/usr/gcc-5.2.0/bin/gcc-5.2.0 CXX=/usr/gcc-5.2.0/bin/g++-5.2.0
The compile steps all use the 5.2 compilers but when the executable is build, the default g++ is used to link everything. Now, this happens to work and the results run but it isn't what I want. I tried adding
LD=/usr/gcc-5.2.0/bin/g++-5.2.0
to the make command line but LD is ignored. Without changing the makefile, how can I get the link step to use the 5.2 compiler?
Here is the make file:
CPPFLAGS = -Wall
LDFLAGS = -lstdc++
CXX = g++
CC = g++
all: TestSHA3 HashSHA3 HashZeroBytes LongTest sha3sum
debug: override CPPFLAGS += -ggdb
debug: all
SHA3-o3:
$(CXX) $(CPPFLAGS) -O3 -c -o SHA3.o SHA3.cpp
o3: SHA3-o3 all
TestSHA3: TestSHA3.o SHA3.o
HashSHA3: HashSHA3.o SHA3.o
HashZeroBytes: HashZeroBytes.o SHA3.o
LongTest: LongTest.o SHA3.o
sha3sum: sha3sum.o SHA3.o
.PHONY: clean realclean rc debug all o3 SHA3-o3
clean:
rm SHA3.o TestSHA3.o HashSHA3.o HashZeroBytes.o LongTest.o sha3sum.o
rc: realclean
realclean: clean
rm TestSHA3 HashSHA3 HashZeroBytes LongTest sha3sum
Here is the command:
make all CXX=g++-5.2.0 LD=g++-5.2.0
And here are the results of the make:
g++-5.2.0 -Wall -c -o TestSHA3.o TestSHA3.cpp
g++-5.2.0 -Wall -c -o SHA3.o SHA3.cpp
g++ -lstdc++ TestSHA3.o SHA3.o -o TestSHA3
g++-5.2.0 -Wall -c -o HashSHA3.o HashSHA3.cpp
g++ -lstdc++ HashSHA3.o SHA3.o -o HashSHA3
g++-5.2.0 -Wall -c -o HashZeroBytes.o HashZeroBytes.cpp
g++ -lstdc++ HashZeroBytes.o SHA3.o -o HashZeroBytes
g++-5.2.0 -Wall -c -o LongTest.o LongTest.cpp
g++ -lstdc++ LongTest.o SHA3.o -o LongTest
g++-5.2.0 -Wall -c -o sha3sum.o sha3sum.cpp
g++ -lstdc++ sha3sum.o SHA3.o -o sha3sum

Use:
make all CXX=g++-5.2.0 CC=g++-5.2.0
The implicit rule for linking uses CC (make manual):
n is made automatically from n.o by running the linker (usually called
ld) via the C compiler. The precise recipe used is ‘$(CC) $(LDFLAGS)
n.o $(LOADLIBES) $(LDLIBS)’.
With your Makefile and dummy source files:
$ make all CXX=`which g++`
/usr/bin/g++ -Wall -c -o TestSHA3.o TestSHA3.cpp
/usr/bin/g++ -Wall -c -o SHA3.o SHA3.cpp
g++ -lstdc++ TestSHA3.o SHA3.o -o TestSHA3
/usr/bin/g++ -Wall -c -o HashSHA3.o HashSHA3.cpp
g++ -lstdc++ HashSHA3.o SHA3.o -o HashSHA3
/usr/bin/g++ -Wall -c -o HashZeroBytes.o HashZeroBytes.cpp
g++ -lstdc++ HashZeroBytes.o SHA3.o -o HashZeroBytes
/usr/bin/g++ -Wall -c -o LongTest.o LongTest.cpp
g++ -lstdc++ LongTest.o SHA3.o -o LongTest
/usr/bin/g++ -Wall -c -o sha3sum.o sha3sum.cpp
g++ -lstdc++ sha3sum.o SHA3.o -o sha3sum
$ make all CC=`which g++`
g++ -Wall -c -o TestSHA3.o TestSHA3.cpp
g++ -Wall -c -o SHA3.o SHA3.cpp
/usr/bin/g++ -lstdc++ TestSHA3.o SHA3.o -o TestSHA3
g++ -Wall -c -o HashSHA3.o HashSHA3.cpp
/usr/bin/g++ -lstdc++ HashSHA3.o SHA3.o -o HashSHA3
g++ -Wall -c -o HashZeroBytes.o HashZeroBytes.cpp
/usr/bin/g++ -lstdc++ HashZeroBytes.o SHA3.o -o HashZeroBytes
g++ -Wall -c -o LongTest.o LongTest.cpp
/usr/bin/g++ -lstdc++ LongTest.o SHA3.o -o LongTest
g++ -Wall -c -o sha3sum.o sha3sum.cpp
/usr/bin/g++ -lstdc++ sha3sum.o SHA3.o -o sha3sum
$ make all CC=`which g++` CXX=`which g++`
/usr/bin/g++ -Wall -c -o TestSHA3.o TestSHA3.cpp
/usr/bin/g++ -Wall -c -o SHA3.o SHA3.cpp
/usr/bin/g++ -lstdc++ TestSHA3.o SHA3.o -o TestSHA3
/usr/bin/g++ -Wall -c -o HashSHA3.o HashSHA3.cpp
/usr/bin/g++ -lstdc++ HashSHA3.o SHA3.o -o HashSHA3
/usr/bin/g++ -Wall -c -o HashZeroBytes.o HashZeroBytes.cpp
/usr/bin/g++ -lstdc++ HashZeroBytes.o SHA3.o -o HashZeroBytes
/usr/bin/g++ -Wall -c -o LongTest.o LongTest.cpp
/usr/bin/g++ -lstdc++ LongTest.o SHA3.o -o LongTest
/usr/bin/g++ -Wall -c -o sha3sum.o sha3sum.cpp
/usr/bin/g++ -lstdc++ sha3sum.o SHA3.o -o sha3sum

Related

c++ makefile cant find directories

I am trying to run makefile but I cant make him find the files I add to it unless I put full path of the file: /home/"user_name"/CLionProjects/Ass1/src/main.cpp
I try to run the makefile when I am in Ass1 so as much that I understand the makefile should find the main.cpp .
Would love to know where I am wrong here, thank you.
what I write:
all: clean compile link
link:
g++ -o /bin/main /home/daniel/CLionProjects/Ass1/bin/main.o /home/daniel/CLionProjects/Ass1/bin/Workout.o /home/daniel/CLionProjects/Ass1/bin/Trainer.o /home/daniel/CLionProjects/Ass1/bin/Studio.o
compile:
g++ -g -Wall -Weffc++ -c -o bin/main.o /home/daniel/CLionProjects/Ass1/src/main.cpp
g++ -g -Wall -Weffc++ -c -o bin/Studio.o /home/daniel/CLionProjects/Ass1/src/Studio.cpp
g++ -g -Wall -Weffc++ -c -o bin/Workout.o /home/daniel/CLionProjects/Ass1/src/Workout.cpp
g++ -g -Wall -Weffc++ -c -o bin/Trainer.o /home/daniel/CLionProjects/Ass1/src/Trainer.cpp
clean:
rm -f bin/*
what I want to write and cant:
all: clean compile link
link:
g++ -o /bin/main /bin/main.o /bin/Workout.o /bin/Trainer.o /bin/Studio.o
compile:
g++ -g -Wall -Weffc++ -c -o bin/main.o /src/main.cpp
g++ -g -Wall -Weffc++ -c -o bin/Studio.o /src/Studio.cpp
g++ -g -Wall -Weffc++ -c -o bin/Workout.o /src/Workout.cpp
g++ -g -Wall -Weffc++ -c -o bin/Trainer.o /src/Trainer.cpp
clean:
rm -f bin/*
the error I get:
rm -f bin/*
g++ -g -Wall -Weffc++ -c -o bin/main.o /src/main.cpp
g++: error: /src/main.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
make: *** [makefile:7: compile] Error 1

missing separator problem that doesn't relate to text editor

I have the following makefile in my c++ program
#All Targets
all: bin/cTrace
bin/cTrace: bin/main.o bin/agent.o bin/session.o bin/graph.o bin/tree.o
#echo 'Building target: main'
#echo 'Invoking: C++ Linker'
g++ -o bin/cTrace bin/main.o bin/agent.o bin/graph.o bin/tree.o bin/session.o
#echo 'Finished building target: main'
#echo ' '
bin/main.o: src/main.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/main.o src/main.cpp
bin/agent.o: src/Agent.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/agent.o src/Agent.cpp
bin/session.o: src/Session.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/session.o src/Session.cpp
bin/graph.o: src/Graph.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/graph.o src/Graph.cpp
bin/tree.o: src/Tree.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/tree.o src/Tree.cpp
but when I write at the terminal: make I get the following message:
'makefile:7 *** missing separator stop'
I have been looking for the problem and I understand that the main cause is that the text editor may change tab to space so I check throughout all my makefile and I hadn't found any problem.
What else can cause it?
edit:
after I compile cat -e -t -v Makefile I get the following result:
spl211#spl211:~/CLionProjects/cTrace$ cat -e -t -v makefile
#All Targets^M$
all: bin/cTrace^M$
^M$
bin/cTrace: bin/main.o bin/agent.o bin/session.o bin/graph.o bin/tree.o^M$
#echo 'Building target: main'^M$
#echo 'Invoking: C++ Linker'^M$
g++ -o bin/cTrace bin/main.o bin/agent.o bin/graph.o bin/tree.o bin/session.o^M$
#echo 'Finished building target: main'^M$
#echo ' '^M$
^M$
bin/main.o: src/main.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/main.o src/main.cpp^M$
^M$
bin/agent.o: src/Agent.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/agent.o src/Agent.cpp^M$
^M$
bin/session.o: src/Session.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/session.o src/Session.cpp^M$
^M$
bin/graph.o: src/Graph.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/graph.o src/Graph.cpp^M$
^M$
bin/tree.o: src/Tree.cpp^M$
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/tree.o src/Tree.cpp

Running HTTP server example from Boost Asio

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

Makefile: Object are not removed after compiling and linking

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)

How do I use g++ to create object file with a specific name

I am making a makefile and one of the targets is exptrtest.o how do I use g++ to create an objectfile with that name, the name of my cpp file is exprtest.cpp not exptrtest.cpp?
exptrtest.o: exprtest.cpp
g++ -Wall -g -c exprtest.cpp
to make it more clear, this is my makefile:
all: exprtest
exprtest: exptrtest.o driver.o parser.tab.o scanner.o
g++ -Wall -g -o exprtest exptrtest.o driver.o parser.tab.o scanner.o
exptrtest.o: exprtest.cpp
g++ -Wall -g -c exptrtest.o exprtest.cpp
driver.o: driver.cpp scanner.hpp driver.hpp
g++ -Wall -g -c driver.cpp
parser.tab.o: parser.tab.hpp parser.tab.cpp
bison parser.ypp
g++ -Wall -g -c parser.tab.cpp
scanner.o: scanner.cpp scanner.hpp
flex -t scanner.ll > scanner.cpp
g++ -Wall -g -c scanner.cpp
clean:
rm parser.tab.hpp parser.tab.cpp scanner.cpp
I'm getting the error:
"g++: error: exptrtest.o: No such file or directory
make: * [exprtest] Error 1"
Use the -o option in conjunction with -c.
exptrtest.o: exprtest.cpp
g++ -Wall -g -c exprtest.cpp -o exptrtest.o