Compilation as a process
First, I'd like to make a brief introduction onto why and when C was created.
According to the Bell Labs paper The Development of the C Language by Dennis Ritchie, “The C programming language was devised in the early 1970s as a system implementation language for the nascent Unix operating system. Derived from the typeless language BCPL, it evolved a type structure; created on a tiny machine as a tool to improve a meager programming environment.”
The development of C was to become the basis for Unix. According to the Bell Labs paper, “By early 1973, the essentials of modern C were complete. The language and compiler were strong enough to permit us to rewrite the Unix kernel for the PDP-11 in C during the summer of the year.”
The compilation process
GCC is a key component of so-called "GNU Toolchain", for developing applications and writing operating systems. The GNU Toolchain includes:
Compile/Link a Simple C Program - hello.c
Below is the Hello-world C program hello.c:
To compile the hello.c:
> gcc hello.c
// Compile and link source file hello.c into executable a.exe (Windows) or a (Unixes)
The default output executable is called "a.exe" (Windows) or "a.out" (Unixes and Mac OS X).
领英推荐
To run the program:
// (Windows) In CMD shell
> a
// (Unixes / Mac OS X) In Bash Shell - include the current path (./)
$ chmod a+x a.out
$ ./a.out
To specify the output filename, use -o option:
// (Windows) In CMD shell
> gcc -o hello.exe hello.c
// Compile and link source file hello.c into executable hello.exe
> hello
// Execute hello.exe under CMD shell
// (Unixes / Mac OS X) In Bash shell
$ gcc -o hello hello.c
$ chmod a+x hello
$ ./hello
Compile/Link a Simple C++ Program - hello.cpp
You need to use g++ to compile C++ program, as follows. We use the -o option to specify the output file name.
// (Windows) In CMD shell
> g++ -o hello.exe hello.cpp
// Compile and link source hello.cpp into executable hello.exe
> hello
// Execute under CMD shell
// (Unixes / Mac OS X) In Bash shell
$ g++ -o hello hello.cpp
$ chmod a+x hello
$ ./hello
There is a lot more to explore from this, I hope it was helpful for begginers that are interested in progamming with C.
Thank you for reading.