[^ Summary-1]: ABI est stable depuis Swift 3.0 ... Il aurait dû être reporté davantage [^ Summary-1-Note-1]. [^ Summary-1-Note-1]: Fin de la version Swift 3
Le meme que la derniere fois.
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
Cette fois, j'ai préparé 4 fichiers.
$ 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
au lieu de @ _silgen_name
.OS X, Commun à Linux
$ 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.
l'a fait. C'est la fin. C’est facile, non?
Vous pouvez également passer un pointeur de fonction, mais c'est une autre histoire [http://qiita.com/YOCKOW/items/9239e65995b8c4f8b14f).
Recommended Posts