Search code examples
c++objective-ccxcodemach-o

Apple Mach-O Linker Error, due to file extension


The dreaded typical linker error..

ld: symbol(s) not found for architecture armv6 collect2: ld returned 1 exit status

However, it is caused by filename? I use C++/Objective-C, so all of my Obj-C files are .mm, but I can never use any .c files. For example I've included the SFMT algorithm in my project, which was giving me these errors, but just changing the single .c file to .cpp made it go away and the code works just fine! I am only including the headers, so I'm not sure why this makes any difference.

The problem now is I'm trying to include Freetype2, giving me the same issue (pretty sure it's because it's .c), but that is far too large to rename every file, and I'm also using a linked binary, so unless I recompile it with new filenames, I can't change that. So now it's time to find the real reason behind this.

Any idea why this would happen? How can I stop linker errors for .c files?


Solution

  • Wrap your Freetype includes within an extern "C" directive:

    // Non-C includes
    #include <iostream>
    
    extern "C"
    {
        #include <freetype/freetype.h>
        // ... Other freetype includes
    }
    

    You can probably use #import instead of #include within an extern "C" directive. I've never tried, but I don't see why it wouldn't work.