简体   繁体   中英

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?

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.

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.

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

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 (eg into memory). So any libraries you want to use are loaded, eg

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM