Description: https://images.manning.com/360/480/resize/book/e/bec8fa5-11f5-4be8-80de-cea6123940cf/Gookin-MEAP-HI.png

From Tiny C Projects by Dan Gookin

This article discusses the C development cycle.


Take 35% off Tiny C Projects by entering fccgookin into the discount code box at checkout at manning.com.


According to ancient Mesopotamian tablets currently on display in the British Museum, four steps are taken to develop a C language program. These are illustrated in Figure 1, where you can plainly see the C development cycle written in cuneiform.


Figure 1. The C development cycle, courtesy the British Museum.


As a review, and because neither of us knows Babylonian, here is the translation:

  1. Start by creating the source code file.
  2. Compile the source code into object code.
  3. Link in a library to create a program file.
  4. Finally, run the program for testing, disappointment, or delight.

Step 4 is a rather liberal translation on my part. The original reads, “Run the program and rejoice by consuming a cow.” I have also omitted references to pagan deities.

Selecting the prefect source code editor

C language source code is plain text. What makes the file a C source code file and not a boring ol’ text file is the .c filename extension; all C source code files use this filename extension. Eyeball code uses the .see extension. Naval code uses .sea. Know the difference.

It’s important that you find a good text editor to craft your source code. It turns out, this step is a big deal. Programmers wage wars over which text editor is best, not to mention the ongoing feud over tabs versus spaces to indent code.

With battle lines drawn, know that I prefer the vim text editor and I use tabs set to 4 spaces. If this detail is enough to cause you to return the book, so be it. Versions of vim, as well as disgusting text editors like Emacs and Nano, are available that run both at the command prompt (in text mode) and in the windowed GUI environment.

As a tip, the .c filename defines a C language source code filetype. On your computer, use whatever file magic is required to associate.c files with your text editor. This association trick allows you to double-click on a C source code file icon and have it open in the text editor as opposed to whatever-the-hell other program it may be associated with.

As far as I know, file association in Linux is sketchy. I tried to get it to work, but it’s not as effective as it is in Windows. So while you can use the GUI version of your favorite editor in Linux, you can’t just double-click on a file icon and have it magically appear in your favorite editor.

To associate a file in Windows, follow these steps:

  1. Right-click on a C language (.c) source code file.
  2. Choose Open With, then choose your favorite text editor from the submenu.
  3. Laugh at Linux users whose file manager lacks this capability.

After associating C source code files with your favorite editor, you can double-click on any C source code file icon and have the editor open it. Especially in a GUI environment, it’s efficient to have your source code open in one window while you work C compiling and testing in a separate terminal window. You can do so in Linux as well, just without handy file association.

The process of compiling, linking, and building

After writing the source code, you build the program. This process combines two original steps that only a handful of programmers at the Old Coder’s Home remember: compiling and linking. Most code babies today just think of compiling, but linking is still in there somewhere.

Long story short, today the process of creating a program is performed in a single step, build. It combines both compiling and, upon success, linking. Compiling generates object code. Object code is linked with a library to form a program. To save time, and because modern compilers call the linker automatically, the only term used is build.

When your source code survives the compiling and linking process, the next step is to run the program. This step isn’t the last, of course: you test the program for bugs and potentially edit and build again. The entire process, the C program development cycle, is illustrated in Figure 2, which is far more realistic than the Babylonians made it out to be in Figure 1:


Figure 2. The true nature of the program development cycle. (Image courtesy of the California Department of Highway Safety.)


For trivia’s sake, the original C compiler in Unix is called cc. Guess what it stands for?

The Unix linker is named ld. It stands for “link dis.” The ld program still exists on today’s Linux systems. It’s called internally by the compiler – unless the code is riddled with errors, in which case the compiler calls its friend Betsy to giggle about how horrible your C code reads.

Do It at the Command Prompt

Welcome to the early years of computing. It’s nostalgic to edit, build, and run C programs in text mode, but it works well and is quite efficient. You must understand how the command line works, which is something I believe all C programmers should know innately. Truly, it’s rare to find a C coder worthy of the title who lacks a knowledge of text mode programming in Linux.

Accessing the terminal window

