Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Build

Connect multiple C++ Files

Here is how to connect multiple c++ file:

PreviousBuild ProcessNextPre-Processors

Last updated 9 months ago

Here is a code example using multiple files:

// File: math_operations.h
#ifndef MATH_OPERATIONS_H
#define MATH_OPERATIONS_H

// Function declarations
int add(int a, int b);
int subtract(int a, int b);

// Variable declaration with external linkage
extern int globalValue;

#endif // MATH_OPERATIONS_H
// File: math_operations.cpp
#include "math_operations.h"

// Function definitions
int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

// Variable definition
int globalValue = 10;
// File: main.cpp
#include <iostream>
#include "math_operations.h"

int main() {
    std::cout << "Sum: " << add(5, 3) << std::endl;
    std::cout << "Difference: " << subtract(10, 4) << std::endl;
    std::cout << "Global value: " << globalValue << std::endl;
    return 0;
}

Now, let's break down how these files are linked:

  1. Header files and source files:

    • math_operations.h is a header file containing function declarations and an external variable declaration.

    • math_operations.cpp is a source file with the implementations of the functions declared in the header.

    • main.cpp is the main source file that uses the functions defined in math_operations.

  2. #include directive:

    • math_operations.cpp includes math_operations.h to ensure the function implementations match the declarations.

    • main.cpp includes math_operations.h to access the function declarations and the external variable.

  3. External linkage:

    • globalValue is declared with extern in the header, making it accessible across multiple files.

    • It's defined in math_operations.cpp and can be used in main.cpp.

  4. Function declarations and definitions:

    • Functions are declared in math_operations.h and defined in math_operations.cpp.

    • This allows main.cpp to use these functions by including only the header file.

To compile and link these files, you would use a command like:

g++ main.cpp math_operations.cpp -o program

This compiles both source files and links them together into an executable named "program".

The #ifndef, #define, and #endif in the header file create an "include guard" to prevent multiple inclusions of the same header, which can cause compilation errors.

🔧
1KB
linker_example.zip
archive
Sample Code for linker using g++