16

I just switched from Windows to Ubuntu 11.10.

I wrote the following code in C with the text editor and saved it as HelloWorld.c in Documents.

#include <stdio.h>
int main()
{
    printf("Hello World!\n");
    return 0;
}

And I started the Terminal and enter the following commands:

cd Documents

gcc HelloWorld.c

A file called a.out, which, after some search on Google, is the executable. I entered this command:

a.out

But I get

a.out: command not found

Which step did I do wrongly?

  • Do not forget to make it executable, use sudo chmod 777 a.out – Nick Shvelidze Mar 13 '12 at 19:19
  • @NickShvelidze Compilers pretty much always set +x for you; you do not need to chmod the executable produced by a compiler. The only common situation where gcc fails to make it executable is if it's creating the file in a filesystem that doesn't support or allow it (in which case chmod immediately afterwards would fail too). Also, 777 should be avoided. There is no need to make it readable, writable, and executable by all users; if it were necessary to run chmod, then chmod +x a.out would be sufficient. – Eliah Kagan Jul 24 '17 at 3:29
33

Since you're running an executable in the current working directory, you should prefix it with ./. So for your program run it as ./a.out.

Explanation

The terminal searches for executables in $PATH. This is a Unix environment variable that lists directories containing system binaries (such as ls, echo, or gcc). If you call an executable that's not in a $PATH directory (such as a.out), you need to indicate its absolute path in the file system.

In the terminal . is a synonym for the current working directory, thus ./a.out. You could equally well call /home/yihang/Documents/a.out.

  • 1
    To further elaborate on this, the "./" serves as a "right here", command. It's a bit redundant, but useful in the sense that you know you'll be executing something. – Ruben Bakker Mar 7 '12 at 12:04
  • 1
    You may also need chmod u+x a.out to allow the system to execute it. – charlesbridge Mar 7 '12 at 12:13
  • Now I get it with "./". The program runs fine without chmod. Thanks. – yihangho Mar 7 '12 at 13:44
  • 1
    @yihang: Please consider to mark this question as answered, by clicking the gray/green circle on the left side of the answer you like most. – Rafał Cieślak Mar 8 '12 at 13:37
  • @Ruben Bakker: Actually, it's not at all redundant. Unlike Windows, Linux does not default to looking in the current, or default, directory. So, if it's not in the path, you need to specify the directory, whatever it is. – Marty Fried Mar 13 '12 at 21:04
9

When you run commands on Linux it searches all the directories listed in the PATH environment variable, and if it doesn't find the command there then you get the message you've seen.

Typically it looks like this:

PATH=/usr/local/bin:/usr/bin:/bin

That means it will look first in /usr/local/bin. If it doesn't find it there it'll look in /usr/bin, and so on.

In fact, this is very similar on DOS/Windows: there's a variable called %PATH% that does exactly the same thing.

The difference is that, on Windows, the current directory is also searched. Unix considers this bad because a local file (such as malware) can override important system programs accidentally.

If you prefer that though, you can make Linux work the same way by adding . to the path:

PATH=.:$PATH

(That says set PATH to .: plus the existing contents of $PATH.)

It ends up looking something like this (it may be different on your machine):

PATH=.:/usr/local/bin:/usr/bin:/bin

If you'd rather not do that, you can simply run each program by specifying the directory explicitly:

./myprog

or

/home/username/myprog
  • 2
    If you do add . to PATH, always add it last. – muru Oct 20 '16 at 7:11
3

Essentially, the a.out is created by default because you didn't specify a name for the executable. Try this instead:

gcc HelloWorld.c -o HelloWorld

Once you do that, you should be able to invoke it by (as Sunil suggested) prefacing "HelloWorld" with a dot-slash(./):

./HelloWorld

Here's a link to an article that explains a little bit about why the a.out gets created: Writing and Compiling C Programs on Linux.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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