TU ACM logo.
blog

Compilers and IDEs for C

We're going to compile our first program!
Posted 15 January 2020 at 1:26 PM
By Joseph Mellor

This is the fourteenth article in the Making Sense of C series. In this article, we're going to discuss text editors, how to use a compiler as part of an IDE, and how to use an IDE.

In the last article, we established that we had four things left to do before we could write our first program:

In this article, we're going to discuss the main components of an IDE:

General Features of an IDE

In this section, we're going to discuss features that are common across pretty much all IDEs. To start, an IDE is a program that can do almost anything you would need to do to write a program. In fact, IDE is short for Integrated Development Environment, which means it's a singular programming environment that allows you to write, compile, run, and debug code within it.

An IDE will try to look like programs you're used to like, Microsoft Word, Excel, or Google Docs. I've made a simplified version of an IDE below.

File  Edit  View  Project  Build  Debug
💾    ↺    ↻    🛠    📂    ▶                                    🔍
Project Explorer
📃 Project
    🖿 src
        🗎 file.c
        🗎 other_file.c
    🖿 includes
        🗎 file.h
file.c
file.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int main(int argc, char ** argv) {
    char * program_name = argv[0];
    if (argc < 3) {
        // TODO: Print Usage Message
        return -1;
    }
    char * file_name = argv[1];
    char * word = argv[2];
    // TODO: Count number of occurrences in a file
    return 0;
}
Output
Console

Compiling src/file.c
Compiling src/other_file.c
Linking...
Build Successful!

If the "image" above looks weird on your phone, turn your phone sideways.

As you can see, the top of the IDE will have tabs like "File" and "View" plus the minimize, maximize, and close buttons, which will work just like they work in almost every other program. Under that, you have a bunch of buttons that do things like save your current file, undo something, redo something, build the project, do stuff with files, run the program, and search through the project for some sort of text. You'll also have a project explorer that will display all the relevant files in the project, which mainly consist of source code files, on the left. To the right of the project explorer, you'll have a text editor with some syntax highlighting. You type your code in the text editor and save it as you would normally expect. Under that, you have at least two tabs: one for the output of building the project (which include which files were compiled and any warnings or errors that showed up) and one for the output of the program itself.

To be clear, most IDEs have the features I've described, though they allow users to do things like move each of the windows around, add and remove tabs, add buttons, etc.

The Project File

Since we want to save information about how to compile our source code and send that information to other people, we need some sort of project file. The project file consists of anything your computer needs to know about to compile your project properly. It includes a list of all the source code files (code that you write), libraries (code that other people have written and compiled for you that you can use in your own projects), and directories you want to include in your project. It also includes information you can tell the compiler about how you want to compile projects. If you're using an IDE, you will go through the settings in the IDE to modify the file. When you compile and link your project, it will go through the project file and figure out what to compile and how to compile it.

While the project file is important under the hood, you as a programmer probably don't need to worry about it directly, though we'll discuss how to make one first by hand then by using other programs. In most IDEs, you'll find the ability to create a new project by going to File (Top Left Corner) > New > Project. You can add source files by right-clicking on the project name in the project explorer (which you can find in either View or Window in the bar at the top of the screen with File and clicking something like Add New File or Add > File).

These project files are often platform specific, so people will use tools like cmake and premake to generate project files for each platform so you don't have to worry about it.

The Compiler

We'll also want some way of compiling the code within the IDE, so we're going to build a compiler into the program or let the user modify the project to use a specific compiler. Either way, we're going to have a button somewhere on the screen that will compile the code and we're probably going to have a tab at the top of the screen titled something like Build.

After compiling a file, the IDE will save the compiled file (known as an object file) so that it doesn't have to recompile the entire project every time. The project file and the compiler will work together to only compile what needs to be compiled, which consists of pretty much any source code file that has changed since it was last compiled or any source code file that doesn't have a corresponding object file. You won't see much of a difference for smaller projects, but larger projects can sometimes take hours to compile from scratch.

All IDEs have four major options to build code:

