Muthaiya
Muthaiya

Reputation: 51

Running a C program in Linux

Can someone explain to me why, in particular, we are using ./a.out to run a program?

Is there any meaning behind this?

Can someone please provide an explanation?

Upvotes: 5

Views: 10827

Answers (5)

dianovich
dianovich

Reputation: 2287

It'd be worth you looking a bit more into C and the way that C programs are compiled.

Essentially, your source code is sent to the preprocessor, where directives like #define and #include are loaded (e.g. into memory). So any libraries you want to use are loaded, e.g.

#include <math.h>

will basically 'paste' the contents of math.h into source code at the point at which it is defined.

Once all this stuff has been expanded out, the compiler turns your source code into object code, which is your source in binary code. a.out is the default name for output if you do not specify a build name.

Upvotes: 1

Matt Whitworth
Matt Whitworth

Reputation: 66

When running an executable like a shell like bash the executable must be in your PATH environment variable for bash to locate and run the program.

The ./ prefix is a shorthand way of specifying the full path to the executable, so that bash does not need to the consult the PATH variable (which usually does not contain the current directory) to run it.

[For a.out (short for "assembler output"), it is the default executable output for a compiler like gcc if no output filename is specified.]

Upvotes: 2

Cybercartel
Cybercartel

Reputation: 12592

gcc -o mynewprogram mynewprogram.c

a.out is the default name for the compiler. AFAIK it is because the linking process is skipped and it is not compiled as an object or library.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798646

The name stands for "assembler output", and was (and still is) the default name for the executable generated by the compiler. The reason you need ./ in front of it is because the current directory (.) is not in $PATH therefore the path to the executable must be explicitly given.

Upvotes: 9

Dr McKay
Dr McKay

Reputation: 2568

If you mean the ./ part, it's for safety. Windows by default appends current directory to PATH, which is bad (there's a risk of DLL injection, and so on). If you mean a.out part, it's just a name (which came from name of format a.out), which you can change by modifying gcc -o parameter.

Upvotes: 3

Related Questions