Skip to content

Latest commit

 

History

History
422 lines (322 loc) · 9.28 KB

commandlines.md

File metadata and controls

422 lines (322 loc) · 9.28 KB

Command lines for C, C++, and C# Programming for Beginners

Chapter 1 - Introducing C, C++, and C#

Installing the GCC and Clang compilers on Windows

Update packages:

pacman -Syu

Install GCC:

pacman -S mingw-w64-ucrt-x86_64-gcc

Verify installation of GCC:

gcc --version

Install Clang:

pacman -S mingw-w64-clang-x86_64-clang

Verify installation of Clang:

clang --version

Installing the .NET SDK for C# on Windows

Verify installation by opening a terminal and running:

dotnet --version

Linux and macOS instructions

For Linux (Ubuntu/Debian), install GCC, Clang, and .NET:

sudo apt update
sudo apt install build-essential clang

For macOS, install Xcode Command Line Tools (includes Clang):

xcode-select --install

For macOS, install GCC (optional, via Homebrew):

brew install gcc

Installing VS Code extensions

Install the C/C++ Extension Pack extension, enter the following in the command prompt:

code --install-extension ms-vscode.cpptools-extension-pack

Using JetBrains IDEs

On macOS, install the Xcode Command Line Tools before using CLion or Rider:

xcode-select --install

On Linux, install GCC, G++, GDB, and CMake using your distribution’s package manager. On Ubuntu or Debian, enter the following:

sudo apt update
sudo apt install build-essential gdb cmake

Trying out a simple C program

Change to the Chapter01 directory. For example, on Windows:

cd c:/c-cpp-cs/Chapter01

Compile the C code file using the GCC compiler and output a file named hello.exe:

gcc -std=c17 hello.c -o hello

Run the executable that is now in the current directory:

./hello

Trying out a simple C++ program

Change to the Chapter01 directory. For example, on Windows:

cd c:/c-cpp-cs/Chapter01

Compile the C++ code file using the Microsoft C++ compiler and output a file named hello.exe:

cl hello.cpp

Run the executable that is now in the current directory:

hello

Trying out the C++ program on macOS or Linux

Change to the Chapter01 directory:

cd ~/c-cpp-cs/Chapter01

Compile the program:

g++ -std=c++20 -Wall -Wextra -pedantic hello.cpp

Run the program:

./hello

Compiling with clang++

Change to the Chapter01 directory:

cd ~/c-cpp-cs/Chapter01

Compile the program:

clang++ -std=c++20 -Wall -Wextra -pedantic hello.cpp -o hello

Run the program:

./hello

Trying out a simple C# program

Change to the Chapter01 directory. For example, on Windows:

cd c:/c-cpp-cs/Chapter01

Compile and run the C# code file using the dotnet CLI and its Roslyn C# compiler:

dotnet hello.cs

Chapter 3 - C Fundamentals

Compiling and running a C program

Start UCRT64, and then in your terminal:

gcc -std=c17 hello.c -o hello

On Linux, macOS, or Windows (MSYS2 terminal):

./hello

Understanding compiler switches

When you first compiled a C program, you used a command like this:

gcc hello.c -o hello

In the real world, you are more likely to use a more complex command like this:

gcc -std=c17 -Wall -Wextra -pedantic hello.c -o hello

The -std=c17 switch

Once you are comfortable with the examples in this book, you can experiment with newer standards such as C23 by changing the compiler switch:

gcc -std=c23 -Wall -Wextra -pedantic main.c -o hello

The -o switch

Using -o makes the command clearer because you choose the name yourself. For example:

gcc main.c -o greeting

This creates a program named greeting.

Compiling more than one source file

The same switches work when your project has multiple .c files. For example:

gcc -std=c17 -Wall -Wextra -pedantic main.c game.c input.c -o guessing_game

Compiling the project

In the terminal, use:

gcc main.c game.c input.c -o guessing_game

To run the game’s executable:

./guessing_game

Chapter 4 - C Memory Management

Detecting memory leaks using tools

Typical usage:

valgrind ./program

Chapter 9 - C# Fundamentals

Compiling and running a C# program

You can create and run a console project using the .NET CLI:

dotnet new console
dotnet run

How the project file controls compilation

When you run this command:

dotnet build

the .NET SDK reads the .csproj file and uses it to decide how to compile the project.

Adding package references

If you add a package using the .NET CLI:

dotnet add package Newtonsoft.Json

the project file is updated to include a PackageReference element in an ItemGroup:

<ItemGroup>
  <PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

Creating, building, and running an example project

Create a new folder and a new console project:

mkdir ProjectFileExample
cd ProjectFileExample
dotnet new console

Build the project:

dotnet build

Now run the project:

dotnet run --no-build

The --no-build option tells the .NET CLI to run the already compiled version of the project. If you omit this option, dotnet run checks whether the project needs to be built first, and builds it first if needed.

Chapter 13 - C# High-Performance Memory and Diagnostics

Starting with lightweight monitoring

List running .NET processes:

dotnet-counters ps

Then monitor a process:

dotnet-counters monitor --process-id 12345 System.Runtime

Replace 12345 with the process ID of your application.

Capturing GC heap information with dotnet-gcdump

Install the tool if needed:

dotnet tool install --global dotnet-gcdump

List .NET processes:

dotnet-gcdump ps

Then collect a GC dump:

dotnet-gcdump collect --process-id 12345 --output app.gcdump

Capturing and analyzing dumps with dotnet-dump

Install the tool if needed:

dotnet tool install --global dotnet-dump

List processes:

dotnet-dump ps

Collect a dump:

dotnet-dump collect --process-id 12345 --output app.dmp

Analyze it:

dotnet-dump analyze app.dmp

Inside the analysis session, you can use commands to inspect managed heap information. For example:

dumpheap -stat

Chapter 15 - Building the Puzzle Arcade Shell

Creating the Blazor WebAssembly project

Create the project in Visual Studio from a command prompt or terminal starting in the c-cpp-cs folder:

mkdir Chapter15
cd Chapter15
dotnet new slnx -n Chapter15
dotnet new blazorwasm -n PuzzleArcade
dotnet sln add PuzzleArcade

Start the PuzzleArcade project from a command prompt or terminal:

cd PuzzleArcade
dotnet run