Compilation as a process

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.”
No hay texto alternativo para esta imagen


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:

  • GNU Compiler Collection (GCC): a compiler suite that supports many languages, such as C/C++ and Objective-C/C++.
  • GNU Make: an automation tool for compiling and building applications.
  • GNU Binutils: a suite of binary utility tools, including linker and assembler.
  • GNU Debugger (GDB).
  • GNU Autotools: A build system including Autoconf, Autoheader, Automake and Libtool.
  • GNU Bison: a parser generator (similar to lex and yacc).

Compile/Link a Simple C Program - hello.c

Below is the Hello-world C program hello.c:

No hay texto alternativo para esta imagen

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

No hay texto alternativo para esta imagen

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.




要查看或添加评论,请登录

Alejandro Reginensi的更多文章

社区洞察

其他会员也浏览了