Calling dynamic libraries from c-sources fails in ghci - macos

I have some C functions from a dynamic library (just zlibVersion here) that I'd like to call from bundled C code in a cabal package. Here I call it both directly as a foreign import and indirectly through the bundled C code to show that the latter crashes ghci, even after the former succeeds.
Main.hs:
module Main (main) where
import Foreign.C.String (CString, peekCString)
foreign import ccall "foreign_test" foreignTest :: IO ()
foreign import ccall "zlibVersion" zlibVersion :: IO CString
main :: IO ()
main = do
zlibVersion >>= peekCString >>= putStrLn
foreignTest
foreign_test.c:
#include <stdio.h>
#include <zlib.h>
void foreign_test() {
puts(zlibVersion());
}
It works fine from a compiled binary:
$ cabal build
$ dist/build/cabal-extra-libs-test/cabal-extra-libs-test
1.2.5
1.2.5
But the indirect call crashes ghci:
$ cabal repl
Preprocessing executable 'cabal-extra-libs-test' for
cabal-extra-libs-test-0.1.0.0...
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
1.2.5
dyld: lazy symbol binding failed: Symbol not found: _zlibVersion
Referenced from: /var/folders/pz/920gzhqn01q8d6vjkvy1yvdr0000gn/T/ghc6850_0/libghc_1.dylib
Expected in: flat namespace
dyld: Symbol not found: _zlibVersion
Referenced from: /var/folders/pz/920gzhqn01q8d6vjkvy1yvdr0000gn/T/ghc6850_0/libghc_1.dylib
Expected in: flat namespace
cabal-extra-libs-test.cabal:
name: cabal-extra-libs-test
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10
executable cabal-extra-libs-test
default-language: Haskell2010
main-is: Main.hs
build-depends: base
c-sources: foreign_test.c
extra-libraries: z
GHC is 7.10.3 with cabal-install 1.24.0.0 on OS X 10.11.5. Building with stack and stack ghci produces the same crash.

That's peculiar.. I don't know much about OSX but dyld: lazy symbol binding failed: Symbol not found: _zlibVersion gives the impression It thinks OSX is an underscore platform (e.g. one where C functions are mangled with an _).
Is this a standard GHC build? and can you try with 8.0.1? lots has changed in the linker.

Related

Haskell Stack doesnt work correctly on OSX Sierra

I can't get haskell's stack to work on my Mac.
I installed haskell by:
brew cask install haskell-platform
Which is specified here: https://www.haskell.org/platform/mac.html#osx-homebrewcask
I've also tried installing via the downloadable package at the same site.
I do the following commands:
stack new my-project
cd my-project
stack setup
stack build
stack exec my-project-exe
Per the documentation here:
https://docs.haskellstack.org/en/stable/README/#quick-start-guide
And I immediately see this:
[1] 43017 killed stack exec my-project-exe
I've also tried many other ways of installing haskell. I've actually been able to get something compiled and executed just using cabal, but I can't then get hlint, hindent, or ghc-mod to work, they all return a similar message to the one above. I'd love to be able to work with HaskForce in IntelliJ on my Mac, but I've never had luck getting haskell to work.
System Specs
macOS 10.12.6 (16G29) (Sierra)
Fresh uninstall/reinstall of XCode (accepted terms)
brew as package manager
Here's a gif of what happens as well: https://imgur.com/gallery/afYym7m
UPDATE
I blew away all stuff related to ghc, stack, and cabal and then installed this way
https://haskell-lang.org/get-started
Writing the little HelloWorld.hs script worked fine and showed Hello, World
However, doing
stack new someproj
cd someproj
stack build
Resulted in this:
➜ someproj stack build
[1 of 2] Compiling Main ( /Users/trevord2/.stack/setup-exe-src/setup-mPHDZzAJ.hs, /Users/trevord2/.stack/setup-exe-src/setup-mPHDZzAJ.o )
[2 of 2] Compiling StackSetupShim ( /Users/trevord2/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs, /Users/trevord2/.stack/setup-exe-src/setup-shim-mPHDZzAJ.o )
Linking /Users/trevord2/.stack/setup-exe-cache/x86_64-osx/tmp-Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 ...
someproj-0.1.0.0: configure (lib + exe)
Configuring someproj-0.1.0.0...
someproj-0.1.0.0: build (lib + exe)
Preprocessing library someproj-0.1.0.0...
[1 of 1] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Lib.o )
Preprocessing executable 'someproj-exe' for someproj-0.1.0.0...
[1 of 1] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/someproj-exe/someproj-exe-tmp/Main.o )
Linking .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/someproj-exe/someproj-exe ...
ld: warning: ignoring file /Users/trevord2/someproj/.stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/libHSsomeproj-0.1.0.0-6corWoTDCA69pE1sqNF8Id.a, file was built for archive which is not the architecture being linked (x86_64): /Users/trevord2/someproj/.stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/libHSsomeproj-0.1.0.0-6corWoTDCA69pE1sqNF8Id.a
Undefined symbols for architecture x86_64:
"_someprojzm0zi1zi0zi0zm6corWoTDCA69pE1sqNF8Id_Lib_someFunc1_closure", referenced from:
_Main_main1_info in Main.o
_S39Z_srt in Main.o
"_someprojzm0zi1zi0zi0zm6corWoTDCA69pE1sqNF8Id_Lib_someFunc1_info", referenced from:
_Main_main_info in Main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`gcc' failed in phase `Linker'. (Exit code: 1)
-- While building package someproj-0.1.0.0 using:
/Users/trevord2/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 build lib:someproj exe:someproj-exe --ghc-options " -ddump-hi -ddump-to-file"
Process exited with code: ExitFailure 1
Which I've seen before and was actually one of my initial problems
UPDATE 2: Created GitHub Issue
Here's the issue: https://github.com/commercialhaskell/stack/issues/3487
After talking with folks on #haskell IRC they recommended I create an issue as this is very peculiar behaviour from what I understand. I'll try make sure whatever we find out here is pushed up there too.

