开发者

Calling C from Objective C

开发者 https://www.devze.com 2023-03-08 05:11 出处:网络
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

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 ini开发者_JAVA百科tializing 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?


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?


Messages such as implicit declaration of function 'mt_seed32' usually pop up, when you use a function before it was defined. See example.

void foo() {
    //do stuff
    bar(); //call bar that was declared later
}

void bar() {
    ...
}

This may happen if you forgot to include the header file, or you included it after you used functions declared in that header file. Another fix is to declare a function prototype before usage.

Also you assign your random number to a pointer to uint32_t. Is this what you really want? If not, then you must remove * from your declaration: uint32_t i = mt_lrand();


Was able to fix my problem by changing file type from '.m' to '.mm'. This causes the compiler to use obj c++ not obj c. It removes the warnings but I'm not sure if I've fixed the underlying issues

Solution – Duplicate Symbol

0

精彩评论

暂无评论...
验证码 换一张
取 消