Ranking Stackingblocks
Ranking Stackingblocks

Reputation: 607

Non-member functions in Objective-C

I want to write a standalone function in Objective-C; so essentially a C-style function, with Objective-C calls in it. For example:

NSString* someFunc()
{
 NSString* str = [[NSString alloc] init];

 return str;
}

I declare the function in a header file, and define in it a .m file. However, the function doesn't appear to be compiled in, as the linker complains about the missing symbol. I thought that maybe I should put it in a C file, but then of course it spat at me for writing Objective-C Nonsense in BASI... C.

What do?

Upvotes: 5

Views: 920

Answers (1)

Ranking Stackingblocks
Ranking Stackingblocks

Reputation: 607

This turned out to be a silly mistake from me. The function was defined in a .m file but I was trying to use it in a .mm file (C++/Objective-C), so naturally it was looking for a C++-mangled symbol. Putting

#ifdef __cplusplus
extern "C" {
#endif

// Declaration

#ifdef __cplusplus
}
#endif

in the header file fixes the issue.

Upvotes: 3

Related Questions