Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C from Objective C

Tags:

c

objective-c

I'm new to objective c & c. I'm trying to use this random generator c library in an objective c program. My understanding is that objective c is a strict superset of c so this should be possible.

My code compiles and runs but I get a lot of warnings.

  • warning: implicit declaration of function 'mt_seed32'
  • warning: implicit declaration of function 'mt_lrand'
  • warning: Semantic Issue: Implicit declaration of function 'mt_seed32' is invalid in C99
  • warning: Semantic Issue: Implicit declaration of function 'mt_lrand' is invalid in C99
  • warning: Semantic Issue: Incompatible integer to pointer conversion initializing uint32_t * (aka unsigned int *) with an expression of type int

I have not imported the C header file to the objective c class - it just finds it. If I import it I get duplicate method errors.

C library header file:

extern void     mt_seed32(uint32_t seed);

extern uint32_t     mt_lrand(void); 

Code to call it: [I've tried calling it with [self method()] but that crashes

mt_seed32(3);

uint32_t *i = mt_lrand();

Can anyone tell me how too get rid of these warnings?

like image 393
andy boot Avatar asked Aug 27 '26 23:08

andy boot


1 Answers

The last compiler error happens because mt_lrand(); returns an int, not a pointer to an int. Therefore, the last line should be

uint32_t i = mt_lrand();

All the other errors are due to the fact that you did not #include the library header. Could you please post the errors that occur when you do include the library header?

like image 100
puzzle Avatar answered Aug 29 '26 17:08

puzzle



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!