Xcode-将目标 C 文件导入 C 文件 [英] Xcode- Importing Objective C files into a C file
问题描述
如果我创建一个 c 文件并将其添加到顶部.
If I create a c file and add this to the top.
#import <GLKit/GLKit.h>
我收到了一堆奇怪的错误.如果我导入任何目标 C 文件,就是这种情况.我在其他问题/答案中没有看到任何相关内容.每个人似乎都认为 C 和 Objective C 可以无缝地协同工作!
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 '('
推荐答案
如果您将 c 文件从 something.c
重命名为 something.m
,您应该能够使用:
If you rename your c file from something.c
to something.m
, you should be able to use:
#import <GLKit/GLKit.h>
在您的文件中.现在,如果你这样做,生成的文件将是一个 Objective-C 文件并且需要一个 Objective-C 编译器来编译,因此需要指定 .m
扩展名.
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.
换句话说,Objective-C 是一种建立在 C 之上的语言.所以你可以混合"它们的语法,可以这么说,但只有 Objective-C 编译器才能编译混合.C 编译器对 ObjC 一无所知.
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.
通过更改文件的扩展名,您可以选择编译器,并且您可以拥有一个纯 C"文件,该文件导入一个 Objective-C 头文件并调用某些 Objective-C 库.
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.
这篇关于Xcode-将目标 C 文件导入 C 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!