1

I've uploaded the project as a zip file so you can try it out. https://dl.dropboxusercontent.com/u/35032740/ShareX/2015/11/Buggy.zip

I wanted to write a wrapper around the clipper library. The code compiles fine with cabal build, runs with cabal run but cabal repl produces this error:

Preprocessing executable 'Buggy' for Buggy-0.1.0.0...
GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
GHC runtime linker: fatal error: I found a duplicate definition for symbol
   _ZNSt6vectorIN10ClipperLib8IntPointESaIS1_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS1_S3_EERKS1_
whilst processing object file
   dist\build\Buggy\Buggy-tmp\wrapper.o
This could be caused by:
   * Loading two different object files which export the same symbol
   * Specifying the same object file twice on the GHCi command line
   * An incorrect `package.conf' entry, causing some object to be
     loaded twice.
ghc.exe: panic! (the 'impossible' happened)
  (GHC version 7.10.2 for x86_64-unknown-mingw32):
        loadObj "dist\\build\\Buggy\\Buggy-tmp\\wrapper.o": failed

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

For reference, here's the cabal file

-- Initial Buggy.cabal generated by cabal init.  For further documentation,
--  see http://haskell.org/cabal/users-guide/

name:                Buggy
version:             0.1.0.0
-- synopsis:
-- description:
-- license:
license-file:        LICENSE
author:              Luka Horvat
maintainer:          lukahorvat9@gmail.com
-- copyright:
-- category:
build-type:          Simple
-- extra-source-files:
cabal-version:       >=1.10

executable Buggy
  main-is:             Main.hs
  c-sources:           clipper.cpp
                     , wrapper.cpp
  -- other-modules:
  -- other-extensions:
  build-depends:       base >=4.8 && <4.9
  -- hs-source-dirs:
  default-language:    Haskell2010
  extra-libraries:     stdc++

Any ideas what the cause might be here? I'm running Windows 10, 64bit.

Luka Horvat
  • 4,283
  • 3
  • 30
  • 48

2 Answers2

2

I don't know the details of object file formats on Windows, so I'm guessing a bit.

Probably clipper.o and wrapper.o both define a weak symbol named _ZNSt6vectorIN10ClipperLib8IntPointESaIS1_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS1_S3_EERKS1_. (I see the same on Linux.) This probably came from a template instantiation (of vector). Weak symbols instruct the system linker to just pick any copy of the symbol if it encounters duplicates.

GHCi on Windows doesn't use the system linker, it has its own runtime linker that can load object files into itself while it runs. As a result it is generally not feature compatible with the system linker. Probably the runtime linker does not understand weak symbols, at least on Windows (https://ghc.haskell.org/trac/ghc/ticket/3333). From the error you got, we can assume that it treats them as regular symbols, and two regular symbols are not allowed to have the same name.

As a workaround, you may be able to build your C++ files with -fno-weak as described in https://stackoverflow.com/a/26454930/190376.

If that doesn't work, an alternative is to build your C++ files into a DLL, which you can have GHCi load using the system dynamic loader, avoiding this whole issue. On Linux this would look like

g++ wrapper.cpp clipper.cpp -shared -fPIC -o libclipper.so
ghci -L. -lclipper

though I imagine the details are different on Windows.

Community
  • 1
  • 1
Reid Barton
  • 14,951
  • 3
  • 39
  • 49
1

The specific error isn't what I'm used to seeing, but those backslashes say you're on Windows, and this otherwise looks like GHC bug #3242 which has been causing pain for years now. Good news: the cause was finally isolated two weeks ago. Bad news: the fix didn't make the deadline for 7.10.3, though at least the 8.0.1 milestone seems secure at this point.

Probably still worth posting your error text to that bug's thread; mine is only an educated guess, someone there will know for sure.