Oskar Lund
Oskar Lund

Reputation: 369

Mixing Objective-c and C: How to use a C source file that lacks header file?

I wan't to use some C source in my Objective-c proj but the source lacks a header file. I get a "implicit declaration of function" warning when building, however the app launches fine and works fine up until I try to call one of the C functions. Now that it crashes could be cause somethings wrong with the args I pass, I haven't investigated that further yet. But:

Is there a way to get rid of the build warning?

Am I on the right track? Meaning that the C source will be usable even without the header file..


Some background :

I'm trying to use a GPL dynamic C library in my Objective-c project (iPhone). With no C experience the C code itself is a bit to low level for me to be able to effectively use. However the C lib also contains some higher level example programs which I can understand what they are doing and I think (hope) also modify to suit my needs. This example program is just a source file fired from a shell script wrapper. No header file.

Upvotes: 0

Views: 370

Answers (1)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

First of all, there is no such thing as a C class.

If you mean just calling a C function you can add the function prototypes in your Objective-C code.

Let's say you need to call a function f that returns an int and takes a char parameter that is defined in your .c file.

In your .m file, where you will call the function, add the following line:

int f(char);

You will get rid of the implicit declaration of function.

Alternatively, you can write all function prototypes in a custom made .h file of your own in case you decide you need to use those functions in other compilation units as well.

Upvotes: 3

Related Questions