swift-haskell-tutorial icon indicating copy to clipboard operation
swift-haskell-tutorial copied to clipboard

Can't get swift to find the haskell function

Open Verneri opened this issue 6 years ago • 4 comments

I have now checked multiple times that i should have done all the correct steps. After several Clean build folder build try outs it also built itself.

Now while running it seems that Haskell is nicely calling Swift as the window opens and i see a nice start of hello world in the console. but then it runs into problems:

hello world
dyld: lazy symbol binding failed: Symbol not found: _square
  Referenced from: /Users/verneri/Library/Developer/Xcode/DerivedData/SwiftHaskell-gboogzegqjkzygasdvpdhasnglaw/Build/Products/Debug/SwiftAppLibrary.framework/Versions/A/SwiftAppLibrary
  Expected in: flat namespace

dyld: Symbol not found: _square
  Referenced from: /Users/verneri/Library/Developer/Xcode/DerivedData/SwiftHaskell-gboogzegqjkzygasdvpdhasnglaw/Build/Products/Debug/SwiftAppLibrary.framework/Versions/A/SwiftAppLibrary
  Expected in: flat namespace

What could be the problem here?

all in all i like the tutorial and am very interested in seeing tooling that let's us develop better UI apps with languages like haskell very much. Would it be possible to actually do an xcode project template for this? i mean one that would have all the pieces together already.

Verneri avatar Apr 05 '18 04:04 Verneri

This repo includes the completed project from the tutorial. stack build then build and run from Xcode should get you a working example app without any modifications needed.

otool -L can be used to check if your app binary is correctly linking to the library:

$ otool -L SwiftHaskell
SwiftHaskell:
	@rpath/SwiftAppLibrary.framework/Versions/A/SwiftAppLibrary (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

Edit: Reading again, the error is from SwiftAppLibrary calling into functions exported from the binary. So checking nm -g might be more useful:

$ nm -g SwiftHaskell
...
00000001000030e0 T _square
...

nanotech avatar Apr 12 '18 01:04 nanotech

I am also receiving the same error from a cloned version of this repository.

dyld: lazy symbol binding failed: Symbol not found: _square
  Referenced from: /Users/name/Library/Developer/Xcode/DerivedData/SwiftHaskell-dwmaozpbzaxvtwacjfoxvvpnqfwe/Build/Products/Debug/SwiftAppLibrary.framework/Versions/A/SwiftAppLibrary
  Expected in: flat namespace

dyld: Symbol not found: _square
  Referenced from: /Users/name/Library/Developer/Xcode/DerivedData/SwiftHaskell-dwmaozpbzaxvtwacjfoxvvpnqfwe/Build/Products/Debug/SwiftAppLibrary.framework/Versions/A/SwiftAppLibrary
  Expected in: flat namespace

It doesn't look like the square symbol is being exported.

$ nm -g build/SwiftHaskell  | grep square
0000000100002568 T _Main_zdfstableZZC6ZZCmainZZCMainZZCsquare1_info
00000001000b5048 D _Main_zdfstableZZC6ZZCmainZZCMainZZCsquare_closure
0000000100002598 T _Main_zdfstableZZC6ZZCmainZZCMainZZCsquare_in

Do you have any suggestions?

jprider63 avatar Dec 17 '20 17:12 jprider63

Something must have changed in GHC. The symbol is present after reverting to GHC 8.0.1:

$ nm -g build/SwiftHaskell  | grep square
00000001005a1f28 D _Main_square_closure
00000001000030b8 T _Main_square_info
00000001005a1f20 D _Main_zdfstableZZC6ZZCmainZZCMainZZCsquare1_closure
0000000100003098 T _Main_zdfstableZZC6ZZCmainZZCMainZZCsquare1_info
00000001005a1f30 D _Main_zdfstableZZC6ZZCmainZZCMainZZCsquare_closure
00000001000030d8 T _Main_zdfstableZZC6ZZCmainZZCMainZZCsquare_info
00000001000030e0 T _square

jprider63 avatar Dec 17 '20 19:12 jprider63

Can reproduce with the latest GHC 9.0.2, looks like the latest version where it works is 8.0.2.

GHC exports _square from Main.o, but when the SwiftHaskell executable is linked newer GHC versions pass -dead-strip to the linker, which cleans the _square symbol out.

azarovalex avatar May 27 '22 16:05 azarovalex