混合目标 C ,(*.m , *.mm & .c /.cpp ) 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4714698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mixing Objective C ,(*.m , *.mm & .c /.cpp ) files
提问by Amitg2k12
In my project Core libraries are part of C/C++ files, while UI needs to be developed in Objective C, I am able to access/Call C++ functions from Objective C/.mm files but reverse no luck so far, i.e. i am not able to call Objective C functions from C++ Files, when i tried to include Objective C header even system header
在我的项目中,核心库是 C/C++ 文件的一部分,而 UI 需要在目标 C 中开发,我能够从目标 C/.mm 文件访问/调用 C++ 函数,但到目前为止没有运气,即我不是能够从 C++ 文件调用 Objective C 函数,当我尝试包含 Objective C 头甚至系统头时
#import <foundation/foundation.h>
getting around 1000+ compilation error,
得到大约 1000+ 编译错误,
something like this
像这样的东西
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:180:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:180: error: expected unqualified-id before '@' token
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:182:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:182: error: expected initializer before '*' token
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183: error: 'NSString' was not declared in this scope
/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183:0 /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:183: error: 'aSelectorName' was not declared in this scope
Am i missing some pre-compile flag etc.. can anyone suggest me, the best possible way to call/access objective C class which is inherited from NSObject, without modifying much C++ code, i just need to call one function
我是否缺少一些预编译标志等..谁能建议我,调用/访问从 NSObject 继承的目标 C 类的最佳方法,无需修改太多 C++ 代码,我只需要调用一个函数
Code structure / Order to include header files are
代码结构/包含头文件的顺序是
Some system header file
Some Core Class Header file
#import <foundation/foundation.h>
回答by
If you have a .cpp file with C++ code that needs to use Objective-C as well, either rename that .cpp file to .mm or pass -x objective-c++
to the compiler.
如果您有一个包含 C++ 代码的 .cpp 文件也需要使用 Objective-C,请将该 .cpp 文件重命名为 .mm 或传递-x objective-c++
给编译器。
回答by Joris van Liempd iDeveloper
I found it imposible to use any Objective-c in the C++ header files.
我发现不可能在 C++ 头文件中使用任何 Objective-c。
However, you can include Objective-c in the implementation files.
但是,您可以在实现文件中包含 Objective-c。
(.mm or you can set how to interpret .cpp files in the info of the file. Choose Info->General:FileType:Sourcecode.cpp.objcpp )
(.mm 或者你可以在文件的信息中设置如何解释 .cpp 文件。选择 Info->General:FileType:Sourcecode.cpp.objcpp )
Use
用
cppClass.h:
cppClass.h:
class objcClass;
objcClass* mMemberVariable;
cppClass.mm:
cppClass.mm:
#import "objcClass.h";
void cppFunction(){
[objcClass message];
}
in the cpp header file.
在 cpp 头文件中。
Then include the header that defines the class in the .cpp or .mm file.
然后在 .cpp 或 .mm 文件中包含定义类的标头。
回答by user362515
If you use Qt, there is Q_FORWARD_DECLARE_OBJC_CLASS
macro which can be used to forward declare an Objective-C class.
如果您使用 Qt,则Q_FORWARD_DECLARE_OBJC_CLASS
可以使用宏来转发声明一个 Objective-C 类。
As a reference, here is the implementation:
作为参考,这里是实现:
#ifndef Q_FORWARD_DECLARE_OBJC_CLASS
# ifdef __OBJC__
# define Q_FORWARD_DECLARE_OBJC_CLASS(classname) @class classname
# else
# define Q_FORWARD_DECLARE_OBJC_CLASS(classname) typedef struct objc_object classname
# endif
#endif
回答by alexmorhun
I used a technique of the name expand files depending on a programming language. Also, I added "-x objective-c ++" to the compiler, but the problem remained. I was helped by council for the link, pay attention to the "file type" parameter.
我根据编程语言使用了名称扩展文件的技术。另外,我在编译器中添加了“-x Objective-c ++”,但问题仍然存在。我被议会的帮助链接,到“文件类型”参数注意。
回答by Khushneet
In XCode 11, it can be done by changing the build setting Compile Source as(GCC_INPUT_FILETYPE) to Objective-C++(sourcecode.cpp.objcpp). By doing this, there is no need to change the file extension type to mm.
在 XCode 11 中,可以通过将构建设置Compile Source as(GCC_INPUT_FILETYPE) 更改为Objective-C++( sourcecode.cpp.objcpp) 来完成。通过这样做,无需将文件扩展名类型更改为 mm。