Undefined references and gcc error when building Haskell executable with cabal? - haskell

I set up a simple cabal project to try and acquaint myself with it better, and I have a very simple Main.hs file:
main :: IO ()
main = putStrLn "Hello, World!"
So then I try to build it, using cabal new-configure and then cabal new-build. There's no problems for the configure command, but building it gives this:
[1 of 1] Compiling Main ( Main.hs, /home/redacted/Documents/Haskell/nixtut/dist-newstyle/build/x86_64-linux/ghc-8.0.1/newp-0.1.0.0/x/newp/build/newp/newp-tmp/Main.o )
Linking /home/daniel/Documents/Haskell/nixtut/dist-newstyle/build/x86_64-linux/ghc-8.0.1/newp-0.1.0.0/x/newp/build/newp/newp ...
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: //nix/store/siks2gcfwx6qwh27m7c5r5lixcr621bd-glibc-2.27/lib/libpthread.so.0: undefined reference to `_IO_enable_locks#GLIBC_PRIVATE'
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: //nix/store/siks2gcfwx6qwh27m7c5r5lixcr621bd-glibc-2.27/lib/libpthread.so.0: undefined reference to `__mmap#GLIBC_PRIVATE'
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: //nix/store/siks2gcfwx6qwh27m7c5r5lixcr621bd-glibc-2.27/lib/libpthread.so.0: undefined reference to `__munmap#GLIBC_PRIVATE'
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: //nix/store/siks2gcfwx6qwh27m7c5r5lixcr621bd-glibc-2.27/lib/libpthread.so.0: undefined reference to `__mprotect#GLIBC_PRIVATE'
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: //nix/store/siks2gcfwx6qwh27m7c5r5lixcr621bd-glibc-2.27/lib/libpthread.so.0: undefined reference to `__tunable_get_val#GLIBC_PRIVATE'
/nix/store/6yjpyqkx6d9k5f2s2g8h9kz40q6rz1yx-binutils-2.31.1/bin/ld: //nix/store/siks2gcfwx6qwh27m7c5r5lixcr621bd-glibc-2.27/lib/libpthread.so.0: undefined reference to `__sigtimedwait#GLIBC_PRIVATE'
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)
Here is my .cabal file:
name: newp
version: 0.1.0.0
synopsis: toying around
-- description:
homepage: none
-- bug-reports:
license: BSD-3-Clause
license-file: LICENSE
author: redacted
maintainer: none
-- copyright:
category: practice
extra-source-files: CHANGELOG.md
executable newp
main-is: Main.hs
--other-modules:
-- other-extensions:
build-depends: base ^>=4.9.0.0
-- hs-source-dirs:
default-language: Haskell2010
I can't seem to understand what's wrong. GCC is installed, I'm not trying to link any external library, I'm not using any other modules, I have no idea know what these references allude to.
Any help is appreciated, because frankly, I don't know where to begin with this. I've tried looking online, but all the situations are different and the solutions don't seem to apply to me. I noticed that for others a similar error occurs when there's an issue with linking or not including modules in the cabal file, but I'm not sure how or if that applies here.

While I don't have a definitive answer as to why the error occurred, after reinstalling nix I could finally build the project. So the issue was not with cabal.

Related

Haskeline throws linking error in stack build

I'm trying to build haskell project via stack build command, but it throws excepton:
-- While building package %some secret project name%-0.1.0.0 (scroll up to its section to see the error) using:
/home/%some secret username%/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_3.2.1.0_ghc-8.10.7 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0 build lib:%some secret project name% exe:%exe name% --ghc-options " -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
Also it outputs following error in log file:
Linking .stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0/build/%secret name%/%secret name% ...
/usr/bin/ld.gold: error: cannot find -ltinfo
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/haskeline-0.8.2/libHShaskeline-0.8.2.a(Terminfo.o)(.text..LsmG9_info+0x76): error: undefined reference to 'set_curterm'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/haskeline-0.8.2/libHShaskeline-0.8.2.a(Terminfo.o)(.text..LsmG9_info+0xcc): error: undefined reference to 'set_curterm'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/haskeline-0.8.2/libHShaskeline-0.8.2.a(Terminfo.o)(.text..LsmG9_info+0x147): error: undefined reference to 'set_curterm'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/haskeline-0.8.2/libHShaskeline-0.8.2.a(Terminfo.o)(.text..LsmG9_info+0x1bb): error: undefined reference to 'set_curterm'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/terminfo-0.4.1.4/libHSterminfo-0.4.1.4.a(Base.o)(.text..Lr5WM_info+0xb2): error: undefined reference to 'tigetnum'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/terminfo-0.4.1.4/libHSterminfo-0.4.1.4.a(Base.o)(.text..Lr5WN_info+0xb2): error: undefined reference to 'tigetflag'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/terminfo-0.4.1.4/libHSterminfo-0.4.1.4.a(Base.o)(.text..Lr5WO_info+0xb2): error: undefined reference to 'tigetstr'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/terminfo-0.4.1.4/libHSterminfo-0.4.1.4.a(Base.o)(.text..Ls64w_info+0x354): error: undefined reference to 'tparm'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/terminfo-0.4.1.4/libHSterminfo-0.4.1.4.a(Base.o)(.text..Ls66r_info+0x129): error: undefined reference to 'setupterm'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/terminfo-0.4.1.4/libHSterminfo-0.4.1.4.a(Base.o)(.text..Ls66r_info+0x22a): error: undefined reference to 'del_curterm'
/home/messieurme/.stack/programs/x86_64-linux/ghc-tinfo6-8.10.7/lib/ghc-8.10.7/terminfo-0.4.1.4/libHSterminfo-0.4.1.4.a(Base.o)(.text..Ls68h_info+0x141): error: undefined reference to 'tputs'
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)
My code haven't any errors in .hs files. Also I can invoke any function (even main) via stack ghciand it works fine.
After some researches I found that if I'll remove haskeline from dependencies in %project name%.cabal file, stack build will execute witout any problems.
I use haskeline only in app/main.hs and my code is from this site. Here it is
import System.Console.Haskeline
main :: IO ()
main = runInputT defaultSettings loop
where
loop :: InputT IO ()
loop = do
minput <- getInputLine "% "
case minput of
Nothing -> return ()
Just "quit" -> return ()
Just input -> do outputStrLn $ "Input was: " ++ input
loop
Here is part of .cabal file where I'm adding haskeline:
executable hi
hs-source-dirs: app
main-is: %Some secret main name%.hs
ghc-options: -Wall -Werror -threaded -rtsopts -with-rtsopts=-N
build-depends: base >= 4.9 && < 5
, %extra deps%
, haskeline
default-language: Haskell2010
Version of haskeline doesn't matter. I tried to set previous versions and error occured with each of them.
I tried to google problem, but it seems that nobody faced it before or it's too trivial to ask about.
I'm using linux mint, maybe it impacts somehow.
I fully reinstalled stack but problem is still with me. Deleting temporary files like .stack-work within project also didn't help
The problem were due to the missing libtinfo-dev. Once I had library installed the problem disappeared. Thanks to the answer in comments

Haskell Failed to load interface for module

Hello i am trying to use a module inside another module but it seems it just won't work.I tried updating the cabal file,stack,i reinstalled the platform,ghc and everything and it just won't let it import modules.I tried adding in the cabal file both other-modules and home-modules section ..to no effect.What could be the problem?
Modules:
module Test where
test::IO()
test=do
elem<-getLine
print elem
module Main where
import Test
main :: IO ()
main = do
putStrLn "hello world"
Cabal-build renders this error:
$ cabal build
Resolving dependencies...
Configuring console-0.1.0.0...
Warning: To use the 'default-language' field the package needs to specify at
least 'cabal-version: >= 1.10'.
Preprocessing executable 'console' for console-0.1.0.0..
Building executable 'console' for console-0.1.0.0..
<no location info>: warning: [-Wmissing-home-modules]
These modules are needed for compilation but not listed in your .cabal file's other-modules: Test
<no location info>: warning: [-Wmissing-home-modules]
These modules are needed for compilation but not listed in your .cabal file's other-modules: Test
When i build with Stack i get this error:
C:\<path>\Main.hs:4:1: error:
Failed to load interface for `Test'
Use -v to see a list of the files searched for.
Failed, modules loaded: none.
H>>= :module + *Main
Cabal file:
name: console
version: 0.1.0.0
-- synopsis:
-- description:
homepage: https://github.com/githubuser/console#readme
license: BSD3
license-file: LICENSE
author: Bercovici Adrian Simon
maintainer: example#example.com
copyright: 2018 Bercovici Adrian Simon
category: Web
build-type: Simple
cabal-version: >=1.2
extra-source-files: README.md
Executable console
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
other-modules: Test
The other-modules: directive must go inside the Executable console stanza.

Missing C library:

I have written some simplistic code to call a function in a C library. The code compiles (and a similar piece works for a standard C library) but when I compile and link, ghc cannot find the C library. My cabal files is:
executable ttclient
main-is: MainFFI4TT.hs
build-depends: base
default-language: Haskell2010
hs-source-dirs: src
other-modules:
Include-dirs: treetaggerSourceC
Includes: tagger-api.h
extra-libraries: libtreetagger
extra-lib-dirs: /usr/lib
the file libtreetagger.so is in /usr/lib but cannot be found. what else would be reuqired? It does not make a difference whether I use stack build or cabal install (8.0.2 or 8.0.1). cabal is version 1.24.0.0 and the system is linux debian stretch.
I would prefer to keep the library in a user dir and not a system dir. Can this be achieved?
The haskell code is:
foreign import ccall "tagger-api.h init_treetagger"
c_initTreeTagger :: CString -> IO ()
-- void init_treetagger(char *param_file_name);
mainFFItest :: IO ()
mainFFItest = do
c_initTreeTagger "german-utf8.par"
I assume you're getting the error message:
* Missing C library: libtreetagger
If so, the likely problem is that extra-libraries should list libraries without the lib prefix:
extra-libraries: treetagger

cabal test fails to link its own objects

I am trying to add a detailed-0.9 test suite to a cabal package that provides an executable. Thus I added:
test-suite igor2-test
type: detailed-0.9
hs-source-dirs: src
test-module: Igor2.Tests
build-depends: Cabal
There is no library in this package, but the executable uses the same source tree. During cabal test it fails linking:
Preprocessing test suite 'igor2-test' for igor2-0.7.1.3...
[1 of 5] Compiling Syntax.Name ( src/Syntax/Name.hs, dist/build/Syntax/Name.o )
... (more compiling)
[1 of 5] Compiling Syntax.Name ( src/Syntax/Name.hs, dist/build/Syntax/Name.p_o )
... (more compiling)
In-place registering igor2-test-0.7.1.3...
[1 of 1] Compiling Main ( dist/build/igor2-testStub/igor2-testStub-tmp/igor2-testStub.hs, dist/build/igor2-testStub/igor2-testStub-tmp/Main.o )
Linking dist/build/igor2-testStub/igor2-testStub ...
/full/path/to/igor2/dist/build/libigor2-test.a(Tests.o): In function `cd3z_info':
(.text+0x2d7): undefined reference to `igor2zmtestzm0zi7zi1zi3_SyntaxziName_arrowName3_closure'
... (more undefined references)
collect2: error: ld returned 1 exit status
Why is this failing? The function arrowName is defined in the module Syntax.Name and this module is compiled. It is not used in the test module though.
I also tried turning the package into a library and having the test-suite build-depends on the library. The linker error proceeds.
This is the Haskell Platform from OpenSuSE 13.1, GHC 7.8.3 which locks Cabal to 1.18.1.3.
Today I stumbled on the same issue.
I found the following workaround:
Add
other-modules: Syntax.Name
to your test-suite declaration.

plugins package unknown symbol when using cabal

I'm messing around with the plugins package however I bumped into a problem.
Here's the code:
Util/Header.hs
module Util.Header(PT(..)) where
data PT a = PT a deriving Show
Plug.hs
module Plug(helloPlugin) where
import Util.Header
helloPlugin :: PT Int
helloPlugin = PT 1
Main.hs
module Main where
import Util.Header
import System.Plugins
main :: IO ()
main = do
mv <- load "Plug.o" ["."] [] "helloPlugin"
case mv of
LoadFailure msg -> print msg
LoadSuccess _ v -> print $ show (v :: PT Int)
This all works fine then compiling with ghc. Building with Cabal works fine as well, but when I run the executable I get this error:
plugintest: /home/kevin/.cabal/lib/plugins-1.5.4.0/ghc-7.6.3/HSplugins-1.5.4.0.o: unknown symbol `ghczm7zi6zi3_ErrUtils_zdsinsertzuzdsgo5_info'
plugintest: user error (resolvedObjs failed.)
My very minimalistic cabal file:
name: plugintest
version: 0.1.0.0
license-file: LICENSE
build-type: Simple
cabal-version: >=1.8
library
hs-source-dirs: src
exposed-modules: Util.Header
build-depends: base ==4.6.*, plugins ==1.5.*
executable plugintest
main-is: Main.hs
build-depends: base ==4.6.*, plugins ==1.5.*, plugintest == 0.1.0.0
hs-source-dirs: src
Now I assume the problem is that it can't find the "ErrUtils" module which is part of the ghc package installed in /usr/lib/ghc-7.x.x.
Since it's using cabal it'll use the $HOME/.cabal/lib/ instead.
Now I obviously wouldn't want to use /usr/lib if I wanted to make it distributable. Sadly I'm not very familiar with how packages are managed nor am I familiar with the plugins package.
I have a feeling this is extremly nooby but I wasn't able to find a solution myself.
So a few questions:
How can I get my dependencies to work in a way to make this distributable?
It seems I'll need to know beforehand what my Plugin.o files will depend on before actually being able to use them (If I understand correctly).
Is there a way to package a .o files that I wouldn't have to worry about this problem? (Sorry if this question is too vague, feel free to ignore)
Thanks in advance!
Ok, so I had the exact same problem.
Here is a workaround I found
Change the load call to
load "Plug.o" [".","dist/build/plugintest/plugintest-tmp"] [] "testplugin"
Make sure you compile the thing with -c or by using the "make" library from plugins.
Quite annoyed by this... The error suggests it is having issues linking against the standard libs, so why does showing it these .o files fix it?
Anyways, this worked for me, and didn't require a ton of mucking around with .cabal files.
You must declare your exported- and other- modules in order for Cabal to package them all together. For instance (from https://github.com/tel/happstack-heroku-test)
name: hktest -- note the name here names
-- the *library* which is a package name
-- in scope when building the executable
...
library
exposed-modules:
HKTest
other-modules:
-- there aren't any, but there could be some
build-depends: base >= 4.6 && <4.7
...
, mtl >= 2.1.2
hs-source-dirs: src
executable server
main-is: Server.hs
other-modules:
-- there might be some use to having these here,
-- but they'll be harder to get into GHCi, so I wouldn't
-- recommend it---just put them in the library part
build-depends: base >=4.6 && <4.7
, hktest -- note that I grab all the hktest
-- modules here
hs-source-dirs: exe
If I leave out one of those modules I'll likely get a build error as Cabal compiles files which expect to be able to find symbols that haven't been packaged.
In your case, since you're building an executable, the common pattern exemplified above is to put all of your code into a library and then have the executable side depend upon that library. For instance, in this example the complete text of exe/Server.hs is
module Main where
import qualified HKTest as HK
main :: IO ()
main = HK.main

Resources