我该如何从Swift文件中调用一个C++函数(不涉及类)?我尝试了以下代码:
在someCFunction.c文件中:
void someCFunction() {
printf("Inside the C function\n");
}
void aWrapper() {
someCplusplusFunction();
}
在someCpluplusfunction.cpp文件中:
void someCplusplusFunction() {
printf("Inside the C++ function");
}
在 main.swift 中:
someCFunction();
aWrapper();
在Bridging-Header.h文件中:
#import "someCFunction.h"
#import "someCplusplusFunction.h"
我发现这个回答很有启发性,但我仍然不能使它正常工作。 你能指导我正确的方向吗?
谢谢!
extern "C"
声明,具体为:#ifdef __cplusplus extern "C" { #endif
在此处添加 C 函数定义...#ifdef __cplusplus } #endif
。我无法在注释中使用换行符,但您需要在“extern C {”和“}”之前后添加换行符。 - ephemer