Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21976

Linker error declaring a function inside class

I thought to know how to declare functions inside .m files, but here I get a linker error.I declare this in the .h file:

#import <Foundation/Foundation.h>
// Other rimports

void SQLite3HelperHandle(NSError* error);

@interface SQLite3Helper : NSObject

// Method signatures

@end

Then in the .m file:

#import "SQLite3Helper.h"

void SQLite3HelperHandle(NSError* error)
{
    // Method body
}

@implementation SQLite3Helper

// Methods implementation

@end

But I get a linker error.And the error has a lot of unreadable information.The only relevant thing is:

"_SQLite3HelperHandleError", referenced from:

Also, how do I declare it inline? I tried declaring it this way in the header:

extern inline void SQLite3HelperHandle(NSError* error);

And normally in the .m file:

void SQLite3HelperHandle(NSError* error);

I also tried other ways to do it, but never found the way to silent that linker error.
It should be as fast as a macro, but the function it too long to write and I prefer type checking so that's why I need an inline function.

Upvotes: 0

Views: 311

Answers (3)

Wade Tregaskis
Wade Tregaskis

Reputation: 2018

To make a function inline define it as static inline, whether it's in a header or not. If you only need it in one file, just define it in that .m file and leave it out of the header. Otherwise, define it entirely in the .h file.

Upvotes: 0

paulmelnikow
paulmelnikow

Reputation: 17208

It sounds like the .m file needs to be included in the target you're building.

I use FOUNDATION_EXPORT void ... in the .h and just what you have in the .m file.

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89509

Looks like C++ style function name mangling.

To solve it, put your C-style function declaration (in your .h file) between this conditional:

#ifdef __cplusplus
extern "C" {
#endif

void SQLite3HelperHandle(NSError* error);

#ifdef __cplusplus
}
#endif

More information about what this is doing can be seen in this related question or this one.

Upvotes: 2

Related Questions