Command lines for C, C++, and C# Programming for Beginners
- Chapter 1 - Introducing C, C++, and C#
- Chapter 3 - C Fundamentals
- Chapter 4 - C Memory Management
- Chapter 9 - C# Fundamentals
- Chapter 13 - C# High-Performance Memory and Diagnostics
- Chapter 15 - Building the Puzzle Arcade Shell
Update packages:
pacman -SyuInstall GCC:
pacman -S mingw-w64-ucrt-x86_64-gccVerify installation of GCC:
gcc --versionInstall Clang:
pacman -S mingw-w64-clang-x86_64-clangVerify installation of Clang:
clang --versionVerify installation by opening a terminal and running:
dotnet --versionFor Linux (Ubuntu/Debian), install GCC, Clang, and .NET:
sudo apt updatesudo apt install build-essential clangFor macOS, install Xcode Command Line Tools (includes Clang):
xcode-select --installFor macOS, install GCC (optional, via Homebrew):
brew install gccInstall the C/C++ Extension Pack extension, enter the following in the command prompt:
code --install-extension ms-vscode.cpptools-extension-packOn macOS, install the Xcode Command Line Tools before using CLion or Rider:
xcode-select --installOn Linux, install GCC, G++, GDB, and CMake using your distribution’s package manager. On Ubuntu or Debian, enter the following:
sudo apt updatesudo apt install build-essential gdb cmakeChange to the Chapter01 directory. For example, on Windows:
cd c:/c-cpp-cs/Chapter01Compile the C code file using the GCC compiler and output a file named hello.exe:
gcc -std=c17 hello.c -o helloRun the executable that is now in the current directory:
./helloChange to the Chapter01 directory. For example, on Windows:
cd c:/c-cpp-cs/Chapter01Compile the C++ code file using the Microsoft C++ compiler and output a file named hello.exe:
cl hello.cppRun the executable that is now in the current directory:
helloChange to the Chapter01 directory:
cd ~/c-cpp-cs/Chapter01Compile the program:
g++ -std=c++20 -Wall -Wextra -pedantic hello.cppRun the program:
./helloChange to the Chapter01 directory:
cd ~/c-cpp-cs/Chapter01Compile the program:
clang++ -std=c++20 -Wall -Wextra -pedantic hello.cpp -o helloRun the program:
./helloChange to the Chapter01 directory. For example, on Windows:
cd c:/c-cpp-cs/Chapter01Compile and run the C# code file using the dotnet CLI and its Roslyn C# compiler:
dotnet hello.csStart UCRT64, and then in your terminal:
gcc -std=c17 hello.c -o helloOn Linux, macOS, or Windows (MSYS2 terminal):
./helloWhen you first compiled a C program, you used a command like this:
gcc hello.c -o helloIn the real world, you are more likely to use a more complex command like this:
gcc -std=c17 -Wall -Wextra -pedantic hello.c -o helloOnce 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 helloUsing -o makes the command clearer because you choose the name yourself.
For example:
gcc main.c -o greetingThis creates a program named greeting.
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_gameIn the terminal, use:
gcc main.c game.c input.c -o guessing_gameTo run the game’s executable:
./guessing_gameTypical usage:
valgrind ./programYou can create and run a console project using the .NET CLI:
dotnet new consoledotnet runWhen you run this command:
dotnet buildthe .NET SDK reads the .csproj file and uses it to decide how to compile the project.
If you add a package using the .NET CLI:
dotnet add package Newtonsoft.Jsonthe project file is updated to include a PackageReference element in an ItemGroup:
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>Create a new folder and a new console project:
mkdir ProjectFileExamplecd ProjectFileExampledotnet new consoleBuild the project:
dotnet buildNow run the project:
dotnet run --no-buildThe --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.
List running .NET processes:
dotnet-counters psThen monitor a process:
dotnet-counters monitor --process-id 12345 System.RuntimeReplace 12345 with the process ID of your application.
Install the tool if needed:
dotnet tool install --global dotnet-gcdumpList .NET processes:
dotnet-gcdump psThen collect a GC dump:
dotnet-gcdump collect --process-id 12345 --output app.gcdumpInstall the tool if needed:
dotnet tool install --global dotnet-dumpList processes:
dotnet-dump psCollect a dump:
dotnet-dump collect --process-id 12345 --output app.dmpAnalyze it:
dotnet-dump analyze app.dmpInside the analysis session, you can use commands to inspect managed heap information. For example:
dumpheap -statCreate the project in Visual Studio from a command prompt or terminal starting in the c-cpp-cs folder:
mkdir Chapter15cd Chapter15dotnet new slnx -n Chapter15dotnet new blazorwasm -n PuzzleArcadedotnet sln add PuzzleArcadeStart the PuzzleArcade project from a command prompt or terminal:
cd PuzzleArcadedotnet run