Fundamentals 8 min read

Master macOS C/C++ Development: Install VS Code, Configure, and Run Hello World

This guide walks you through installing VS Code on macOS, configuring essential C/C++ extensions, setting up the Clang/LLVM toolchain, creating a simple Hello World project, compiling, running, and debugging it using VS Code's tasks and launch configurations.

AI Cyberspace
AI Cyberspace
AI Cyberspace
Master macOS C/C++ Development: Install VS Code, Configure, and Run Hello World

Previous List

C Language Quick Guide (0) – C Family History

MacBook Pro Install VS Code

Download installer: https://code.visualstudio.com/Download

Extract and place into Applications.

Configure C/C++ Development Environment

Official docs: https://code.visualstudio.com/docs/cpp/config-clang-mac

Install common C/C++ extensions:

C/C++: language support

C/C++ Clang Command Adapter

C/C++ Extension UI Themes

CMake

CMake Tools

Makefile Tools

Code Runner (for compiling)

CodeLLDB (debug, solves Catalina LLDB issue)

Check Clang/LLVM on macOS.

$ clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Install the code command for CLI usage.

Hello World

1. Create project and source

mkdir /workspace/projects
cd /workspace/projects

mkdir helloworld
cd helloworld
code .

Using code creates a .vscode folder with tasks.json (compiler build settings), launch.json (debugger settings), and c_cpp_properties.json (compiler path and IntelliSense settings).

2. Basic syntax

C file types :

.h – header files for declarations.

.c – C source files.

Key program components :

Tokens – keywords, identifiers, constants, strings, symbols.

printf("Hello, World! 
");

The five tokens are:

printf
(
"Hello, World! 
"
)
;

Preprocessor directives

Comments ( // or /* ... */)

Functions

Statements and expressions

Identifiers

Keywords

Semicolon as line terminator

Whitespace lines ignored by the compiler

3. Example code

#include <iostream>

int main()
{
    for (int i=0; i<3; i++)
    {
        std::cout << "hello world" << std::endl;
    }
    return 0;
}

#include <stdio.h> is a preprocessor directive.

int main() is the entry function.

/*...*/ is a comment.

printf(...) prints "Hello, World!".

return 0 ends main and returns 0.

4. Compile and run

Run to see output.

First run generates tasks.json with build parameters.

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ generate active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": ["$gcc"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Debugger generated task."
        }
    ],
    "version": "2.0.0"
}

Corresponding compile command:

/usr/bin/clang++ -fcolor-diagnostics -fansi-escape-codes -g /workspace/projects/helloworld/helloworld.cpp -o /workspace/projects/helloworld/helloworld

5. Debug

Click line numbers to set breakpoints.

Enter debug view to start debugging.

Custom debug flow can be configured via a launch.json file.

Original launch.json:

{
    "configurations": [
        {
            "name": "C/C++: clang++ generate and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang++ generate active file"
        }
    ],
    "version": "2.0.0"
}

Custom use of CodeLLDB plugin:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

C/C++ configuration

Finally, the UI can set various C/C++ extension options, automatically generating a c_cpp_properties.json file.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingCompilationC languagemacOSVS CodeC++
AI Cyberspace
Written by

AI Cyberspace

AI, big data, cloud computing, and networking.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.