In general, you want to Build your code unless something weird happened and you need to Rebuild your code. Most IDEs will have a button somewhere that looks like a hammer or some other tool you would use to build something that will allow you to build code. Also, building will often save any unsaved files, but not necessarily.

The Code Executor

Compiling is cool and all, but you need to be able to run your code to make sure it actually works. So somewhere on the screen you'll see a Play button that will run your code. Sometimes, it will also save your unsaved files and build what it needs to build. There's also an option to run the code in the toolbar at the top, but different IDEs have it in different places, but Run is a good start if it exists. There's also a version of the code executor called the debugger that will run the code, but you can tell it to do certain things like watch variables or stop at a certain point in the code so you can look at the entire program step by step and see what's going wrong. It often uses a Bug icon if it's in the button toolbar, but you can also find it anywhere you can run the code. For example, if you have a play button, it could have a drop down menu to the right that will have the option to run the debugger. If you have a Run tab at the top of the IDE, you'll find the ability to run the debugger in that tab.

The Text Editor

You write your code in this window. Text editors have several features, most notably syntax highlighting, auto indentation, some form of autocomplete, an error checking mechanism, line numbers, etc. They also allow you to edit multiple files at once using tabs like tabs in a browser.

The File Manager

The file manager works just like you would expect. It allows you to create files, open files, edit files, etc. It's pretty much contained in the File tab at the top of the screen and the project manager.

The Output Windows

These windows serve as output for different parts of the IDE. You'll always have one for outputting the status of the build, which consists of which files were compiled and any warnings or errors found. You can also have another window that prints out the result of your program.

Other Features

You can often change settings all you want, import plugins, change the background and the colors used in syntax highlighting, etc. I recommend that you should change the background to some form of dark mode or use a theme with a dark background.

Which IDE to Use?

The IDE you use barely matters. They all have the same core features and everything else they have is just a minor feature that might make you develop faster, though some IDEs might be faster than other IDEs. The compiler does matter, however. Most compilers are platform dependent, so gcc won't compile programs for Windows and the Visual C++ compiler will only compile programs for Windows. In short, you need to make sure to pick an IDE that allows you to compile for your system and your target system.

Windows

If you're on Windows, you should either use a cross platform IDE like Eclipse or Netbeans or install some form of Linux, which you can do by setting up the Windows Subsystem for Linux or by installing a virtual machine like Virtual Box and installing Linux on the virtual machine. If you choose to set up either the Windows Subsystem for Linux or install the Virtual Machine with a Linux distro, you can use the terminal to install the gcc compiler (which is necessary for a lot of programs to properly install, so you might as well install it) and you can install mingw to build programs for Windows in C. If you compile something with gcc, then it will work within the Linux part of your computer, which includes the Windows Subsytem for Linux or within the virtual machine. If you compile something with mingw, then it will work on the Windows part of your computer. If you want graphical output, you have to install a virtual machine. If you want to use Eclipse on Windows, someone else has already written an article on setting up Eclipse in Windows for C/C++ development.

