ld: symbol(s) not found for architecture x86_64

Are you encountering the ld: symbol(s) not found for architecture x86_64 error but have no idea about how to solve the issue? Not to worry, we have got your back! Dive into this article to know about the whys and hows of this error and the different ways in which you can resolve the problem.

What does the symbol not found for architecture x86_64 mean?

This error means that the c/ c++ linker cannot find some symbols required by the code when you are trying to link some executable code. In simpler terms, the error means that some portion of the code is missing, which is needed to execute and apply the command. The missing part maybe a variable or function. The code must originate from self-compiled object files or in the form of libraries that are either static or dynamically linked.

Why Does This Error Occur?

The undefined symbols error occurs mainly due to incorrect compiler usage. Usually, the GCC (GNU compiler collection, a set of compilers available in Windows, Linux) is used to compile C files with a dot C extension. Now, however, dot CPP files(i.e., C++ files) are to be compiled, and for doing that, the command G++ has to be used. Using this command, you can file in and get your file compiled successfully. 

You may commonly encounter this error while executing your CPP or C++ files on Mac. Your C family language code has not been linked to its implementation yet, though the header has been imported.

Here’s An Example 

Let the header be 

Importing it in main.c , we get:- 

Compiling it without the implementation of SimpleMath.h, this is the result you get:-

What is the Solution?

The solution is to compile it with SimpleMath.c, like so:-

You can also link the code with system implementation if there is any:-

So now, the SimpleMath.c that you get at the final is:- 

Trying these alternatives may also help:-

The foremost thing to do is to read the whole thing attentively. Look for missing symbols and see whether you identify and understand them. Search for any object files that you missed while writing the function. Adding these inputs may resolve the issue.

It may even be possible that some symbols have been declared but remained undefined. Please check whether you defined every symbol you declared since it may be one of the reasons you are facing this error.

Adding the necessary linker flags can help overcome this problem too. You may require pointing out the location of the libraries to the linker. The order in which the flags are specified is vital, so it should be prioritized to look for symbols from the libraries after the linker has recognized that they may be required.

Take help from the internet to know about the function or variable you cannot decipher so that you understand what part they are enlisted under and work accordingly.

You may be using a dependency of some library. Then again, the addition of appropriate linker flags is of supreme importance so that you can include the dependencies as and when required.

While these steps may fix the error, a better approach and solution to the problem can be adopted after knowing the entire error message, knowing the compiler and linker put to use, including the code, and on what platform the error is to be fixed, like the one mentioned above.

Conclusion

Hence, there is no one formula to end errors like these. You can try to rectify the compiler you are using, i.e., use G++ instead of GCC to solve the symbol not found error.

FAQs  

  1. Q) What is an undefined error?

In C++, An undefined error means that a function or a variable hasn’t been written or created, or a link hasn’t been made against the object code that consists of the missing function or variable.