Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode- Importing Objective C files into a C file

If I create a c file and add this to the top.

#import <GLKit/GLKit.h>

I get a bunch of strange errors. This is the case if I import any objective C file. I have seen nothing about this in other questions/answers. Everyone seems to suggest that C and Objective C work seamlessly together!

gl.h
Unknown type name 'GLenum'; did you mean 'enum'?

GLKNamedEffect.h
Expected identifier or '('
like image 928
David John Avatar asked Apr 01 '26 16:04

David John


2 Answers

If you rename your c file from something.c to something.m, you should be able to use:

#import <GLKit/GLKit.h>

within your file. Now, if you do that, the resulting file will be an Objective-C file and will need an Objective-C compiler to be compiled, hence the need to specify the .m extension.

In other words, Objective-C is a language built on top of C. So you "mix" their syntaxes, so to speak but only the Objective-C compiler will be able to compile the mix. The C compiler will know nothing about ObjC.

By changing the extension of your file, you are in a way choosing the compiler, and you can have a "plain C" file which imports an Objective-C header and calls into some Objective-C library.

like image 159
sergio Avatar answered Apr 04 '26 09:04

sergio


C isn't objective-c and so, cannot import objective-c source or include files. (In other words, what you have been told is false)

like image 22
KevinDTimm Avatar answered Apr 04 '26 09:04

KevinDTimm



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!