» Quick Introduction to C++ » 2. Advanced » 2.6 Build a Binary

Build a Binary

Input and Output

In C++, you can perform input and output using the Standard Input/Output streams: std::cin for input and std::cout (or std::cerr for errors) for output. These streams are part of the <iostream> header.

#include <iostream>

int main() {
    int userInput;

    // Prompt the user for input
    std::cout << "Enter an integer: ";

    // Read user input from the standard input (keyboard)
    std::cin >> userInput;

    std::cout << "You entered: " << userInput << std::endl;

    return 0;
}

String Input

#include <iostream>
#include <string>

int main() {
    // Declare a string variable to store user input
    std::string userInput;

    // Prompt the user for input
    std::cout << "Enter a string: ";

    // Read user input as a string
    std::getline(std::cin, userInput);

    // Display the entered string
    std::cout << "You entered: " << userInput << std::endl;

    return 0;
}

Program Arguments

In C++, you can take command-line arguments using the argc and argv parameters in the main function. argc is the number of command-line arguments, and argv is an array of strings containing the arguments.

#include <iostream>

int main(int argc, char *argv[]) {
    // Check if there are at least two arguments (including the program name)
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " <name>" << std::endl;
        return 1; // Return an error code
    }

    // The first argument (argv[0]) is the program name
    // The second argument (argv[1]) is the first command-line argument

    std::cout << "Hello, " << argv[1] << "!" << std::endl;

    return 0;
}

Compilation

Compilation is the process of translating human-readable C++ source code into machine-executable code. It involves 4 steps: preprocessing, compiling, assembly and linking.

Preprocessing

The preprocessor scans the source code and handles directives that start with #, such as #include and #define. It includes header files, expands macros, and performs conditional compilation.

Compiling

The preprocessed code is fed into the compiler, which translates it into assembly code or an intermediate representation. The compiler checks the syntax and semantics of the code and generates object files containing machine code.

Assembly

The assembler converts the assembly code into machine code or binary code that the computer's processor can understand. This step produces object files with a ".o" extension.

# Compile main.cpp to object
g++ -c -std=c++11 src/main.cpp -o main.o
# Compile calculator.cpp to object
g++ -c calculator.cpp -o target/calc.o

g++ is GNU C++ Compiler, one of the compiler drivers of the GNU Compiler Collection, aka GCC.

GCC

Learn more about GCC here: https://gcc.gnu.org/

Linking

The linker takes the object files generated during compilation and combines them with any necessary libraries to create the final executable file. It resolves references to functions and variables, assigns memory addresses, and produces an executable file.

# Link objects and create executable
g++ main.o target/calc.o -o bin/myprog

Keep Going. Keep Learning.