[^ Summary-1]: ABI is stable from Swift 3.0 ... It should have been postponed further [^ Summary-1-Note-1]. [^ Summary-1-Note-1]: Winding down the Swift 3 release
Same as last time.
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
This time I prepared 4 files.
$ ls
c.c c.h swift.h swift.swift
c.h
#include <stdio.h>
void c_function(const char *string);
void call_swift_function(void);
swift.h
void swift_function(const char *string);
c.c
#include "c.h"
#include "swift.h"
void c_function(const char *string) {
printf("%s", string);
}
void call_swift_function(void) {
swift_function("Hello, I'm C.\n");
}
swift.swift
@_silgen_name("swift_function")
func swift_function(cString:UnsafePointer<CChar>) {
print(String.fromCString(cString)!)
}
c_function("Hello, I'm Swift.\n")
call_swift_function()
@ asmname
instead of @ _silgen_name
.OS X, Linux common
$ clang -c c.c -oc.o
$ ls
c.c c.h c.o swift.h swift.swift
OS 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.h swift.swift
$ ./main
Hello, I'm Swift.
Hello, I'm C.
Linux
$ swiftc swift.swift c.o -import-objc-header c.h
$ ls
c.c c.h c.o main swift.h swift.swift
$ ./main
Hello, I'm Swift.
Hello, I'm C.
did it. This is the end. It ’s easy, is n’t it?
You can also pass a function pointer, but that's another story [http://qiita.com/YOCKOW/items/9239e65995b8c4f8b14f).