Table of Content#
- Choosing a Text Editor or IDE
- Installing a Compiler
- GCC (GNU Compiler Collection)
- Clang
- Microsoft Visual C++ Compiler (Windows Only)
- Setting up the Environment Variables (Windows and Linux)
- Writing and Compiling Your First C++ Program
- Common Practices and Best Practices
- Using Version Control (e.g., Git)
- Code Formatting and Style Guides
- Testing Your Code
- Example Usage
- References
1. Choosing a Text Editor or IDE#
Text Editors#
- Visual Studio Code: A lightweight, cross - platform text editor with excellent C++ support. It has a vast ecosystem of extensions for features like code completion, debugging, and more.
- Sublime Text: Known for its speed and simplicity. It can be customized with plugins to enhance C++ development.
Integrated Development Environments (IDEs)#
- Microsoft Visual Studio (Windows): A full - fledged IDE with powerful debugging, code analysis, and project management features for C++ development on Windows.
- Xcode (macOS): The official IDE for macOS and iOS development. It has good support for C++ projects.
- CLion (Cross - Platform): Developed by JetBrains, it offers a rich set of features like intelligent code completion, refactoring, and debugging for C++ development.
2. Installing a Compiler#
GCC (GNU Compiler Collection)#
- Linux:
- Most Linux distributions come with GCC pre - installed. You can check the version by running
gcc --versionin the terminal. - If not installed, on Debian - based systems (e.g., Ubuntu), you can install it using
sudo apt-get install build-essential. This will install GCC along with other build tools.
- Most Linux distributions come with GCC pre - installed. You can check the version by running
- macOS:
- You can install GCC using Homebrew. First, install Homebrew if not already installed. Then run
brew install gcc.
- You can install GCC using Homebrew. First, install Homebrew if not already installed. Then run
- Windows:
- You can use MinGW (Minimalist GNU for Windows). Download the MinGW installer from the official website. During the installation, select the
gccpackage.
- You can use MinGW (Minimalist GNU for Windows). Download the MinGW installer from the official website. During the installation, select the
Clang#
- Linux and macOS:
- Clang is often included in the system's package manager. On Ubuntu, you can install it with
sudo apt-get install clang. On macOS with Homebrew, runbrew install llvm(Clang is part of the LLVM project).
- Clang is often included in the system's package manager. On Ubuntu, you can install it with
- Windows:
- You can download the pre - built binaries from the LLVM website and set up the environment variables (similar to GCC on Windows).
Microsoft Visual C++ Compiler (Windows Only)#
- If you are using Microsoft Visual Studio, the Visual C++ compiler is included. You can also install the Build Tools for Visual Studio separately from the Visual Studio installer.
3. Setting up the Environment Variables (Windows and Linux)#
Windows#
- For MinGW (GCC on Windows):
- After installing MinGW, add the
bindirectory (e.g.,C:\MinGW\bin) to thePATHenvironment variable. - Right - click on
This PC(orMy Computer), go toProperties->Advanced system settings->Environment Variables. - In the
System Variablessection, find thePATHvariable, clickEdit, and add the MinGWbindirectory path.
- After installing MinGW, add the
Linux#
- For GCC or Clang:
- The compiler's binary paths are usually already in the
PATHif installed via the package manager. But if you have installed from source or in a non - standard location, you may need to add the path to thePATHvariable. - You can edit the
~/.bashrc(for Bash shell) or the appropriate shell configuration file. For example, if your compiler is in/usr/local/bin, add the lineexport PATH=$PATH:/usr/local/binto the shell configuration file and then runsource ~/.bashrcto apply the changes.
- The compiler's binary paths are usually already in the
4. Writing and Compiling Your First C++ Program#
Writing the Code#
Open your chosen text editor or IDE and create a new file with a .cpp extension (e.g., hello.cpp).
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}Compiling the Code#
- Using GCC:
- In the terminal (navigate to the directory containing
hello.cpp), rung++ hello.cpp -o hello(the-ooption is used to specify the output executable name).
- In the terminal (navigate to the directory containing
- Using Clang:
- Run
clang++ hello.cpp -o helloin the terminal.
- Run
- Using Microsoft Visual C++ Compiler (in Visual Studio):
- Create a new Console Application project. Add the
hello.cppcode to themain.cpp(or equivalent) file. Then build the project (usually by pressingF7or using theBuildmenu).
- Create a new Console Application project. Add the
5. Common Practices and Best Practices#
Using Version Control (e.g., Git)#
- Initialize a Git repository in your project directory using
git init. - Commit your code regularly. For example,
git add.(add all changes) andgit commit -m "Initial commit of hello world program". - You can also push your code to a remote repository (e.g., GitHub, GitLab) for backup and collaboration.
Code Formatting and Style Guides#
- Follow a C++ style guide like the Google C++ Style Guide or the C++ Core Guidelines.
- Many IDEs and text editors have plugins or built - in features to format code according to a style guide. For example, Visual Studio Code has the
Format Documentcommand (usuallyCtrl+Shift+IorCmd+Shift+Ion macOS) which can be configured to use a specific style.
Testing Your Code#
- Write unit tests for your functions. You can use testing frameworks like Google Test.
- For example, to test a simple function:
// mymath.cpp
int add(int a, int b) {
return a + b;
}
// test_mymath.cpp
#include <gtest/gtest.h>
#include "mymath.cpp"
TEST(AddTest, PositiveNumbers) {
EXPECT_EQ(add(2, 3), 5);
}Compile and run the tests using the appropriate build commands for your compiler and testing framework.
6. Example Usage#
Let's say you are developing a simple calculator program.
Code#
#include <iostream>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int divide(int a, int b) {
if (b == 0) {
std::cerr << "Error: Division by zero" << std::endl;
return 0;
}
return a / b;
}
int main() {
int num1, num2;
char operation;
std::cout << "Enter first number: ";
std::cin >> num1;
std::cout << "Enter operation (+, -, *, /): ";
std::cin >> operation;
std::cout << "Enter second number: ";
std::cin >> num2;
int result;
switch (operation) {
case '+':
result = add(num1, num2);
break;
case '-':
result = subtract(num1, num2);
break;
case '*':
result = multiply(num1, num2);
break;
case '/':
result = divide(num1, num2);
break;
default:
std::cerr << "Invalid operation" << std::endl;
return 1;
}
std::cout << "Result: " << result << std::endl;
return 0;
}Compilation#
Using GCC: g++ calculator.cpp -o calculator