Makefile giving "No such file or directory" - c++

So I just got through a bunch of compiler issues and finally make runs well for the most part, however now I am getting an error
g++ prog4.cc
g++ -o lextest prog4.o Lexer.o Token.o
g++: prog4.o: No such file or directory
make: *** [lextest] Error 1
This is my makefile
OBJ = prog4.o Lexer.o Token.o
LEXER = Lexer.cc Lexer.h
TOKEN = Token.cc Token.h
OPTS = -g -c -Wall -Werror
lextest: $(OBJ)
g++ -o lextest $(OBJ)
prog4.o: prog4.cc $(LEXER) $(TOKEN)
g++ $(OPTS) prog4.cc
Lexer.o: Lexer.cc Lexer.h
g++ $(OPTS) Lexer.cc
Token.o: Token.cc Token.h
g++ $(OPTS) Token.cc
clean:
rm -f *.o *~
Can anyone see any obvious errors in the makefile or would this error be generated by an issue in prog4.cc?
Pretty new to C++, so I hope this is just some simple error that I can fix.
Thanks!

You are missing the -c and -o options to g++
g++ -c -o prog4.o $(OPTS) prog4.cc

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

C++: error: linker command failed with exit code 1 (use -v to see invocation)

NOTE PLEASE: I read the question and their answers but still, I haven't found any answer to my problem so I had to ask this question.
I have a macOS and kali Linux laptop and when I compaile this same code in my iMac the code work fine (I have some errors in exeption but I'm trying to fix them).
but when I tried to compaile the same code in my kali linux I got the following error:
└─$ make 1 ⨯
g++ -Wall -Wextra -Werror -I. -c -o main.o main.cpp
g++ -Wall -Wextra -Werror -I. -c -o Bureaucrat.o Bureaucrat.cpp
clang++ -Wall -Wextra -Werror -I. -o Bureaucrat main.o Bureaucrat.o -I Bureaucrat.hpp
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:27: Bureaucrat] Error 1
my makefile:
SRCS = main.cpp \
Bureaucrat.cpp \
OBJS = $(SRCS:.cpp=.o)
CPP = clang++
RM = rm -f
CPPFLAGS = -Wall -Wextra -Werror -I.
NAME = Bureaucrat
all: $(NAME)
$(NAME): $(OBJS) Bureaucrat.hpp
$(CPP) $(CPPFLAGS) -o $(NAME) $(OBJS) -I Bureaucrat.hpp
clean:
$(RM) $(OBJS)
fclean: clean
$(RM) $(NAME)
re: fclean $(NAME)
.PHONY: all clean fclean re

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!

Makefile - Erro: file truncated

i have a simple Makefile:
CC=g++
CFLAGS= -Wall -std=c++11 -M -MF dependencyFileName.d -c
objects = Table.o LimitedTable.o aDimension.o test.o
edit: $(objects)
g++ -o edit $(objects)
test.o: LimitedTable.o Table.o aDimension.o test.cpp
$(CC) $(CFLAGS) test.cpp -o test.o
LimitedTable.o: LimitedTable.cpp LimitedTable.hpp Table.o aDimension.o
$(CC) $(CFLAGS) LimitedTable.cpp -o LimitedTable.o
aDimension.o: aDimension.cpp aDimension.cpp Table.o
$(CC) $(CFLAGS) aDimension.cpp -o aDimension.o
Table.o: Table.cpp Table.hpp
$(CC) $(CFLAGS) Table.cpp -o Table.o
clean:
rm -f *.o
and I get this error:
marius#marius-Lenovo-Y50-70 ~/Documents $ make clean
rm -f *.o
marius#marius-Lenovo-Y50-70 ~/Documents $ make edit
g++ -Wall -std=c++11 -M -MF dependencyFileName.d -c Table.cpp -o Table.o
g++ -Wall -std=c++11 -M -MF dependencyFileName.d -c aDimension.cpp -o aDimension.o
g++ -Wall -std=c++11 -M -MF dependencyFileName.d -c LimitedTable.cpp -o LimitedTable.o
g++ -Wall -std=c++11 -M -MF dependencyFileName.d -c test.cpp -o test.o
g++ -o edit Table.o LimitedTable.o aDimension.o test.o
Table.o: file not recognized: File truncated
collect2: error: ld returned 1 exit status
make: *** [edit] Error 1
Can anyone tell me what is wrong ?
Could a wrong include in one of the files be a reason for this error ?
There are some problems with the way you handle your dependency file, but first:
I have a simple Makefile
No you don't. The amount of boilerplate code is way too high, and adding any file to your projet will require you to manually edit that makefile again.
Your Makefile should be boiled down to this:
SRC := $(wildcard *.cpp)
OBJ := $(SRC:.cpp=.o)
DEP := $(OBJ:.o=.d)
CPPFLAGS := -MMD -MP
CXXFLAGS := -std=c++11 -Wall
edit: $(OBJ)
$(CXX) $^ -o $#
-include $(DEP)
clean:
$(RM) $(OBJ) $(DEP)
Here you :
avoid repeating yourself too much,
make good use of make's implicit rules to save time,
use the right built-in variables instead of overriding the wrong ones,
correctly handle dependency files creation and actually use them to prevent manual recompilation,
won't need to edit the makefile when adding a .cpp or .hpp file to your project.
Also, that should fix your problem. Don't forget to clean before trying to compile again after such an error ("file truncated") occurred.

