Skip to content

Instantly share code, notes, and snippets.

@zqqf16
Forked from neonichu/DynamicFunctions.swift
Created August 4, 2016 07:26
Show Gist options
  • Save zqqf16/6a2206b068210cec1588462d1f7d39a3 to your computer and use it in GitHub Desktop.
Save zqqf16/6a2206b068210cec1588462d1f7d39a3 to your computer and use it in GitHub Desktop.
Using dlopen / dlsym to call C functions from Swift
import Darwin
let handle = dlopen("/usr/lib/libc.dylib", RTLD_NOW)
let sym = dlsym(handle, "random")
let functionPointer = UnsafeMutablePointer<() -> CLong>(sym)
let result = functionPointer.memory()
println(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment