VS Code C++: Undefined reference to functions defined in another .cpp file

Summary

The issue of undefined references to functions defined in another .cpp file in a C++ project using VS Code and g++ (MSYS2) arises due to the linker not being able to find the definitions of these functions. This occurs because the compiler is only compiling the main.cpp file and not including the example.cpp file where the functions are defined.

Root Cause

The root cause of this issue is:

  • The tasks.json file is set up to compile the active file only, which in this case is main.cpp.
  • The linker is not able to find the definitions of the functions display and other because they are defined in example.cpp, which is not being compiled.
  • The compiler is not including the example.cpp file in the compilation process, resulting in the linker not being able to resolve the references to the functions.

Why This Happens in Real Systems

This issue can occur in real systems due to:

  • Incomplete compilation: When only a part of the codebase is compiled, the linker may not be able to find all the necessary definitions.
  • Incorrect project configuration: If the project configuration is not set up correctly, the compiler may not include all the necessary files in the compilation process.
  • Missing dependencies: If there are missing dependencies or libraries, the linker may not be able to resolve all the references.

Real-World Impact

The real-world impact of this issue is:

  • Build failures: The project will not build successfully, resulting in a build failure.
  • Linker errors: The linker will report undefined references to functions or variables, making it difficult to debug the issue.
  • Delayed development: The issue can cause delays in development, as it may take time to identify and fix the root cause.

Example or Code

// example.hpp
#pragma once
#include 
class Example {
public:
    void display(int name);
    void other();
};

// example.cpp
#include "example.hpp"
#include 
void Example::display(int name) {
    std::cout << "Value: " << name << std::endl;
}
void Example::other() {
    std::cout << "Other function" << std::endl;
}

// main.cpp
#include "example.hpp"
int main() {
    Example obj;
    obj.display(3);
    obj.other();
    return 0;
}

To fix the issue, the tasks.json file needs to be updated to include all the necessary files in the compilation process.

How Senior Engineers Fix It

Senior engineers fix this issue by:

  • Updating the tasks.json file: To include all the necessary files in the compilation process.
  • Using a build system: Such as CMake or Makefiles to manage the compilation process.
  • Verifying the project configuration: To ensure that all the necessary files and dependencies are included.

Why Juniors Miss It

Juniors may miss this issue because:

  • Lack of understanding: Of the compilation and linking process.
  • Insufficient experience: With project configuration and build systems.
  • Overlooking details: Such as the tasks.json file configuration.