Problem with my makefile - c++

I think I have a problem with my makefile. I'm writing this program:
Q2.cpp contains the main.
Agent.cpp Agent.h
Timing.cpp Timing.h
RandomDouble.cpp RandomDouble.cpp
And I'm using the header randoma.h in RandomDouble.cpp.
I downloaded the randomaelf64.a file and I wrote this makefile:
Q2 : Q2.o Agent.o Timing.o RandomDouble.o
g++ -Wall -g randomaelf64.a RandomDouble.o Q2.o Agent.o Timing.o -o Q2
Q2.o : Q2.cpp Agent.h Timing.h
g++ -Wall -g -c Q2.cpp -o Q2.o
Agent.o : Agent.cpp Agent.h Timing.h RandomDouble.h PrintQ2.h
g++ -Wall -g -c Agent.cpp -o Agent.o
RandomDouble.o : RandomDouble.cpp RandomDouble.h randoma.h
g++ -Wall -g -c RandomDouble.cpp -o RandomDouble.o
Timing.o : Timing.cpp Timing.h Agent.h
g++ -Wall -g -c Timing.cpp -o Timing.o
clear :
rm *.o Q2
Except for the first command, each g++.. command is working when I do it separately.
Even when I add a main() to RandomDouble.cpp and run:
g++ -Wall -g randomael64.a RandomDouble.cpp -o rand
it's working. So I think that maybe the problem is with my makefile.
When I run make I get this error:
RandomDouble.o: In function `InitSeed()':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:11: undefined reference to `MersenneRandomInit'
RandomDouble.o: In function `InitSeed(int)':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:16: undefined reference to `MersenneRandomInit'
RandomDouble.o: In function `GetRandomDouble()':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:21: undefined reference to `MersenneRandom'
Agent.o: In function `Agent::SendMessage()':
/cs/stud/ofrenk33/CPP/ex1/Agent.cpp:31: undefined reference to
...
make: *** [Q2] Error 1
I need to say that there are functions declared in randoma.h which are in the randomaelf64.a library.
How do I fix this problem?

You need to link against the library after all your own object files. The linker will only include as much of the library as it needs, and if there are no unresolved references yet, then none of the library will be needed. Change the first rule to:
Q2 : Q2.o Agent.o Timing.o RandomDouble.o
g++ -Wall -g RandomDouble.o Q2.o Agent.o Timing.o randomaelf64.a -o Q2

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

make error: “g++: error: bin/main.o: No such file or directory”

I'm trying to compile a program I wrote, and the structure of the directory is the following:
ctrace > bin, include, src, makefile
bin>
include > Agent.h, Graph.h, Session.h, Tree.h
src> Agent.cpp, ContactTracer.cpp, Graph.cpp, main.cpp, Session.cpp, Tree.cpp, Virus.cpp
The error I'm getting is the following:
g++: error: bin/main.o: No such file or directory
Makefile:5: recipe for target 'cTrace' failed
make: *** [cTrace] Error 1
and this is my make file:
# All Targets
all: cTrace
cTrace: bin/Session.o bin/Agent.o bin/Graph.o bin/Tree.o bin/Virus.o bin/ContactTracer.o
#echo 'Building target: cTrace'
g++ bin/Session.o bin/Agent.o bin/Graph.o bin/Tree.o bin/Virus.o bin/ContactTracer.o bin/main.o -o cTrace
#echo 'target cTrace built successfully'
bin/main.o: src/main.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/main.o src/main.cpp
bin/Session.o: src/Session.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/Session.o src/Session.cpp
bin/Agent.o: src/Agent.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/Agent.cpp -o bin/Agent.o
bin/Graph.o: src/Graph.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/Graph.cpp -o bin/Graph.o
bin/Tree.o: src/Tree.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/Tree.cpp -o bin/Tree.o
bin/Virus.o: src/Virus.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/Virus.cpp -o bin/Virus.o
bin/ContactTracer.o: src/ContactTracer.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/ContactTracer.cpp -o bin/ContactTracer.o
clean:
rm -rf bin/*.o cTrace
What could be the issue? all directories exist, my guess is that main.o is never generated but I can't see why as I don't fully understand all the flags in the makefile. Any tips on how to write a better makefile would be appreciated.
On a side note, I was able to run this exact same project using a CMake generated by CLion.
Thanks in advance.
You are missing bin/main.o as dependency for cTrace target. That's why make does not compile it, and resulting object file is missing. Just add bin/main.o to your list of dependencies in line 4.
Problem seems to be that bin/main.o should be mentioned as a dependency of cTrace
cTrace: bin/Session.o bin/Agent.o bin/Graph.o bin/Tree.o bin/Virus.o bin/ContactTracer.o bin/main.o
At present building cTrace doesn't cause the make file to try and build bin/main.o and so you get an error about a missing file.

Boost & makefile

i'm trying to use the boost_math libs on OS X (i'm not using Xcode), specifically the one containing the error function
I downloaded and compiled boost_1_60_0 myself (using bootstrap.sh and following the instructions.) I didn't use home-brew or something else, which might be why my installation seems so screwed up.
What i'm trying to include in my Szabo.hpp is this:
#include <boost/math/special_functions/erf.hpp>
My makefile goes like this:
LIB_FLAGS = -L/Documents/boost_1_60_0/stage/lib -lboost_math
ALL_OBJECTS = main.o Gaussienne.o Grille.o Szabo.o
all: $(ALL_OBJECTS)
g++ -o hydrogene $(ALL_OBJECTS) $(LIB_FLAGS)
Gaussienne.o: Gaussienne.cpp
g++ -o Gaussienne.o -c Gaussienne.cpp -W -Wall -ansi
main.o: Gaussienne.hpp Grille.hpp main.cpp Szabo.o
g++ -o main.o -c main.cpp -W -Wall -ansi
Grille.o: Grille.cpp Gaussienne.cpp
g++ -o Grille.o -c Grille.cpp -W -Wall -ansi
Szabo.o: Szabo.cpp Gaussienne.cpp
g++ -o Szabo.o -c Szabo.cpp -W -Wall -ansi
clean:
rm -rf *.o
mrproper: clean
rm -rf hydrogene
I get no linking error from g++, however i got:
In file included from Szabo.cpp:12:
./Szabo.hpp:21:10: fatal error: 'boost/math/special_functions/erf.hpp' file not found
#include <boost/math/special_functions/erf.hpp>
^
1 error generated.
Can you please provide help on how to fix this? Thanks in advance
Ok so apparently likes this, it works:
LIB_FLAGS = -L/Users/devolution/Documents/boost_1_60_0/stage/lib -lboost_math_tr1
I_FLAGS = -I/Users/devolution/Documents/boost_1_60_0/
ALL_OBJECTS = main.o Gaussienne.o Grille.o Szabo.o
all: $(ALL_OBJECTS)
g++ -o hydrogene $(ALL_OBJECTS) $(LIB_FLAGS)
Gaussienne.o: Gaussienne.cpp
g++ -o Gaussienne.o -c Gaussienne.cpp -ansi ${I_FLAGS}
main.o: Gaussienne.hpp Grille.hpp main.cpp Szabo.o
g++ -o main.o -c main.cpp -ansi ${I_FLAGS}
Grille.o: Grille.cpp Gaussienne.cpp
g++ -o Grille.o -c Grille.cpp -ansi ${I_FLAGS}
Szabo.o: Szabo.cpp Gaussienne.cpp
g++ -o Szabo.o -c Szabo.cpp -ansi ${I_FLAGS}
.PHONY: clean mrproper
clean:
rm -rf *.o
mrproper: clean
rm -rf hydrogene
Is there a way to pass I_FLAGS?
You've compiled Boost's separately-compiled libraries, which is great, but you didn't copy the headers to your toolchain's include path. Indeed, most of Boost is comprised of header-only libraries, so this is arguably the more crucial step of installing Boost.
The internet tells me you may be able to find the default header search path with the following command at shell:
gcc -x c++ -v -E /dev/null
(https://stackoverflow.com/a/19852298/560648)
When you find it, copy the distribution's boost subdirectory to it.
And, yes, having home-brew install Boost for you would have been much easier… probably one command!

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

Weird error " multiple definition of `xxx` " while compiling C++ project

When I try to compile my C++ project via my Makefile I keep getting errors like those:
Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `Bot::getRandomMessage()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: first defined here
Server.o: In function `Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:27: multiple definition of `Bot::Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:27: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `Bot::getName()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:33: multiple definition of `Bot::getName()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:33: first defined here
Server.o: In function `ChatRoom::getCurrentTime()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: multiple definition of `ChatRoom::getCurrentTime()'
main.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: first defined here
Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `vectorOfThreads'
main.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: first defined here
Server.o: In function `Bot::getRandomMessage()':
I'm quite confused with that.. When I compile it directly with the command
g++ main.cpp -Wall -pedantic -Wno-long-long -O0 -ggdb -lncurses -pthread -o AppName , then it doesn't produce any error. So I expect, that the error appears somewhere in my Makefile
#macros
Remove=rm -rf
Doxygen=Doxyfile
RUN=./dvoram64
FLAGS=-Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g
OBJECTS=main.o Bot.o Server.o Client.o GrumpyBot.o JokerBot.o WeatherBot.o DummyBot.o
#generates final binary and documentation
all: $(Doxygen)
make compile
#build into final binary
compile: $(RUN)
#run program
run:
make link
$(RUN)
$(RUN)
clean:
$(Remove) dvoram64
$(Remove) $(OBJECTS)
#generate documentation in '<login>/doc' folder
doc: $(Doxygen) /*
( cd ./ | doxygen $(Doxygen))
link: $(OBJECTS)
g++ $(OBJECTS) -lncurses -pthread -o dvoram64
#rules how to compile into the executalble file
$(RUN): $(OBJECTS)
Bot.o: ./Bot.cpp ./Bot.h
g++ $(FLAGS) -c ./Bot.cpp
DummyBot.o: ./DummyBot.cpp ./DummyBot.h ./Bot.h
g++ $(FLAGS) -c ./DummyBot.cpp
GrumpyBot.o: ./GrumpyBot.cpp ./GrumpyBot.h ./Bot.h
g++ $(FLAGS) -c ./GrumpyBot.cpp
JokerBot.o: ./JokerBot.cpp ./JokerBot.h ./Bot.h
g++ $(FLAGS) -c ./JokerBot.cpp
WeatherBot.o: ./WeatherBot.cpp ./WeatherBot.h ./Bot.h
g++ $(FLAGS) -c ./WeatherBot.cpp
Client.o: ./Client.cpp
g++ $(FLAGS) -c ./Client.cpp
main.o: ./main.cpp
g++ $(FLAGS) -c ./main.cpp
Server.o: ./Server.cpp ./Bot.h ./JokerBot.h ./WeatherBot.h ./GrumpyBot.h ./DummyBot.h
g++ $(FLAGS) -c ./Server.cpp
Could anybody please explain me, what causes this error and show me, how to fix it?
Look at what the error messages are telling you. Start with the first lines:
Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `Bot::getRandomMessage()'
This message says that the object file Server.o contains a multiple definition of the function Bot::getRandomMessage() function, and that the multiple definition comes from line 18 in the source file Bot.cpp. Now look at the next line:
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: first defined here
This tells you that the definition in Server.o is a multiple definition because there is also a definition in Bot.o. It also tells you the definition in Bot.o came from line 18 in the source file Bot.cpp, which is the same place in the source as the other definition.
This means that Bot.cpp was compiled at least twice, once to make Server.o and once to make Bot.o.
That is probably not what you want. It suggests that some of your source or header files include Bot.cpp when you meant to include Bot.h, or that you have otherwise included Bot.cpp incorrectly. Another possibility is that you have a compile command that compiles Bot.cpp to make Server.o.
generally when i face something like this ...... its a double rule occurrence or the project environment is messed up but Bro , this ain't a makefile issue.
you will have to look into the code ..... I simulated and tested the makefile you put in the question here , with empty files and echo . The makefile seems to be working a-ok.
Kaizen ~/so_test $ make -nf mk.t2
make compile
Kaizen ~/so_test $ make -nf mk.t2 compile
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./main.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./Bot.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./Server.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./Client.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./GrumpyBot.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./JokerBot.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./WeatherBot.cpp
echo g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./DummyBot.cpp
i cant deduce much of a suggestion based on whats there , srry ...
When you compile with g++ main.cpp -Wall -pedantic -Wno-long-long -O0 -ggdb -lncurses -pthread -o AppName you actually don't include your Server, Client, Bot, DummyBot etc. (That you have in your makefile). Thats why you don't see that error.
also If main.cpp compiles without any other files then why do you need these Client, Bot, Server etc .. in your makefile ?
There must be a redefinition somewhere. try to clean and recompile. and then check the functions that its reporting. like Server.cpp:74, Bot.cpp:18, Bot::getRandomMessage()
also surprisingly your main.cpp doesn't call any Server, Bot ... functions. If it calls its supposed to throw linker errors.