Compile
Use the C Compiler in the command-line to make executable binary files from the .c
scripts (which are plain text).
The output file is named a.out
by default, meaning assembler output. You can set an argument to get a custom name - usually the name your app with no extension, e.g. helloworld
.
You can add the compiled file to version control, but is no generally the case. A compiled file will only run on the same OS and archtitecture, so often C projects will provide a Makefile
command, so that a machine can run make
to compile all files.
Sometimes, you might choose to compile several versions of your application (such as for macOS, Linux and Windows) using C (or another language like Go or Rust). And then distributed those multiple pre-built files on GitHub Releases or for access to download on your website.
Linux and macOS
GCC vs CC Compiler
Use either cc
or gcc
commands in bash. gcc
is the GNU C Compiler from the GCC (GNU Compiler Collection).
- According to this forum discussion, they can be used interchangeably.
βIn Ubuntu (and most other Linux distributions I guess) cc is gcc.β
- Difference between GCC and CC Compiler | Difference Between
CC is the name given to the UNIX Compiler Command. It is used as the default compiler command for your operating system and also is executable with the same command. GCC, on the other hand, is the GNU Compiler operating system. On systems that run on GNU and Linux, it is common to find the CC being a link so that the scripts can use either compiler interchangeably and easily.
There are various differences observed as regards to using the GNU compiler collection and the CC compiler. These differences can generally be grouped into two main groups. One of these is more specific while the other group is more generic.
How to compile and execute
$ gcc --help
Usage: gcc [options] file...
Options:
...
-o <file> Place the output into <file>.
...
Using the script hello.c
, we create an executable named hello
(no extension) using the -o
flag.
$ cc -o hello hello.c
$ gcc -o hello hello.c
Then execute the compiled file:
$ ./hello
Without specifying the output filename with -o
, the default behaviour is to create a.out
file, meaning assembler output.
$ gcc hello.c
$ ./a.out
Windows
How to compile and run
Based on tutorial
Compile the script.
> cl hello.c
That will create hello.exe
.
Run it like this:
> hello