stack ghci does not load local module but ghci does

This is similar to the question at
stack ghci not loading up local modules?
but not exactly the same.
I have a directory with the files Main.hs and Shapes.hs where Main.hs has a reference to the module Shapes.
If I run
ghci Main.hs
everything works fine. This is version 7.10.
But if I run, in version 8,
stack ghci
:load Main.hs
I get the error
[1 of 1] Compiling Main ( Main.hs, interpreted )
Main.hs:3:1: error:
Failed to load interface for ‘Shapes’
It is not a module in the current program, or in any known package.
Failed, modules loaded: none.
I tried the solution on the link
:load Shapes.hs Main.hs
but it still does not work. I get the error
[1 of 2] Compiling Shapes ( Shapes.hs, interpreted )
[2 of 2] Compiling Main ( Main.hs, interpreted )
Main.hs:1:1: error:
The IO action ‘main’ is not defined in module ‘Main’
Failed, modules loaded: Shapes.
I found the following which discusses this second matter:
How to avoid "‘main’ is not defined in module ‘Main’" when using syntastic
But if I were to put in
main :: IO ()
in the Main.hs file, I still get errors. If I were to additionally add the line
main = return ()
and have these two additional lines at the end, then it would load. But after loading it just exits main and I no longer have access to the functions in main.
I don't see any reason why I need to use any IO features when I import some other module. How do I, in version 8, load a local module without using IO?
I fixed my own problem. There was a line in Main.hs at the top called
module Main where
that I removed. Now it works if I load in stack ghci with
:l Main.hs Shapes.hs

Can't compile a simple hello world using GHC 7.8rc2 and Windows 7 (or install packages with cabal)

