Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C Linker Error: Undefined Symbols

What does it mean to have undefined symbols?

There are no errors in the code files themselves and I am NOT using any external libraries.

I DID add a typedef NS_ENUM prior to this linker error occurring.

Where do I add this -v to see invocation?

Here is the error message:

Undefined symbols for architecture x86_64:
  "_OBJC_IVAR_$_UIViewController._parentViewController", referenced from:
      -[PEPI_LessonController setParentViewController:] in PEPI_LessonController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
like image 208
Guido Anselmi Avatar asked Sep 02 '25 16:09

Guido Anselmi


2 Answers

"Undefined Symbols"

Building source code files to an executable file consist of at least two steps:

  1. Compile the source code files to intermediate binary files (often called xyz.o).
  2. Link the intermediate binary files to the final executable file.

The error message "undefined symbols" is a linker message. It may appear even though the compilation process was successful without notice. The linker organizes final memory address relations and it replaces symbols that the compiler had to assume they would be valid later, if all parts of the code would be available. Without this, no modularization would be possible at all.

-v to see invocation

If you build your application in Xcode, then Xcode calls all the compile and link commands for you (CompileC, Ln, Clang ...). But remember that a typical IDE runs only the commands you could run by yourself in the shell. Theoretically, you could develop big applications only in a text editor and a shell. So I suggest take some time and try to copy paste some commands listed in the Xcode build report to a shell :-) You'll learn a lot about the backgrounds. Therefore, in my opinion, -v to see invocation is used while invoking the command in the shell - or in the build settings, if you wish permanently more information.

"External libraries"

Finally, try to clarify "external libraries". To look at the most simple example: even if you write a simple C program and you want to know something trivial like the length of a string, you'll include <glibc.h>. Now this is an external library. It's external to your program code. Are you sure you haven't included external libraries?

Solving linker problems

Linker errors are often confusing and somehow difficult, because details of the linked modules tend to be out of sight. You may find many hints if you enter the error message in a search engine. For example, have a look at here:

Undefined symbols for architecture armv7: "_SCNetworkReachabilityCreateWithAddress"

Even if all components are found for linking, all paths are known etc, they may have the wrong version or else.

like image 125
peter_the_oak Avatar answered Sep 04 '25 09:09

peter_the_oak


In my case I had forgotten to add the .m file to all the same targets as the .h and that's what caused this issue. In case it helps anyone thought I'd mention here... double check your target memberships!

like image 36
Harout360 Avatar answered Sep 04 '25 10:09

Harout360