Every Linux distro comes with a terminal window. Mac OS X features a terminal program. Even Windows 10 comes with a command shell, though better is to install the Linux subsystem for Windows and use an Ubuntu bash shell for consistency with the other platforms. Never have the times been so good for text mode programming. Crack open a diet Coke and kick off your sandals!

Installing a compiler

If you’re using Linux, Unix, or OS/X, the compiler is already installed. It must be: the compiler is one of the tools the operating system uses to configure itself. Even the Ubuntu shell in Windows 10 features the traditional cc compiler, though I feel you’re better offer installing clang.

A version of clang is also available to run in Windows 10 natively. For example, you could setup clang to work with your favorite Windows IDE. Still, this book is about command line programming, so I recommend getting clang for Windows 10’s Ubuntu shell terminal window.

With many Linux distros you obtain command line packages by using the GUI package manager. These include clang and other command line tools. In Windows 10, as with other Linux distros, you can use the command line package manager to install new software. For Ubuntu Linux in Windows 10, the package manager is named apt, the Advanced Package Tool.

To use apt to install the clang compiler, type the following command in the Ubuntu shell in Windows 10:

  
 sudo apt install clang
  

The sudo command (superuser do) prompts for your login password. The program then downloads and installs the clang compiler. Press the Y key when prompted.

To confirm that the clang compiler is installed, type this command:

  
 clang –v
  

The current clang version and other stuff is output. You’re ready to start coding.

Exploring text screen editors

You can use a GUI editor to write and edit code in one window and then use the terminal window to build and run. This approach is how I code, though I still have a text mode editor handy. Sometimes I just need to run the editor at the prompt, edit, then return to the command prompt.

Many useful editors are installed by default with Linux. Those that aren’t can be obtained from your distro’s package manager. On the Mac, you can use the Homebrew system to add text mode programs that Apple deems unworthy to ship with its operating system; learn more about Homebrew at brew.sh.

My favorite text mode editor is VIM, the improved version of the classic vi editor, that I mentioned earlier. It has both a terminal window version that runs in text mode as well as a full GUI version. The program is available for all popular operating systems.

Another popular text mode editor is Emacs. Like VIM, it’s also available as a text mode editor as well as a GUI editor. I don’t use Emacs, so I am unable to wax eloquent upon its virtues.

Whatever text editor you obtain, ensure that it offers C language color-coding as well as other helpful features like matching pairs: parentheses, brackets, and braces. With many editors, it’s possible to customize features, such as writing a startup script that properly contorts the editor to your liking. For example, I prefer a 4-space tab stop in my code, which I can set by configuring the .vim_rc file in my home directory.

If you desire to install a text-mode editor and one isn’t available, use the Linux package manager to install it. For example, to get vim in the Windows 10 Ubuntu shell, use this command:

  
 sudo apt install vim
  

Or you can substitute vim with your favorite, less-potent editor.

Obtaining libraries

When you link a library, you add its capabilities to your system’s programming powers. For example, add the libcurl library to perform curl commands in your programs, accessing the Internet, using FPT, uploading and downloading, and so on. Libraries come with function definitions and one or more header files to assist you. Documentation is available – the API or Application Programming Interface – that helps you learn how things work and put the library to use.

When you figure out which libraries your code needs, the next step is to obtain the library. This process works just like installing software at the Linux command prompt: Use the distro’s package manager to search for and obtain the required library. For example, libcurl is obtained by using this command:

  
 sudo apt-get libcurl4
  

Running apt-get with superuser privileges fetches and installs the libcurl library.

Once you have the library, follow the API to discover which functions do what and learn the process behind making the library do its thing. Be sure to check the documentation for examples. Don’t expect the text to be warm and fuzzy; these developers are programmers, not writers.

During the build process, the linker needs to know about the library so that it can create the program. It already knows where the standard C library exists, which is how it can build regular C programs. But for another library, the library name must be specified as covered in the preceding section.

My advice is to always run a test program first, such as outputting the library’s current version. Successfully build this program to ensure that the library is installed and linked. Then you’re on your way to using the library and building interesting and potentially useful programs.

That’s all for this article. If you want to learn more about the book, check it out on Manning’s liveBook platform here.