0

I have already checked out the many similar questions on this website, but I keep getting this error when trying to run my program conversion.cpp with ./conversion.

The program is here:

(i can't seem to copy paste the text)

Whenever I run the program, I get the error that there is no such file or director. I have already tried chmod to no avail.

Any help would be great appreciated!

iceblender
  • 29
  • 1
  • 2
  • How did you compile it? `g++` or `gcc`? – muru Sep 02 '14 at 18:07
  • use `./conversion.cpp` instead of `./conversion` – αғsнιη Sep 02 '14 at 18:12
  • No. You can't run a c or c++ program without compiling and linking it first. It's not a scripting language like php, or python. – Marty Fried Sep 02 '14 at 18:15
  • I'm really new to do this and naturally I don't have right terminology but I just used gedit conversion.cpp wrote the program in that window, and went back to the original script to run it. – iceblender Sep 02 '14 at 18:36
  • When I used ./conversion.cpp I get this http://puu.sh/biCF9/c9fb344b84.png – iceblender Sep 02 '14 at 18:37
  • The steps are the following: Create the .cpp, compile it (gcc), then run the created executable (./executable) – hytromo Sep 02 '14 at 18:37
  • [WikiHow](http://www.wikihow.com/Compile-a-C/C%2B%2B-Program-on-Ubuntu-Linux) and [this Q/A](http://askubuntu.com/questions/61408/what-is-a-command-to-compile-and-run-c-programs) – αғsнιη Sep 02 '14 at 18:52

1 Answers1

3

You must compile your .cpp file into a binary file, chmod +x the resulting binary file, then run the compiled binary. You can do this easily with the following syntax:

g++ <cpp file> -o <resulting binary file>

i.e. the following for your particular program:

g++ conversion.cpp -o conversion

Example:

mgodby@mg-ws1:~/code$ g++ helloworld.cpp -o helloworld
mgodby@mg-ws1:~/code$ ls
helloworld  helloworld.cpp
mgodby@mg-ws1:~/code$ chmod +x helloworld
mgodby@mg-ws1:~/code$ ./helloworld 
Hello World!
mgodby@mg-ws1:~/code$

Note 1: If your compile fails for some reason, make sure that all necessary packages are installed -

sudo apt-get -y install g++

Note 2: You must re-compile, i.e. run the g++ command again, every time you make a change to the original .cpp file, else the changes you make will not take effect.


The reason that you cannot run your .cpp file directly is that c++ is a "compiled" language and not a "scripting" or "interpreted" language. If you'd like to know more about this particular distinction, you can refer to the following article to get the basics: Interpreted language - Wikipedia

MGodby
  • 1,152
  • 6
  • 11