"bash: No such file or directory" even though file exists
Solution 1
.o
files are not executables.
Please try one of the following:
-
Skip the creation of the
.o
file and creating the executableg++ -Wall -std=c++11 average.cpp -o average ./average
-
Link the
.o
file into an executableg++ -Wall -std=c++11 -c average.cpp -o average.o g++ -Wall -std=c++11 -o average average.o ./average
If the average
file still doesn't run try giving it permissions
chmod +x average
EDIT (07/03/17)
Explanation
I was asked to explain what the commands do, so here it goes.
g++
This is the compiler that will interpret your code and transform it into machine code that your machine can interpret.
You can read more about it on the man pages. Just open a terminal and type man g++
.
-Wall
This flag tells the compiler to tell you about almost all warnings (Wall = Warn all). Take a look at the code bellow.
int i = 1;
if (i > i) { ... }
If you don't put the flag -Wall
no warning is shown, but if you do you see something like the following:
warning: self-comparison always evaluates to false [-Wtautological-compare]
if (i > i) {}
-std=c++11
With this flag you are telling the compiler which version of the language to use. This is a quite complex topic, but in short C++ is a living language, meaning that it changes overtime and improves, C++11 is one of those changes and it adds a lot of improvements. Nowadays you can use -std=c++14
and sometime this year you'll be able to use -std=c++17
.
You seem new to C++ so worry not about all this version things! :D
-c
Compile Without Linking.
Basically allows the creation of the .o
files (aka object files), that contain a bunch of data, mostly compiled code and information for the compiler to link the objects around.
It makes compilation of large project quicker, since when you change one file it only needs to create one object file and link it with the already compiled object files.
-o file
Taking from the man
page:
Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.
Basically this flag tells the compiler where to put the result of whatever process it is doing (compiling/linking/...).
Hope this explanation helps! :)
Solution 2
you should check the file permissions and ensure that the permissions are set to allow the file to be executed as a program also when trying to execute the file you need to put the .o extension on as by experience this is the only way i have been able to execute files from the command line
Related videos on Youtube
Aaron Kang
Updated on September 18, 2022Comments
-
Aaron Kang over 1 year
I am fairly new to Ubuntu, and recently, I created a .o file by typing in the terminal:
g++ -Wall -std=c++11 -c average.cpp -o average.o
However, when I tried to execute the .o file, I got this error:
bash: ./average: No such file or directory
even though I clearly have the file. So I decided to type in
file average.o
, which got me this:ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
.Is the problem the fact that it has to be a 32-bit executable, and if so, how can I change it into a 32-bit executable?
Edit: So I am able to create
average
, but when I try to run it, I would get this error:terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Aborted (core dumped)
Edit 2: I was able to fix the core dump and managed to give permission for my file to run, but now, I get a new error:
bash: ./average: cannot execute binary file: Exec format error
.When I did
file averge
, I got this:average: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
.-
muru about 7 yearsNow we have a programming problem, which should be asked on Stack Overflow
-
Melebius about 7 yearsAd @muru: …with providing the source code, of course.
-
David Foerster about 7 yearsI rolled back the addition of the answer to the question post. If you solved your problem based on an existing answer it's sufficient and preferred that you please accept that answer and vote for it to increase its visibility and confirm that it was most valid in the solution of your issue. Pleas don’t put the answer in your question or the comments! :-) (see What should I do when someone answers my question?)
-
Aaron Kang about 7 yearsSorry about that! Will these sayings into consideration next time.
-
-
Aaron Kang about 7 yearsI was able to run the file, but if you look at my edit, I run into another problem, so I think I will go to stack overflow for that problem.
-
Ricardo Rodrigues about 7 yearsI corrected the Linking of the
.o
files. Please try again if you can. -
Aaron Kang about 7 yearsI was able to follow the second part and run ./average, but I still got a core dump. Thank you for the help though.
-
Ricardo Rodrigues about 7 yearsI think the core dump is another problem. Please create a new C bug report in the StackOverflow website.
-
Aaron Kang about 7 yearsI was able to fix the core dump and managed to give permission for my file to run, but now, I get a new error:
bash: ./average: cannot execute binary file: Exec format error
. When I didfile averge
, I got this:average: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
. -
Ricardo Rodrigues about 7 yearsI now noticed that the command that skipped the
.o
file had the-c
flag. You should not use that flag when creating a executable. Does removing it helps? -
Aaron Kang about 7 yearsYes! Taking out the -c flag worked and my program is up and running again. Thank you so much! I just have some questions as to what does what. Is
g++
the compiler for the code,-o
the creation of an executable, and-std=c++11
an access to some C++ libraries? Also, what is the point of-Wall
? -
Ricardo Rodrigues about 7 yearsI'll update my answer to explain things better.