I am attempting to run my make file however i am getting the following two errors:
make: c: command not found
and
make: o: command not found
I am attempting to do this inside of cygwin. I have g++ and make installed on it, however when I run the make file I receive these errors.
Any ideas?
The makefile:
all: MergeSort clean
MergeSort: main.o MergeSort.o
$g++ -o MergeSort main.o MergeSort.o
main.o: main.cpp MergeSort.h
$g++ -c main.cpp
MergeSort.o: MergeSort.cpp MergeSort.h
$g++ -c MergeSort.cpp
clean:
rm -rf *o
cleanall:
rm -rf *o *exe
You need to remove the $ from the $g++ lines. It's trying to expand some variable that doesn't exist, and is swallowing up the "$g++ -" from your commands.
The syntax for using a variable is:
$(CXX) -c main.cpp
In this case, CXX is the path to the C++ compiler, which is defined for you. You can change it by adding the following line to your makefile:
CXX = g++
If you are trying to avoid echoing back the command make is running, use # instead of $.
$g++ is not defined in that makefile, so the command becomes
-o MergeSort main.o MergeSort.o
and
-c main.cpp
Either drop the $ from $g++ and use g++, or define the variable in your makefile.
CXX = g++
all: MergeSort clean
MergeSort: main.o MergeSort.o
$CXX -o MergeSort main.o MergeSort.o
main.o: main.cpp MergeSort.h
$CXX -c main.cpp
MergeSort.o: MergeSort.cpp MergeSort.h
$CXX -c MergeSort.cpp
clean:
rm -rf *o
cleanall:
rm -rf *o *exe
Related
I have a program and i have this make file and im trying to run my program with this makefile, and it compiles well the problem is when i run the program i what to run it like this ./user -n tejo.tecnico.ulisboa.pt -p 58011 with this -n tejo.tecnico.ulisboa.pt or this -p 58011 being optional.
I saw this post Passing arguments to "make run" and im not understanding what im doing wrong in the make run command
So can anyone tell me whats wrong in my makefile?
btw im fairly new at making makefiles and using the command line.
Program:
# Makefile
CC = g++
LD = g++
AUXLIB = auxiliar_code/aux_functions.h
SOCKETLIB = socket_operations/socket_functs.h
COMMANDSLIB = commands/commands.h
.PHONY: all clean run
all: client
client: socket_operations/socket.o auxiliar_code/aux.o commands/commands.o commands/client.o main.o
$(LD) -o user socket_operations/socket.o auxiliar_code/aux.o commands/commands.o commands/client.o main.o
auxiliar_code/aux.o: auxiliar_code/aux_functions.cpp $(AUXLIB) client_constants.h
$(CC) -o auxiliar_code/aux.o -c auxiliar_code/aux_functions.cpp
commands/client.o: commands/Client.cpp $(COMMANDSLIB) $(SOCKETLIB) $(AUXLIB) client_constants.h
$(CC) -o commands/client.o -c commands/Client.cpp
commands/commands.o: commands/commands.cpp $(COMMANDSLIB) $(SOCKETLIB) $(AUXLIB) client_constants.h
$(CC) -o commands/commands.o -c commands/commands.cpp
socket_operations/socket.o: socket_operations/socket_functs.cpp $(SOCKETLIB) $(AUXLIB) client_constants.h
$(CC) -o socket_operations/socket.o -c socket_operations/socket_functs.cpp
main.o: main.cpp $(COMMANDSLIB) $(AUXLIB) client_constants.h
$(CC) $(CFLAGS) -o main.o -c main.cpp
clean:
#echo Cleaning files generated
rm -f auxiliar_code/*.o commands/*.o socket_operations/*.o *.o user
run: user
#echo ./user $(filter-out $#,$(MAKECMDGOALS))
%:
#:
What you should do is to declare a variable (possibly with default):
# Fill in your default here, setting from command line will override
USER_OPTIONS ?=
run: user
./user $(USER_OPTIONS)
Then invoke make setting the option from the command line:
make run USER_OPTIONS="-n tejo.tecnico.ulisboa.pt -p 58011"
I recently started a small project in C++. I created a simply Makefile:
output: main.o google_api.o
g++ main.o google_api.o -o output
rm *.o
clear
./output
main.o: main.cpp
g++ -c main.cpp
test.o: google_api.cpp google_api.h
g++ -c google_api.cpp
And when I compile my code I get the next error -
non-aggregate type 'vector' cannot be initialized
with an initializer list
I am check for this issue and find that I need to add -std=c++11 support to my makefile to fix the problem. I add this command to the code:
g++ -std=c++11 main.o google_api.o -o output
But this is not make any change. I would love if someone can help me to fix this problem. Thanks
change this:
main.o: main.cpp
g++ -c main.cpp
to:
main.o: main.cpp
g++ -std=c++11 -c main.cpp
You may as well use something like this as basis for your Makefile:
CXX=g++
CXXFLAGS=-g -Wall -MMD -std=c++11
LDLIBS=-lm # list libs here
output: main.o google_api.o
clean:
$(RM) *.o *.d output
-include $(wildcard *.d)
There are also similar questions on stackoverflow: Makefile c++11 support
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!
This is my make file.
all: observer
observer: main.o weather_center.o display.o subject.o observer.o
g++ main.o weather_center.o display.o subject.o observer.o -o observer
main.o: main.cpp
g++ -c main.cpp
weather_center.o: weather_center.cpp
g++ -c weather_center.cpp
display.o: display.cpp
g++ -c display.cpp
subject.o: subject.cpp
g++ -c subject.cpp
observer.o: observer.cpp
g++ -c observer.cpp
clean:
rm -f *o observer
Here I'm trying to use
clean:
rm -f *o observer
To clean up the temporary *.o files. But program compiles and generate the target assembly, but doesn't delete the *.o files. Not showing any errors also.
I tried rm -f *o observer in terminal. It works fine.
I have used Tab for indent
there are no files start with clean or rm in the directory.
tried $(RM) instead of rm. but no lucky
I found the issue. Have to specify clean as a target of all otherwise it wont call. generally like this.
all: [your executive names] clean
In above case
all: observer clean
Here is the full make file of above case
all: observer clean
observer: main.o weather_center.o display.o subject.o observer.o
g++ main.o weather_center.o display.o subject.o observer.o -o observer
main.o: main.cpp
g++ -c main.cpp
weather_center.o: weather_center.cpp
g++ -c weather_center.cpp
display.o: display.cpp
g++ -c display.cpp
subject.o: subject.cpp
g++ -c subject.cpp
observer.o: observer.cpp
g++ -c observer.cpp
clean:
rm -f *o observer
As others have mentioned, this is likely an indentation issue. I copied your makefile exactly, touched some dummy .o files and ran it:
$ touch main.o weather_center.o display.o subject.o observer.o
$ make clean
Makefile:4: *** missing separator (did you mean TAB instead of 8 spaces?). Stop.
$
After fixing indentation, it seems to work just fine for me:
$ touch main.o weather_center.o display.o subject.o observer.o
$ ls *.o
display.o main.o observer.o subject.o weather_center.o
$ make clean
rm -f *o observer
$ ls *.o
ls: cannot access *.o: No such file or directory
$
The specific indentation fixes I made were:
target lines should not have any leading whitespace at all
recipe lines must start with exactly one tab and no other whitespace
E.g:
clean:
rm -f *o observer
I have saved the fixed version here, because stackoverflow messes with tabs/whitespace. Make sure to copy from the RAW Paste Data.
Having read the question, comments and your self-answer in a little more detail, I think there is possibly some explaining to do about conventional usage of make.
You may call the make executable with a list of targets to build, or no targets at all.
In the case that targets are specified, then make will attempt to build/rebuild those targets. For instance, with the corrected Makefile, make observer would build the observer target (observer executable), make main.o would simply compile main.cpp to produce main.o, and make clean would invoke the clean rule to delete the listed files.
On the other hand, if you invoke make with no targets, then make will simply use the first target defined in the Makefile as the target that it builds. The convention is that this target is called all, but it can be called whatever you like. So in the case of the corrected makefile, invoking make without explicitly passing any targets should result in all and its dependency observer being rebuilt. I suspect this is where the confusion has arisen - invoking make for this makefile with no targets explicitly mentioned should not result in invocation of the clean target. This is the expected make behavior.
all: observer clean #1
observer: main.o weather_center.o display.o subject.o observer.o
g++ main.o weather_center.o display.o subject.o observer.o -o observer
main.o: main.cpp
g++ -c main.cpp
weather_center.o: weather_center.cpp
g++ -c weather_center.cpp
display.o: display.cpp
g++ -c display.cpp
subject.o: subject.cpp
g++ -c subject.cpp
observer.o: observer.cpp
g++ -c observer.cpp
clean:
rm -f *.o #2
1 fix all: observer to [all: observer clean]
2 fix rm -f *o observer to [rm -f *.o]
I have a Makefile that works for how I'm using it, but will anyone tell me if what I'm doing is good practice? Or if there is a better, cleaner or more efficient way to achieve the goal I am reaching?
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
EXEC = Proj2.out
# The c++ flags to use for compilation
CXXFLAGS = -Wall
# The c++ compiler to use for compilation
CXX = g++
# This section is called on 'make'
# Will call compile, and then call clean
all: compile clean
# Perform action on all object files (May or may not exist)
# The makefile will implicitly compile all .o files needed
# Will also compile them into the EXEC file listed
compile: $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $(EXEC) $(OBJECTS)
# This section is called after compilation is completed
# This will clean all existing .o files listed in the directory
clean:
rm -f *.o
Here is the terminal output when I call 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
rm -f *.o
Is it good practice to use a Makefile like this? Specifically, am I doing the cleaning part of my Makefile correctly?
You should not make all depend on clean at all. By doing this you are ensuring that every time you run make, you have to recompile everything. If you want to do that then using make is itself useless: just write a shell script that compiles and links your code.
The clean target should be a separate target and if you want to clean your workspace you run make clean explicitly.
The other problem with your makefile is that the link rule lists compile as the target, but it builds $(EXE). It's almost never a good idea to have a rule create a file which is not exactly the target you told make it would build. To ensure this, always use $# as the target to generate. Rewrite it like this:
compile: $(EXE)
$(EXE): $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $# $^