Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have some question about compiling .cpp inside a folder, it says

first.cpp No such file or directory

but when I run it in home it compiles. Any settings I might have left out? Thanks.

Here's the Terminal output (view it bigger): enter image description here

law@ubuntu:~$ ls
android-sdk   Downloads         Pictures        test        wallpapers
android-sdks  eclipse           Public          tomcat      workspace
Desktop       examples.desktop  sourcecodescpp  Ubuntu One
Documents     Music             Templates       Videos
law@ubuntu:~$ dir sourcecodescpp
first.cpp
law@ubuntu:~$ g++ first.cpp
g++: error: first.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
law@ubuntu:~$ ./a/out
bash: ./a/out: No such file or directory
law@ubuntu:~$ ls
android-sdk   Downloads         Pictures        test        wallpapers
android-sdks  eclipse           Public          tomcat      workspace
Desktop       examples.desktop  sourcecodescpp  Ubuntu One
Documents     Music             Templates       Videos
law@ubuntu:~$ dir ls
dir: cannot access ls: No such file or directory
law@ubuntu:~$ ls
android-sdk   Downloads         Music           Templates   Videos
android-sdks  eclipse           Pictures        test        wallpapers
Desktop       examples.desktop  Public          tomcat      workspace
Documents     first.cpp         sourcecodescpp  Ubuntu One
law@ubuntu:~$ g++ first.cpp
law@ubuntu:~$ ./a.out
Hello World!law@ubuntu:~$ ^C
law@ubuntu:~$ ^C
law@ubuntu:~$ 
share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

You were in your home folder (/home/law) when you issued the g++ first.cpp command. But first.cpp is located in the sourcecodescpp folder. To compile first.cpp, you must either enter the folder that contains it:

cd sourcecodescpp

(Then the output file will, by default, be created inside sourcecodescpp.)

Or qualify its name with the name of the folder that contains it:

g++ sourcecodescpp/first.cpp

(Then the output file will, by default, be created inside your home folder.)

share|improve this answer
add comment

You need to move (change directory, or cd) into the sourcecodescpp dirctory first, to run/compile any files in there. For that please run

cd sourcecodescpp

from the Terminal and now you'll be in that directory.

Now you can run

g++ first.cpp

to compile and

./a.out

to run.

share|improve this answer
add comment

That's right, you can give full path to your cpp file to avoid such error.

I think even following should work:

g++ /home/yourdirectory/first.cpp
share|improve this answer
    
g++ /home/<username>/yourdirectory/first.cpp should be used in his case. Or rather g++ ~/yourdirectory/first.cpp Where ~ is a replacement for the path to user's home directory. –  saji89 May 26 '12 at 19:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.