Extern C functions in Objective-c

15,984

Solution 1

extern "C" may do the trick. I'm able to get C functions to compile and link by doing something like this both around the implementation and header file declaration. Here's a simple example:



#if __cplusplus
extern "C" {
#endif

/// converts a degree value to radians
double DegreesToRadians(double degrees);

/// converts radian value to degrees
double RadiansToDegrees(double radians);


#if __cplusplus
}   // Extern C
#endif


Implementation file:


#import "Math.h"

#if __cplusplus
extern "C" {
#endif


double DegreesToRadians(double degrees) {return degrees * M_PI / 180;};
double RadiansToDegrees(double radians) {return radians * 180/M_PI;};


#if __cplusplus
} //Extern C
#endif

Solution 2

You should never have to use extern "C" in an Objective-C project. This is because Objective-C is a strict superset of C.

Share:
15,984
Hans Espen
Author by

Hans Espen

.Net developer

Updated on June 04, 2022

Comments

  • Hans Espen
    Hans Espen almost 2 years

    i develop iPhone apps, and after updating to sdk 3.0, I get an error on CFWriteStreamCreateWithFTPURL while linking. This is the code I call to get the error.

    streamInfo.writeStream = CFWriteStreamCreateWithFTPURL(NULL, urlRefWrite);
    

    I have an idea that it can be solved using extern "C", but after having googled it, I have not found the solution to my problem. Any ideas?

    Thanks in advance