Treat Swift functions and closures as C function pointers

Overview

Last time wrote how to call a Swift function from C, but it is troublesome to prepare a separate header called swift.h. However, the attribute [^ 1] and @ _silgen_name ("...") s also smells suspicious, and ... So, there is also a way to process Swift functions and closures as C function pointers. There is, so I will write it.

[^ 1]: For small projects, you can just write a function prototype in c.c ... right?

wrap up

  1. Use @convention (c).
  2. That's it (you don't need to read this article if you understand this)

environment

OS X


$ clang --version
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ swift --version
Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29)
Target: x86_64-apple-macosx10.9

Linux


$ clang --version
clang version 3.8.0 (tags/RELEASE_380/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin

$ swift --version
Swift version 2.2.1 (swift-2.2.1-RELEASE)
Target: x86_64-unknown-linux-gnu

source file

Like last two times, there are only three files.

$ ls
c.c  c.h  swift.swift

c.h


#include <unistd.h>
void call_me_later(void (*closure)(), unsigned int seconds);

c.c


#include "c.h"
void call_me_later(void (*closure)(), unsigned int seconds) {
  sleep(seconds);
  closure();
}

swift.swift


func swift_function() {
  print("I just woke up! Yes, I'm Function.")
}
let swift_closure:@convention(c) ()->() = {
  print("I just woke up! Uh-oh, I'm Closure.")
}
call_me_later(swift_function, 0)
call_me_later(swift_closure, 2)

Compilation procedure

Create object file from C source

OS&#x0020;X,&#x0020;Linux common


$ clang -c c.c -oc.o
$ ls
c.c  c.h  c.o  swift.swift

Compile Swift source with object files

OS&#x0020;X


$ xcrun --sdk macosx swiftc swift.swift c.o -import-objc-header c.h
ld: warning: object file (c.o) was built for newer OSX version (10.11) than being linked (10.9)
$ ls
c.c		c.h		c.o		main		swift.swift
$ ./main
I just woke up! Yes, I'm Funtion.
I just woke up! Uh-oh, I'm Closure.

Linux


$ swiftc swift.swift c.o -import-objc-header c.h
$ ls
c.c  c.h  c.o  main  swift.swift
$ ./main
I just woke up! Yes, I'm Funtion.
I just woke up! Uh-oh, I'm Closure.

did it. This is the end. It's easy [^ 2]?

[^ 2]: If you google There seems to be various applications.

Recommended Posts

Treat Swift functions and closures as C function pointers
Call a C function from Swift
Introducing C ++ characters and strings for Swift programmers
C macros for watchOS and Swift API Availability
C macros for tvOS and Swift API Availability
Handle pointers such as UnsafePointer <T> in Swift