DEV Community

Discussion on: Calling Swift from Go

Collapse
 
maxencecharriere profile image
Maxence Charriere

Hello, I tried to reproduce what is described in this blog post.

I'm using swift 5 and go1.12.4

When I try to compile, I got:

▶ go build .
# github.com/maxence-charriere/murlok/internal/mac
ld: library not found for -lmac
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Enter fullscreen mode Exit fullscreen mode

(mac is replacing the go_swift name)

Here is the preamble:

/*
#cgo CFLAGS: -I./include
#cgo LDFLAGS: -L.build/debug/ -lmac
#include <stdlib.h>
#include "include/mac.h"
*/
import "C"
Enter fullscreen mode Exit fullscreen mode

I did swift build before.
Look like it doesn't find the lib.

Here is what swift build is generating:

murlok/internal/mac  swift ✗                                                                                  23m ⚑ ◒
▶ ls -l .build/debug/
total 32
drwxr-x---   4 maxence  staff    128 Apr 20 10:05 ModuleCache
drwxr-x---   3 maxence  staff     96 Apr 20 10:05 index
drwxr-xr-x  10 maxence  staff    320 Apr 20 10:05 mac.build
-rw-r--r--   1 maxence  staff    372 Apr 20 10:05 mac.swiftdoc
-rw-r--r--   1 maxence  staff  10844 Apr 20 10:05 mac.swiftmodule
drwxr-x---   3 maxence  staff     96 Apr 20 10:05 macPackageTests.product
Enter fullscreen mode Exit fullscreen mode

Do you have an idea of what the problem could be?

Collapse
 
gerwert profile image
gerwert

I had a similar error in a different project, after upgrading MacOS (and Swift to Swift 5?). Solved it by deleting .build directory, and re-building the project.
Did you try the example project github.com/onderweg/swift-from-go ?

Collapse
 
vjerci profile image
Vjeran • Edited

I've tried your solution but for some reason it doesn't seem to work

all the time it reports error image missing or something like that

then i've stumbled upon this article

ardanlabs.com/blog/2013/08/using-c...

figured out .dylib is missing

and then added it like this:

go run -exec "env LD_LIBRARY_PATH=/Users/vjeranfistric/go/src/github.com/vjerci/swift-from-go" main.go
Enter fullscreen mode Exit fullscreen mode

or packing it into folder like this

swift-from-go.app/Contents/Frameworks/libgo_swift.dylib
Enter fullscreen mode Exit fullscreen mode

It did the trick for me