dubbeat
dubbeat

Reputation: 7847

How can I call a C function from Objective-C

I'm trying to figure out how to call a c function from an obj-c file. I've seen some good examples of how to do the opposite.

In the example below I have an objective c file. It contains a c function named setup. I want to be able to create an instance of my obj-c file in the regular way and then call the setup function.

Header

#import <Foundation/Foundation.h>

void setup(int,float);

@interface Test : NSObject {
}

@end

Source

#import "Test.h"
#include <stdio.h>
#include <stdlib.h>

void setup(int val1,float val2)
{
    //do something with values  
}

@implementation Test

@end

View did load

Test *test =[Test alloc]init]

//this does not work
test.setup(6,1.4);

Upvotes: 3

Views: 4999

Answers (1)

Andres Kievsky
Andres Kievsky

Reputation: 3491

Just call setup(). As declared, is in no way tied to an object - it's just a regular C function.

Upvotes: 11

Related Questions