Got an assignment to write a simple program in c++11 which contains the following files:
main.cpp
house.h
tree.h
The main function is obviously in the main.cpp file.
main.cpp includes the 2 header files and uses them.
when I tried to compile on Linux, I wrote:
g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp -o prog
And I got a prog file which I could run.
Then I tried to compile it using a makefile, what I did was this:
all:
g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp -o prog
and hit make all
Sadly it doesn't work.
Tried to search the web for how to do it properly, no luck.
Can you tell me how it needs to be done?
Also, how to make a smart compile? i.e. compile only when the above files are changed.
Thanks!
Create a Makefile with just the following lines and check this out:
CFLAGS += -g -O2 -Wall -W -pedantic
CXXFLAGS=$(CFLAGS) -std=c++11
CFLAGS += -pthread
LDFLAGS += -pthread
And type make main. Make will search its built-in rules and discover that there is a way to get from main.cpp to a compiled program named main. It will then execute a built-in rule that looks something like:
$(CXX) $(CXXFLAGS) $(LDFLAGS) $^ -o $#
If you want to just type make then you can add:
all: main
And the built-in implicit rules will build main for you when you type make.
In Makefile
all:
<TAB>g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp -o prog
This will work, Please pay attention here that <TAB> is the tab character that we need to insert.
Simply compiled it by issuing
make
Or for a simple cases, make provides a short cut with default rule. You can compile our code without using Makefile.
CXXFLAGS=-std=c+=11 -Wall -pedantic -pthread make main
But this approach will name the program name as name of the file, i.e. main here, instead of prog as you wanted
Related
Just taking a use case for this instance. I'm compiling a c++ file, and sometimes, I'd like to compile without debugging symbols i.e. the -g enabled and sometimes I would like to enable it.
So, I thought of just making two targets in which the second target would reassign a make variable(is it possible) and change the compiling options. I wonder if such a behaviour is possible to achieve with makefiles?
Below is some pseudocode demo and the user enters make first#bg into the command line:
gpp = g++ -std=c++17
first: hello.cpp
$(gpp) hello.cpp -o $#
#/* some other recipes, assuming the list is really long*/
first#bg: main.o
gpp = g++ -g -std=c++17
execute_all_recipe_of_first_target_which_is_really_long_to_copy()
main.o: main.cpp
$(gpp) main.cpp -c -o main.o #the value of gpp should'd also changed here since first#bg executed
If it is possible please provide me with the actual syntax for the demonstrated behaviour. Thanks in advance.
You can do something like this:
first#bg: gpp += -g
first#bg: first
Note that it's more idiomatic to define CXX=g++ and CXXFLAGS=-std=c++17 and then tweak CXXFLAGS, and use make DEBUG=1 for debug builds:
CXX=g++
CXXFLAGS=-std=c++17
ifeq ($(DEBUG), 1)
CXXFLAGS+=-g
endif
Then invoke the compiler as $(CXX) $(CXXFLAGS) hello.cpp -o $# for example. See also this link
I can't seem to include a header in my test program using a Makefile.
I've attempted to try relative paths using -I with no luck. I'm new to Make and for some reason I am having a hard time understanding it's usage.
my code, test.cpp
#include <iostream>
#include <results/enumTest.h>
int main()
{
return 0;
}
and my Makefile:
CFLAGS = -Wall -g -Wextra -Wpedantic -std=gnu++11 -m64 -Iinclude
test: test.o
gcc $(CFLAGS) -I/.. -o test test.o
test.o: test.cpp
gcc $(CFLAGS) -I/.. -c test.cpp
my directory structure:
/testDir/
./results/enuMtest.h
./test/test.cpp
./test/Makefile
I would hope that I could compile and then run the test software using a Makefile. This is more or less a tutorial for me.
Your include path -I/.. is invalid. You're trying to access the parent directory of the root directory, which cannot exist. Change your Makefile to use relative paths instead with -I..
This will access the parent directory as intended:
CFLAGS = -Wall -g -Wextra -Wpedantic -std=gnu++11 -m64 -Iinclude
test: test.o
g++ $(CFLAGS) -I.. -o test test.o # Change here
test.o: test.cpp
g++ $(CFLAGS) -I.. -c test.cpp # ...and here
Note the removed slashes.
EDIT: As commented by #Lightness, you should include non-system headers with "header.h" rather than <header.h>. Additionally, since you are trying to compile a C++ program, it is recommended to use g++ instead of gcc (I've updated this in the snippet above).
There are several improvements possible.
You try to set the include path to the parent dir of / which is /.
You try to compile a c++ program using gcc but not g++
You don't need (it would still work) to set an include path, when linking. (test:
test.o)
Since there is no directory named include in the filetree you specified, you also don't need -Iinclude in the CFLAGS
Usually the C++ variant of CFLAGS is named CXXFLAGS, but I did not change it in the modified example below.
A corrected makefile would be:
CFLAGS = -Wall -g -Wextra -Wpedantic -std=gnu++11 -m64
test: test.o
g++ $(CFLAGS) -o test test.o
test.o: test.cpp
g++ $(CFLAGS) -I.. -c test.cpp
As an additional note:
#include "" instead of #include <> would also work. The difference is, that "" searches the included file relative from the location of the current source file, while <> uses the directories you specify using -I.
Find more details here
For a project, I have to create a simple makefile for the source code which includes pthreads and command line arguments (if those matter to include).
The first version of the makefile that didn't work was this:
mr: mr.o
g++ -std=c++11 -pthread mr.o -o mr
mr.o: mapred.cc
g++ -std=c++11 -pthread -c mapred.cc
clean:
rm *.o mr
and I got the following error that the object file did not exist?
So then I decided to flip the two first statements around:
mr.o: mapred.cc
g++ -std=c++11 -pthread -c mapred.cc
mr: mr.o
g++ -std=c++11 -pthread mr.o -o mr
clean:
rm *.o mr
and it compiles, sort of? All I get in the terminal is:
g++ -std=c++11 -pthread -c mapred.cc
and nothing else. When I look at what files were created, all I see is a new file mapred.o created but no executable. So no errors but not fully completed. If you guys have any tips to help me out that would be very appreciated. Thank you!
If you don't specify an explicit output name with the -o option, then the compiler will name object file the same as the source file but with an .o suffix.
In your case, the command
g++ -std=c++11 -pthread -c mapred.cc
will create an object file named mapread.o.
Either use mapread.o for your target names and when linking, or use the -o option:
g++ -std=c++11 -pthread -c mapred.cc -o mr.o
As for your second problem, unless you specify an explicit target when invoking make, it will only use the first target and nothing else.
I have made a Makefile for my CMSC 202 course project, 'Blackjack'. It does everything I need it to and it works perfectly. You may be asking why I posted here then, this is because I have no idea how it works and I didn't use any other resources but myself to create it.
Here is my Makefile code.
# Object files to either reference or create
OBJECTS = Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
# The executable file that will be created at the end
EXEC = Proj2.out
# The flags to use for compilation
FLAGS = -Wall
# The code compiler to use for compilation
CC = g++
# Perform action on all object files (May or may not exist)
all: $(OBJECTS)
$(CC) $(FLAGS) -o $(EXEC) $(OBJECTS)
Here is the terminal output when I call make in the terminal.
g++ -c -o Proj2.o Proj2.cpp
g++ -c -o Blackjack.o Blackjack.cpp
g++ -c -o Deck.o Deck.cpp
g++ -c -o Card.o Card.cpp
g++ -c -o Hand.o Hand.cpp
g++ -c -o Player.o Player.cpp
g++ -Wall -o Proj2.out Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
Can anyone tell me how the .o files are being compiled? It does not look like they are being prompted to be compiled with that g++ -c -o $.o $.cpp command anywhere in the Makefile. Nor did I state to use any .cpp files.
Thank you in advance for your help.
Edit
Thanks to all your great help, this is now the terminal output I receive when using make.
g++ -Wall -c -o Proj2.o Proj2.cpp
g++ -Wall -c -o Blackjack.o Blackjack.cpp
g++ -Wall -c -o Deck.o Deck.cpp
g++ -Wall -c -o Card.o Card.cpp
g++ -Wall -c -o Hand.o Hand.cpp
g++ -Wall -c -o Player.o Player.cpp
g++ -Wall -o Proj2.out Proj2.o Blackjack.o Deck.o Card.o Hand.o Player.o
Thank you so much to all of you who have contributed.
Make has a set of implicit rules (see here for a reference). For instance
Compiling C++ programs
`n.o' is made automatically from `n.cc' or `n.C' with a command of the form
`$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)'.
Most make's will also use this rule for .cpp files.
When make sees there's a x.o requirement for one of your targets, it will try to see if it can generate x.o using implicit rules, and in your case find it can do it starting from a .cpp file.
This Makefile uses implicit rules which are a great way to reduce duplication.
By default the first target will be built, here all. It depends on a number
of object files listed in a variable $OBJECTS, e.g. Proj2.o who's
dependencies aren't listed in the Makefile. Now if make sees an input file in the current directory
with a matching name, e.g. Proj2.cpp it will try
to build Proj2.o from it (there are other implicit rules for sources in
other languages). Proj2.o would then be built by default with the command
$(CXX) $(CXXFLAGS) -c -o Proj2.o
where $(CXX) the name of the C++ compiler (g++ in your case).
The explicit build step for all assembles all the object files into the
target executable.
Looking at above build command you'll notice a small problem in your Makefile. Since the flags to the C++ compiler are given in a variable FLAGS and not the standard CXXFLAGS no warnings will be emitted when building the object files. Using the standard name would fix this (you do want warnings, maybe even more than -Wall gives you).
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.