Ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Explanation of the error “ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1”

This error typically occurs during the compilation and linking process of a program. It indicates that the linker (ld) was unable to find symbols (functions, variables, etc.) that are required by your program for the specified architecture (arm64).

To resolve this error, you need to identify the missing symbols and provide the necessary libraries or source files to the linker.

Example

Let’s say you have a C++ program that includes a header file called “math_functions.h” which contains the declaration of a function called “add_numbers”. In your main program file, you call the “add_numbers” function.

// math_functions.h
  int add_numbers(int a, int b);
// main.cpp
  #include "math_functions.h"
  
  int main() {
      int result = add_numbers(5, 10);
      return 0;
  }

Now, if you encounter the “ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1” error, it means that the linker could not find the definition of the “add_numbers” function.

One possible solution to fix this error is to make sure you have provided the implementation (definition) of the “add_numbers” function. In a separate C++ source file, let’s say “math_functions.cpp”, you need to define the function as follows:

// math_functions.cpp
  #include "math_functions.h"
  
  int add_numbers(int a, int b) {
      return a + b;
  }

Now, you need to compile and link both the “main.cpp” and “math_functions.cpp” files together. Assuming you are using the clang compiler, you can use the following command:

clang main.cpp math_functions.cpp -o program

This command compiles both source files and generates an executable called “program”. If the linker successfully finds the definition of the “add_numbers” function, the error should no longer occur.

Related Post

Leave a comment