有没有办法调用从Swift的dylib加载的C函数?
这是我的dylib文件:
cppdemofile.cpp
#include "cppdemofile.h"
int add(int a, int b) {
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
cppdemofile.h
#ifndef __CppDemoLibrary__cppdemofile__
#define __CppDemoLibrary__cppdemofile__
#pragma GCC visibility push(default)
extern "C" int add(int a, int b);
#pragma GCC visibility pop
#endif
Run Code Online (Sandbox Code Playgroud)
编译到dylib并检查:
nm -gU libCppDemoLibrary.dylib
0000000000000f80 T _add
Run Code Online (Sandbox Code Playgroud)
......复制libCppDemoLibrary.dylib到~/lib......
Swift程序:
@IBAction func buttonClick(sender: NSButton) {
let handle = dlopen("libCppDemoLibrary.dylib", RTLD_NOW)
if (handle != nil) {
var sym = dlsym(handle, "add")
if (sym != nil) {
let pointer …Run Code Online (Sandbox Code Playgroud)