Giter VIP home page Giter VIP logo

swift-cgo-example's Introduction

swift-cgo-example

Edit: There is an unoffcial, more convenient, way to call a swift function from C using @_cdecl. See https://forums.swift.org/t/best-way-to-call-a-swift-function-from-c/9829/2.

There’s no official way yet. Aside from name mangling, Swift functions use a different calling convention from C. Unofficially, if you’re willing to deal with more than the usual amount of code breakage and compiler bugs, there’s an unofficial attribute @_cdecl that does this:

See #2 (comment) for an example.


This example demonstrates how to use CGO to call Swift code from Go. The path looks something like this:

Swift --> Bridging Header --> Objective-C --> C Header Definition --> CGO Include

While the process of calling Objective-C from Go is demonstrated in alexdiaferia's project gogoa, calling swift code requires a bridging header that is usually generated in Xcode. This is needed for interoperability between Objective-C and Swift, but as far as I'm aware, requires a precompiled object/library file in order to access the Swift function definition in Go.

In this example we:

  • Generate a bridging header on the command line instead of Xcode.
  • Output a library containing Objective-C and Swift code.
  • Link that library using CGO.

To test it out, run

make && ./swift-cgo-example

swift-cgo-example's People

Contributors

smasher164 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

s-you czxcc

swift-cgo-example's Issues

Compile errors with Swift 4.1.2 and Xcode 9.4.1

I am trying to compile this on 10.13, which with a couple modifications to the makefile seems possible. These errors I think I've worked around:

  • NSApplication.shared() seems to not be supported anymore? I was getting this error, but removing this line of code doesn't seem to break anything yet.
minimal.swift:7:29: error: cannot call value of non-function type 'NSApplication'
        NSApplication.shared()
        ~~~~~~~~~~~~~~~~~~~~^~
  • 'NSTitledWindowMask' is unavailable in Swift. Replaced this with .fullSizeContentView and it seems to have fixed it. Here's the error I was getting:
minimal.swift:10:42: error: 'NSTitledWindowMask' is unavailable in Swift
                              styleMask: NSTitledWindowMask,
                                         ^~~~~~~~~~~~~~~~~~
AppKit.NSTitledWindowMask:3:12: note: 'NSTitledWindowMask' has been explicitly marked unavailable here

I can't seem to get past this part:

minimal.m:6:13: warning: class method '+CreateWindow' not found (return type defaults to 'id') [-Wobjc-method-access]
    [Window CreateWindow];

Running minimal.go exhibits the same issue before crashing:

2018-07-26 16:59:58.242 minimal[22059:5510792] +[minimal.Window CreateWindow]: unrecognized selector sent to class 0x40d81a8
2018-07-26 16:59:58.243 minimal[22059:5510792] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[minimal.Window CreateWindow]: unrecognized selector sent to class 0x40d81a8'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff3de372db __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x00007fff64fd8c76 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff3decfcf4 +[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x00007fff3ddad820 ___forwarding___ + 1456
	4   CoreFoundation                      0x00007fff3ddad1e8 _CF_forwarding_prep_0 + 120
	5   minimal.a                           0x00000000040d777e CreateWindow + 30
	6   minimal                             0x0000000004046700 runtime.asmcgocall + 112
)
libc++abi.dylib: terminating with uncaught exception of type NSException
SIGABRT: abort
PC=0x7fff65d42b66 m=0 sigcode=0

Is there something simple that I'm missing? I did some Objective C programming ages ago and have dabbled with Swift, but it's been quite some time.

Unable to access the library(minimal.a) on another machine

With the help of this project I was able to create the library(minimal.a). I utilised this library for accessing swift code through go. I am accessing swift code through my go executable using this library. It is all working fine on my machine.
However, I get following error when I try to execute it on other machines.
dyld: Symbol not found: _$sBOWV
Referenced from: minimal.a
Expected in: /usr/lib/libswiftCore.dylib
in minimal.a
Abort trap: 6

I thought this is some kind of linking issue so I statically linked the library using following command
"xcrun swiftc minimalC.o minimalS.o -static-stdlib -emit-library -module-name minimal -o minimal.a". Using -static-stdlib the issue got resolved.

But, -static-stdlib is not supported on latest MacOS. So my question is how can I use this library on other machines without using -static-stdlib.

Note: Utilised this method to access swift from go but, underlying code is different

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.