从目标C类调用C ++函数

时间:2022-09-07 07:58:35

I want to call a c++ function from my objective C class i am not sure how to call it.Please somebody help me by giving some sample codes or useful links Thanks in advance

我想从我的目标C类调用c ++函数我不知道如何调用它。请有人帮我提供一些示例代码或有用的链接提前感谢

3 个解决方案

#1


5  

Change the .m extension to .mm on the Objective-C file to make it Objective-C++.

在Objective-C文件上将.m扩展名更改为.mm,使其成为Objective-C ++。

#2


2  

An alternative to switching your class implementation file to Objective-C++ is to declare the C++ function as extern "C", assuming its arguments are all C types. Place that declaration inside a C-compatible C++ header file and include it from both the C++ file that implements the function and from the Objective-C file that calls it. This will of course only work if the data types of parameters and the return type are pure C types, but is in some cases "cleaner" than the Objective-C++ approach.

将类实现文件切换到Objective-C ++的替代方法是将C ++函数声明为extern“C”,假设它的参数都是C类型。将该声明放在C兼容的C ++头文件中,并将其包含在实现该函数的C ++文件和调用它的Objective-C文件中。这当然只有在参数的数据类型和返回类型是纯C类型时才有效,但在某些情况下比Objective-C ++方法“更清晰”。

#3


0  

Expanding on what pmjordan said, if you have a .m file calling in to a .mm or .cpp, make sure that the main header of the .mm or .cpp, where the functions you are calling are declared, have conditionally compiled extern "C" like this .h file:

扩展pmjordan所说的,如果你有一个.m文件调用.mm或.cpp,请确保.mm或.cpp的主标题,你调用的函数被声明,有条件编译extern “C”喜欢这个.h文件:

#ifdef __cplusplus
extern "C" {
#endif

void functionA(void);
void functionB(const char *);

#ifdef __cplusplus
}
#endif

When this header is included as part of the .mm or .cpp, the extern "C" will do the right thing, and when the .h is compiled as part of the calling .c or .m file that calls those functions, the extern "c" is excluded and will not cause the compiler to complain.

当此标头作为.mm或.cpp的一部分包含时,extern“C”将做正确的事情,当.h被编译为调用这些函数的调用.c或.m文件的一部分时, extern“c”被排除在外,不会导致编译器抱怨。

Using this solution will prevent you from needlessly changing .m files into .mm files just so you can simply call C routines in c++ files.

使用此解决方案可以防止您不必要地将.m文件更改为.mm文件,这样您就可以简单地在c ++文件中调用C例程。

#1


5  

Change the .m extension to .mm on the Objective-C file to make it Objective-C++.

在Objective-C文件上将.m扩展名更改为.mm,使其成为Objective-C ++。

#2


2  

An alternative to switching your class implementation file to Objective-C++ is to declare the C++ function as extern "C", assuming its arguments are all C types. Place that declaration inside a C-compatible C++ header file and include it from both the C++ file that implements the function and from the Objective-C file that calls it. This will of course only work if the data types of parameters and the return type are pure C types, but is in some cases "cleaner" than the Objective-C++ approach.

将类实现文件切换到Objective-C ++的替代方法是将C ++函数声明为extern“C”,假设它的参数都是C类型。将该声明放在C兼容的C ++头文件中,并将其包含在实现该函数的C ++文件和调用它的Objective-C文件中。这当然只有在参数的数据类型和返回类型是纯C类型时才有效,但在某些情况下比Objective-C ++方法“更清晰”。

#3


0  

Expanding on what pmjordan said, if you have a .m file calling in to a .mm or .cpp, make sure that the main header of the .mm or .cpp, where the functions you are calling are declared, have conditionally compiled extern "C" like this .h file:

扩展pmjordan所说的,如果你有一个.m文件调用.mm或.cpp,请确保.mm或.cpp的主标题,你调用的函数被声明,有条件编译extern “C”喜欢这个.h文件:

#ifdef __cplusplus
extern "C" {
#endif

void functionA(void);
void functionB(const char *);

#ifdef __cplusplus
}
#endif

When this header is included as part of the .mm or .cpp, the extern "C" will do the right thing, and when the .h is compiled as part of the calling .c or .m file that calls those functions, the extern "c" is excluded and will not cause the compiler to complain.

当此标头作为.mm或.cpp的一部分包含时,extern“C”将做正确的事情,当.h被编译为调用这些函数的调用.c或.m文件的一部分时, extern“c”被排除在外,不会导致编译器抱怨。

Using this solution will prevent you from needlessly changing .m files into .mm files just so you can simply call C routines in c++ files.

使用此解决方案可以防止您不必要地将.m文件更改为.mm文件,这样您就可以简单地在c ++文件中调用C例程。