I threw a lot at you, so if you feel confused at any point, here's the simplest way to get our basic C programs to compile on Windows:

  1. Set up the Windows Subsystem for Linux using the linked tutorial.
  2. Download VSCode or any other text editor, but if you don't know which one to get, just download VSCode.
  3. Open up the Ubuntu App
  4. In the Ubuntu App, type the command sudo apt install gcc g++ gdb git make to install all the programs after the word install (you probably already have several of them, but it's good to make sure that you have them).
  5. Create a folder somewhere on your computer where you're going to put all your projects. For example, let's say you put it in C:\Users\​[username]\dev, where [username] is your username.
  6. Inside your projects folder, create another folder for a specific project. Since we have to do "Hello, World!", we're going to have the folder C:\Users\​[username]\dev​\hello-world.
  7. Put all your .c and .h files in that folder. We will use build tools like make, cmake, or premake later in this series, but we're keeping it simple for now. Use your text editor to create your files in the folder and write the code. In our case, we're going to create the file C:\Users\​[username]\dev​\hello-world\​hello-world.c and we're going to put the following code in the file.
    1
    2
    3
    4
    5
    6
    #include <stdio.h>
    
    int main(void) {
        puts("Hello, World!");
        return 0;
    }
    
  8. On the command line in the Ubuntu app, type cd /mnt/c/[dir], where [dir] is everything after C: in the path of the project folder, but with all the backslashes replaced with forward slashes. In our case, it's Users/​[username]/dev​/hello-world.
    user@computer:~/dev$ cd /mnt/c/users/[username]/dev/hello-world
    user@computer:/mnt/c/users/[username]/dev/hello-world$
    

From here, compiling on all operating systems is the same and we'll cover it in the section on Compiling on the Command Line. For now, we're going to talk about the other operating systems.

Linux

If you're on Linux, you should probably use a decent text editor like VSCode and the command line for everything else. With VSCode, you can actually open a terminal up inside the program itself. You'll need to install the packages gcc, g++, gdb, git, and make for this tutorial, so sudo [package manager] install gcc g++ gdb git make, where you'll have to use the [package manager] for your distro. The package managers are apt for the Debian family (Ubuntu, Mint, XUbuntu, Debian, etc.), rpm for Fedora, yum for CentOS, zypper/rpm for openSUSE, urpmi for Mandriva, and pacman for Arch. If you don't know, it's most likely apt, then rpm, and then yum, but you can search "[distro] how to install packages" online if you can't figure it out.

MacOS

If you're on Mac, you should probably use XCode or anything that works on Linux. MacOS also has a terminal that works almost exactly like the Linux terminal (both were based of the Unix System), so you can use a decent text editor and the terminal just like in Linux. In the terminal, type the command brew install gcc g++ gdb git make to install the necessary programs to compile in C.

Which IDE Does the Author Use?

I use Linux, so I use gcc in the terminal with a separate text editor and a project file known as a Makefile, which is what you will use if you set up the Windows Subsystem for Linux (Seriously read that tutorial.). The command line also serves as my file manager, code executor, debugger, and everything else. The file manager and the code executor are some of the basic functions of the terminal, while the debugger is a separate command line program, gdb.

Compiling on the Command Line

The command is pretty simple for right now, though we're going to add more to it later.

user@computer:~/dev$ cd [folder with your .c files]
user@computer:~/dev/hello-world/$ gcc [files you want to compile] -o [name of executable]

For example, let's say we want to compile this simple program (don't worry about the specific function, it will be covered in the next article) that will just print out "Hello, World!\n". Furthermore, let's call this file test.c.

1
2
3
4
5
6
#include <stdio.h>

int main(void) {
    puts("Hello, World!");
    return 0;
}

To compile this file, use the command

user@computer:~/dev/hello-world/$ gcc test.c -o hello-world

Then, to execute it, we can use ./ followed by the program name (which shouldn't have any spaces in its name).

user@computer:~/dev/hello-world/$ ./hello-world
Hello, World!
user@computer:~/dev/hello-world/$

If you make changes to the source file test.c, then you will have to recompile using the same exact command you used to compile test.c last time.

user@computer:~/dev/hello-world/$ gcc test.c -o hello-world

Summary

I want to emphasize again that only whether you can compile your code, run your code on the target system, debug your code, and manage your files matters when choosing an IDE. Other than that, most IDEs share probably 95% of the things you would want in an IDE, so the choice is over the 5% of features that could probably make it easier for you to code. For this tutorial, we're going to be focused on using a terminal with an external text editor since

though we will try to indicate where and how to compile programs using other IDEs. If anyone wants to write an article on setting up a specific IDE for C and writing programs in it, contact us at thetuacm@gmail.com and we can talk more about it.

What's Next

Now that we have a way to compile our programs, we're going to finish talking about files in Files in C, Pt. 2. After we finish talking about files, we're going to start using them to write the word counter.

A picture of Joseph Mellor, the author.

Joseph Mellor is a Senior at TU majoring in Physics, Computer Science, and Math. He is also the chief editor of the website and the author of the tumd markdown compiler. If you want to see more of his work, check out his personal website.
Credit to Allison Pennybaker for the picture.