C++: Makefile: object files not compiling? make then says objects not found

So, client.o and server.o are not being compiled... but the make file doesn't error...
I've checked the directory where all the code is and the sub folders. but client and server .o just aren't there... =\
here is my make file:
main_objects = src/main.o src/fann_utils.o src/Config.o
network_objects = src/neural_network_basic.o
hash_objects = src/hashes.o src/hashes/Murmur.o
cloud_objects = src/cloud/client.o src/cloud/server.o
all_objects = $(main_objects) $(hash_objects) $(network_objects) $(cloud_objects)
all: hPif clean
hPif : $(all_objects)
g++ -o hPif $(all_objects) -lfann -L/usr/local/lib
src/cloud/client.o : src/cloud/chat_client.cpp src/cloud/chat_message.hpp
g++ -c src/cloud/chat_client.cpp
src/cloud/server.o : src/cloud/chat_server.cpp src/cloud/chat_message.hpp src/cloud/chat_server.h
g++ -c src/cloud/chat_server.cpp
neural_network_basic.o : src/neural_network_basic.cpp src/neural_network_basic.h
g++ -c src/neural_network_basic.cpp
hashes/Murmur.o : src/hashes/Murmur.cpp src/hashes/Murmur.h
g++ -c src/hashes/Murmur.cpp
Config.o : src/Config.cpp src/Config.h
g++ -c src/Config.cpp
hashes.o : src/hashes.cpp src/hashes.h
g++ -c src/hashes.cpp
fann_utils.o: src/fann_utils.cpp fann_utils.h
g++ -c src/fann_utils.cpp
main.o: src/main.cpp src/main.h
g++ -c src/main.cpp
clean:
rm -rf src/cloud/*.o
rm -rf src/hashes/*.o
rm -rf src/*.o
rm -rf *.o
Console output looks like this:
g++ -c -o src/main.o src/main.cpp
g++ -c -o src/fann_utils.o src/fann_utils.cpp
g++ -c -o src/Config.o src/Config.cpp
g++ -c -o src/hashes.o src/hashes.cpp
g++ -c -o src/hashes/Murmur.o src/hashes/Murmur.cpp
g++ -c -o src/neural_network_basic.o src/neural_network_basic.cpp
g++ -c src/cloud/chat_client.cpp
g++ -c src/cloud/chat_server.cpp
g++ -o hPif src/main.o src/fann_utils.o src/Config.o src/hashes.o src/hashes/Murmur.o src/neural_network_basic.o src/cloud/client.o src/cloud/server.o -lfann -L/usr/local/lib
i686-apple-darwin10-g++-4.2.1: src/cloud/client.o: No such file or directory
i686-apple-darwin10-g++-4.2.1: src/cloud/server.o: No such file or directory
make: *** [hPif] Error 1
g++ -c src/cloud/chat_client.cpp will build an object file called chat_client.o, not client.o. Either add -o $# to the rule to create an object file with the same name as the target, or rename the file to chat_client.o everywhere it's mentioned in the makefile.