C - Environment Configuration(VS Code, macOS)
Tags: c, ruby
Published: 2020-09-30 00:07:21

## **Table of Contents:**

* [Program Execution Process](#program-execution-process)
* [The Linking Process](#linker)
* [Environment Configuration With VS Code/Clang](#configuring-environment-for-c-on-macos-with-vs-code-amp-clang)
* [Running Code In The Terminal](#the-terminal)
* [Visual Studio Code](#visual-studio-code)

---

In this blog, I will walk through configuring your environment to function with **C** in **VS Code** on **macOS**. 

When you begin configuring your working environment for **C**, you will learn more about computer architecture, program execution, file types, the IDE, and what the compiler will be doing.

## **Program Execution Process**

A computer needs to be told what to do. It needs instructions to follow. The **instruction set** is the bridge between software and hardware. It lives on the CPU. 

The CPU is the brain of the computer. It executes the instructions it receives. The only instructions it can understand are in machine language.

In order for us to send instructions to the CPU in the C programming language, we need to compile source code. Source code is what is written in a text editor using a programming language.

During compilation, the source code is converted into object code. Object code is a portion of machine code. Machine code is a program written in machine language. Machine language is binary.

Resources: 

- [**Instruction Set Architecture**](https://en.wikipedia.org/wiki/Instruction_set_architecture)

### **Linker** 

After the source code has been compiled into object code, your program will go through the _"linking"_ process. 

This is where the compiler will link together object modules and external libraries into your program. It's about gathering all dependencies and linking them together in your code in order to produce an executable file.

It will also display errors if there was a problem linking with your program. For example, it would report an error if a certain library was missing or couldn't be found.

The final result of the linking process will be an executable file. In Windows, executable files end with `.exe`. Unix won't have that file extension but the program can be executed with the path to your executable.

Resources: 

- [**Linker**](https://en.wikipedia.org/wiki/Linker_(computing))

## **Configuring environment for C on macOS with VS Code & Clang**

### **The Terminal**

If you would like to run your code without an IDE on macOS, you will only need a Text Editor, Compiler, and a Terminal.

If you already have [XCode](https://apps.apple.com/us/app/xcode/id497799835?mt=12) installed, you will have a C compiler. If not, you can install clang like this: `xcode-select --install`

To create and run a simple program:

1. Create a simple `hello_world.c` file
2. Running `gcc hello_world.c` or `/usr/bin/clang hello_world.c` will compile the file
3. Run the executable with `./hello_world`

### **Visual Studio Code**

It is easier to use an Integrated Development Environment (IDE) instead. You will have to install 2 things:

1. [Visual Studio Code](https://code.visualstudio.com/docs/setup/mac)
2. The [C/C++ Intellisense extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) 

Then we will begin configuring the VS Code IDE. We will have to create a few `.json` files through VS Code.

#### The Source Code

For this configuration walkthrough, we will be creating a simple 'Hello World' program. Create a new project directory and name it however you wish. Open the directory in VS Code.

I created another `Hello_World` directory inside of the project folder, but you don't need to. Create a `Hello_World.c` file inside of the `Hello_World`. Place the following code inside of it:

```c
#include <stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}
```

**Before you begin the next sections of writing the `json` files, please read the [`tasks.json`/`launch.json` Variables Reference](https://code.visualstudio.com/docs/editor/variables-reference)**

#### The Compiler Path (`c_cpp_properties.json`)

In order to ensure that the IDE can find the compiler, we have to set up a `c_cpp_properties.json` file to link the compiler path.

To do that, open the `Command Palette` either by going to the `view` tab at the top or by running `Cmd + Shift + P`. Search for `c/c++` and find `Edit configurations UI`. Mac will most likely have found the compiler path for you, but if not it will be located in `/usr/bin`.

![Compiler Path](https://drive.google.com/uc?export=view&id=1F89_wXuEezZfv4GFQQtDPuVI7zrxb1eS)

Next, scroll down to the IntelliSense mode:

![IntelliSense](https://drive.google.com/uc?export=view&id=1-N8DXDdsr8VWNI9IX7AmqOcHg24e21AK)

Make sure you have the right mode selected. It should be `clang-x64`.

Once you have finished settings those, it will create the `c_cpp_properties.json` file:

![c_cpp_properties.json](https://drive.google.com/uc?export=view&id=13KhHHvBnCJwQJG49ThnqWSPBAK1P2fax)

#### The Compiler Build Settings (`tasks.json`)

Each and every time you begin a new program, you have to configure these build settings in a project's root directory. They are specific to your program.

Enter the command palette and search for `tasks`. Find `Configure Default Build Task`. Choose `Create tasks.json from template file`, then select `Others`.

![tasks.json](https://drive.google.com/uc?export=view&id=1fkBLrZ_RBK-yPys0WhQTI5-eLPQIrX83)

- The `command` will be `"command": "/usr/bin/clang"`
- The `"args"` will have a couple of options: `-g`, which is the compilation command for global, `-o`, which will generate an object file, after that we name the executable as `"${file}"`, which is the currently opened file, then after that, we specify the source files.
- The `"group"` will specify `"kind": "build"` & `"isDefault": true`. `"kind"` specifies that the task will run when you run `cmd + shift + B`.

#### Debugging Settings (`launch.json`)

`launch.json` is used to set up the debugging settings. To create the `launch.json` file, enter the command palette, and search for `launch`. Select `"Debug: Open launch.json"`. Select the C++ (GDB/LLDB) environment.

The only two things we will change are the `program` and `stopAtEntry`.

![launch.json](https://drive.google.com/uc?export=view&id=1QnIDra7VhY319JyoQDrsA7QmkRDhMtpH)

- `program` will specify the executable we want to run.
- `stopAtEntry` will be set to `true`.

#### Compiling/Executing The Program

To compile your program, run `cmd + shift + B`. This will create the executable and the debugging folder called `HelloWorld.dSYM`.

To execute the program, simply run the path to the executable. Since my program executable is inside of another directory of my project directory, I have to run the program as `Hello_World/HelloWorld`

Resources:

- [**VS Code Clang Walkthrough**](https://code.visualstudio.com/docs/cpp/config-clang-mac)
- [**Clang Compiler Wiki**](https://en.wikipedia.org/wiki/Clang)
- [**clang.llvm.org**](https://clang.llvm.org/)
- [**LLVM Wiki**](https://en.wikipedia.org/wiki/LLVM)
- [**llvm.org**](https://llvm.org/)
- [**gcc.gnu.org**](https://gcc.gnu.org/)
- [**GNU Compiler Collection Wiki**](https://en.wikipedia.org/wiki/GNU_Compiler_Collection)