I'm just starting to learn C, and I wondered:
- What is the difference between a compiled filename and a source filename?
For instance, why would I want to have a compiled filename cat and a source filename cat.c?
CodePudding user response:
The name of the executable file can be anything, as long as the operating system is okay with it.
By default, some build tools name the executable file a.out
. In many common Unix tools, this is easily changed with the command-line switch -o name
, which says to put the output file in a file named name
.
Executable files automatically become commands because, when a command is typed in a command-line shell and it is not a command built into the shell, the shell looks for a file with that name and, if it is executable, it executes the file. When making use of this, you would like to have the file name be meaningful to the user. a.out
is not generic and not particularly meaningful, so programs are usually given other names.
For simple programs, the name of the sole source file or the primary source file is often used as the name of the executable file (or vice-versa) except that the source file as an extension designating its programming language (like .c
) and the executable file either has no extension or has some extension like .exe
(for “executable”).
Command-line shells might look only in certain directories for executable files. Often they can be told to search in the current directory, but that is not a good idea because it makes it too easy to run one program by accident when another is intended. In particular, an attacker might be able to get a person or program to run a program that the attacker controls if the shell is lax about where it searches for executable files. Because of this, you usually need to run programs in the current directory using ./name
, which indicates you deliberately want to run a program in the current directory.
Many people designate a directory in their home folder where they put executable files and add that directory to the path their shell searches for executables.
CodePudding user response:
A programming language is either 'interpreted' or 'compiled' (or both).
An interpreted language is like Python: you can type in individual expressions and they will be evaluated when you press enter.
C and Java are both compiled languages: You cannot enter individual commands. Rather, you have to "compile" the code into an executable, binary format first.
By convention, if we have a C program that we want to call prog
, we will have our C source code in prog.c
, and compile our executable to the file prog
.
In case you are wondering how to compile a C program, gcc -o prog prog.c
is good to start with. Feel free to comment if you have questions about compiling.
CodePudding user response:
In compiled languages like C and C , you can only run a compiled file. In these languages, you cannot run a source file. You compile the text-based source file into the binary compiled file (executable) so you can run the program.
You can name either file almost anything you want, but they must remain separate and distinct files, since one is generated from the other.