Search code examples
iosxcodestatic-librariesclangsymbols

"Undefined symbols for architecture" clang error (using static library) while the symbol definition is present


I am developing an open-source iOS static library with a demo project. I created my "library" xcodeproj, created a class DKNavigationBar, included it where I had to include it. Then I created a "demo" xcodeproj and dragged "library" xcodeproj into it. I included my static library in "Target Dependencies" section and imported my library in the pch file using #import <MyLibrary/MyLibrary.h>. I built the demo project and there was no errors at all. Then, in my DKDAppDelegate.m I called [DKNavigationBar class] - LLDB gave me no error. Then I built my project and boom:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_DKNavigationBar", referenced from:
      objc-class-ref in DKDAppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

At the first glance, it seems like a normal mistake: "Oh, I forgot to add my source file to the 'compile sources' build phase". But when I looked deeper and deeper I realized that everything is set up correctly. After some research I realized that symbol definition is present in my .a file and the static library is built for my architecture:

$ cd /path/to/derived/data/product/folder

$ lipo -info libMyLibrary.a
input file libMyLibrary.a is not a fat file
Non-fat file: libMyLibrary.a is architecture: i386

$ otool -MVv libMyLibrary.a
Archive : libMyLibrary.a
libMyLibrary.a(DKNavigationBar.o):
Module table (0 entries)

$ nm libMyLibrary.a
libMyLibrary.a(DKNavigationBar.o):
00000074 S _OBJC_CLASS_$_DKNavigationBar
         U _OBJC_CLASS_$_UINavigationBar
00000060 S _OBJC_METACLASS_$_DKNavigationBar
         U _OBJC_METACLASS_$_NSObject
         U _OBJC_METACLASS_$_UINavigationBar
         U __objc_empty_cache
         U __objc_empty_vtable
00000038 s l_OBJC_CLASS_RO_$_DKNavigationBar
00000010 s l_OBJC_METACLASS_RO_$_DKNavigationBar

Now I have no idea what's going wrong. Any help would be appreciated.


TL;DR

  1. I created a static library project and did everything correctly
  2. I created a demo project, dragged the library project into it and included my library in "Target Dependencies", and did everything correctly
  3. Xcode recognizes the header files and live debugger gives me no error.
  4. When building, an undefined symbols for architecture clang error pops up
  5. I examined my built libMyLibrary.a file and found out that my symbol definition is there.

Solution

  • Add the library under "Link Binary With Libraries" in build phases.