Question

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?

Was it helpful?

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.

OTHER TIPS

Surround your c header file with this. This can also surround the include:

#ifdef __cplusplus   
extern "C" {         
#endif     

// function declarations etc if this is your own header.
// OR you can use this in the .mm file to surround your include.
//...

#ifdef __cplusplus       
};                       
#endif   

This specifies external linkage for your c functions. If you don't do this when you include your c .h files the C++ compiler will mangle in a different way from the C compiler, and cause problems for the linker.
By using extern "C", you are telling your C++ compiler to use C-style mangling of the functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top