Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a C function from Objective-C

Tags:

c

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);
like image 599
dubbeat Avatar asked Dec 08 '25 02:12

dubbeat


1 Answers

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

like image 193
Andres Kievsky Avatar answered Dec 10 '25 16:12

Andres Kievsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!