I can't compile a simple hello world or install packages with cabal install when using ghc 7.8, cabal 1.18.1.3 and cabal-install 1.18.0.2.
When doing a cabal install stm (or any other package) from a command prompt Windows shows a "ghc.exe has stopped working" window, and the output is:
Reading available packages...
Choosing modular solver.
Resolving dependencies...
Ready to install stm-2.4.2
Extracting
Waiting for install task to finish...
C:\Users\%USER%\AppData\Roaming\cabal\packages\hackage.haskell.org\stm\2.4.2\stm-2.4.2.tar.gz
to C:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556...
Updating stm.cabal with the latest revision from the index.
Configuring stm-2.4.2...
creating C:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556\stm-2.4.2\dist\setup
creating C:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556\stm-2.4.2\dist
creating C:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556\stm-2.4.2\dist\setup
"c:\ghc-7.8.0\bin\ghc.exe" "--make" "-odir" "C:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556\stm-2.4.2\dist\setup" "-hidir" "C:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556\stm-2.4.2\dist\setup" "-i" "-iC:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556\stm-2.4.2" "-package-id" "Cabal-1.18.1.3-ac8664a5c71d605f2a4a8e21170969e0" "C:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556\stm-2.4.2\dist\setup\setup.hs" "-o" "C:\Users\%USER%\AppData\Local\Temp\stm-2.4.2-6556\stm-2.4.2\dist\setup\setup.exe"
Failed to install stm-2.4.2
Last 10 lines of the build log ( C:\Users\%USER%\AppData\Roaming\cabal\logs\stm-2.4.2.log ):
World file is already up to date.
cabal.exe: Error: some packages failed to install:
stm-2.4.2 failed during the configure step. The exception was:
user error
I installed(?) GHC 7.8 and cabal-install with the following steps
Downloaded GHC 7.8 RC2 (http://www.haskell.org/ghc/dist/7.8.1-rc2/ghc-7.8.0.20140228-i386-unknown-mingw32.tar.bz2)
Extracted the contents of the tar to folder $GHC$
Added $GHC$\bin and $GHC$\mingw\bing folder to the PATH.
Downloaded prebuilt cabal-install (
http://www.haskell.org/cabal/release/cabal-install-1.18.0.2/cabal.exe)
Copied cabal.exe to $GHC$\bin so it's in the path
Run cabal update
Run cabal install stm (or any other package)
Before doing all this, I uninstalled my previous Haskell Platform, deleted folders cabal and ghc from %USER%\AppData\Roaming, and other haskell stuff from the Program Files folder.
I am not sure if GHC needed any additional installation steps
I need GHC 7.8 because I want to try out GHCJS.
PS:
I also added LANG=C to my env variables so that compiling does not throw a UTF-8 encoding error.
cabal --version shows:
cabal-install version 1.18.0.2
using version 1.18.1 of the Cabal library
Which seems to be ok except that the cabal library shows up as 1.18.1 instead of 1.18.1.3, but i'm not sure that's an error.
ghc-pkg list shows:
c:/ghc-7.8.0\lib\package.conf.d:
Cabal-1.18.1.3
Win32-2.3.0.1
array-0.5.0.0
base-4.7.0.0
bin-package-db-0.0.0.0
binary-0.7.1.0
rts-1.0
bytestring-0.10.4.0
containers-0.5.4.0
deepseq-1.3.0.2
directory-1.2.0.2
filepath-1.3.0.2
(ghc-7.8.0.20140228)
ghc-prim-0.3.1.0
(haskell2010-1.1.1.1)
(haskell98-2.0.0.3)
hoopl-3.10.0.0
hpc-0.6.0.1
integer-gmp-0.5.1.0
old-locale-1.0.0.6
old-time-1.1.0.2
pretty-1.1.1.1
process-1.2.0.0
template-haskell-2.9.0.0
time-1.4.1
transformers-0.3.0.0
** EDIT **: Trying to compile stm manually by going into its folder and running ghc generates the same error window and gives the output:
EDIT 2: Actually, I can't even compile a simple hello world with ghc helloworld.hs:
main = putStrLn "Hello, World!"
Compiling with "-v3" gives the following (uninformative) output:
Glasgow Haskell Compiler, Version 7.8.0.20140228, stage 2 booted by GHC version 7.6.3
Using binary package database: C:\ghc-7.8.0\lib\package.conf.d\package.cache
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-ce20d91f79b43ede95508e8cfd2188b0
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-67cd00feecdc491a633c46b0f7652b46
wired-in package base mapped to base-4.7.0.0-d4a0afe8e5c5f0dff2237b1231b56978
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-f798cb95c80f3e3751ec33b413381b57
wired-in package dph-seq not found.
wired-in package dph-par not found.
Hsc static flags:
wired-in package ghc-prim mapped to ghc-prim-0.3.1.0-ce20d91f79b43ede95508e8cfd2188b0
wired-in package integer-gmp mapped to integer-gmp-0.5.1.0-67cd00feecdc491a633c46b0f7652b46
wired-in package base mapped to base-4.7.0.0-d4a0afe8e5c5f0dff2237b1231b56978
wired-in package rts mapped to builtin_rts
wired-in package template-haskell mapped to template-haskell-2.9.0.0-f798cb95c80f3e3751ec33b413381b57
wired-in package dph-seq not found.
wired-in package dph-par not found.
*** Chasing dependencies:
Chasing modules from: *asd.hs
Stable obj: []
Stable BCO: []
Ready for upsweep
[NONREC
ModSummary {
ms_hs_date = 2014-03-10 04:11:51 UTC
ms_mod = main:Main,
ms_textual_imps = [import (implicit) Prelude]
ms_srcimps = []
}]
*** Deleting temp files:
Deleting:
compile: input file asd.hs
Created temporary directory: C:\Users\facuq\AppData\Local\Temp\ghc6824_1
*** Checking old interface for main:Main:
[1 of 1] Compiling Main ( asd.hs, asd.o )
*** Parser:
*** Renamer/typechecker:
*** Desugar:
Result size of Desugar (after optimization)
= {terms: 7, types: 5, coercions: 0}
*** Simplifier:
Result size of Simplifier = {terms: 7, types: 5, coercions: 0}
*** Tidy Core:
Result size of Tidy Core = {terms: 7, types: 5, coercions: 0}
*** CorePrep:
Result size of CorePrep = {terms: 9, types: 6, coercions: 0}
*** Stg2Stg:
*** CodeOutput:
*** New CodeGen:
*** CPSZ:
*** CPSZ:
It's actually a bug in GHC; it was confirmed on the GHC mailing list, and two people can reproduce it (ghc's bug manager thread).
your ghc.exe path and cabal.exe path should be in your path variable
It has worked for me.

Simulating keystrokes with Haskell on Windows

I am trying to write a Haskell program that simulates keystrokes on Windows. I tried to call keybd_event and SendInput, but neither one compiled. I can run the program with the interpreter, though. When I try to build the program when it contains a binding to SendInput in winable.h, I get the error:
cabal install
...
[1 of 2] Compiling WindowsKeys ( dist\build\WindowsKeys\WindowsKeys-tmp\WindowsKeys.hs, dist\build\WindowsKeys\WindowsKeys-tmp\WindowsKeys.o )
[2 of 2] Compiling Main ( src\Main.hs, dist\build\WindowsKeys\WindowsKeys-tmp\Main.o )
Linking dist\build\WindowsKeys\WindowsKeys.exe ...
dist\build\WindowsKeys\WindowsKeys-tmp\WindowsKeys.o:fake:(.text+0x35d): undefined reference to `SendInput'
collect2: ld returned 1 exit status
cabal: Error: some packages failed to install:
WindowsKeys-0.1.0.0 failed during the building phase. The exception was:
ExitFailure 1
The verbose error is at http://pastebin.com/trg21N0x , but it doesn't seem to contain any more clues. I get a similar error when I try to use keybd_event.
The hsc file I wrote includes these headers:
#include "windows.h"
#include "winuser.h"
#include "winable.h"
Here is the C binding:
foreign import ccall unsafe "winable.h SendInput"
c_SendInput :: UINT
-> Ptr Input
-> CInt
-> IO UINT
I assumed that I could not call SendInput on winuser.h because of the #if :
#if (_WIN32_WINNT >= 0x0403)
WINUSERAPI UINT WINAPI SendInput(UINT,LPINPUT,int);
When I add a binding for _WIN32_WINNT , the value is 0x400.
I have version 2012.4.0.0 of the Haskell Platform. It came with a folder of headers containing the ones that I included. I could not find any other headers with the same names on my computer. I am using Windows 7 Professional, version 6.1 .
Thank you!
Here is WindowsKeys.cabal :
-- Initial WindowsKeys.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: WindowsKeys
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.8
extra-source-files: windows.h, winuser.h, winable.h
executable WindowsKeys
main-is: Main.hs
other-modules: WindowsKeys
build-depends: base ==4.5.*, Win32 ==2.2.*
hs-source-dirs: src
build-tools: hsc2hs
extra-libraries: user32
include-dirs: src
The build succeeds when I comment out the bindings to the keyboard functions.
I finally found that I had used the wrong calling convention. keybd_event and SendInput both need to be called with stdcall instead of ccall.

Haskell: http-wget hangs my program in windows

I've tried this on two computers. Here are my steps:
install wget on cygwin
cabal install http-wget
upgrade my PATH environment variable to include C:\cygwin\bin
open WinGHCi and type this:
From prompt:
GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :m + Network.HTTP.Wget
Prelude Network.HTTP.Wget> wget "http://www.google.com" [] []
Loading package bytestring-0.9.2.1 ... linking ... done.
Loading package Win32-2.2.2.0 ... linking ... done.
Loading package filepath-1.3.0.0 ... linking ... done.
Loading package old-locale-1.0.0.4 ... linking ... done.
Loading package old-time-1.1.0.0 ... linking ... done.
Loading package directory-1.1.0.2 ... linking ... done.
Loading package process-1.1.0.1 ... linking ... done.
Loading package failure-0.1.2 ... linking ... done.
Loading package transformers-0.2.2.0 ... linking ... done.
Loading package http-wget-0.6.2.3 ... linking ... done.
Nothing else happens after this line. The command doesn't complete. It just hangs forever. I'm very new to Haskell and don't know how to debug this issue.
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.4.2
I've tried the wget command in the cygwin prompt and it works as expected.
This works from WinGHCi:
let getGoogle = do (_, Just hout, _, _) <- createProcess (proc "wget" ["-q", "www.google.com", "-O", "-"]){ std_out = CreatePipe }; hGetContents hout
I got that from IRC from a gentleman named mcstar

Resources