4

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:~$ 
0

3 Answers 3

5

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.)

2

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.

1

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
1
  • 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, 2012 at 19:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .