DEV Community

Cover image for C - Environment Configuration(VS Code, macOS)
Ethan Gustafson
Ethan Gustafson

Posted on

C - Environment Configuration(VS Code, macOS)

Table of Contents:


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:

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:

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 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
  2. The C/C++ Intellisense extension

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:

#include <stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Before you begin the next sections of writing the json files, please read the tasks.json/launch.json 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

Next, scroll down to the IntelliSense mode:

IntelliSense

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

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

  • 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

  • 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:

Top comments (0)