Attempting a C++ binding to Haskell: getting undef

2019-07-19 15:24发布

问题:

I am attempting a C++ binding in Haskell and running into "undefined reference" errors when calling the binding.

I've make a small project (http://github.com/deech/CPlusPlusBindings) to illustrate the problem. It includes a small C++ class, a C wrapper, a C test script and a Haskell binding and a test script. The C test script works, the Haskell one gives me:

  [1 of 1] Compiling Binding          ( dist/build/Binding.hs, dist/build/Binding.o )

  src/Binding.chs:6:26: Warning: Defined but not used: `res'
  In-place registering CPlusPlusBinding-0.1.0.0...
  Preprocessing executable 'binding_test' for CPlusPlusBinding-0.1.0.0...
  [1 of 1] Compiling Main             ( src/BindingTest.hs, dist/build/binding_    /binding_test-tmp/Main.o )

 src/BindingTest.hs:3:1: Warning:
     Top-level binding with no type signature: main :: IO ()
     Linking dist/build/binding_test/binding_test ...
     ../CPlusPlusBinding/c-src/libA_C.a(A_C.o): In function      `A_static_function':

     ../CPlusPlusBinding/c-src/A_C.cpp:4:0:
        undefined reference to `A::static_function()'
     ../CPlusPlusBinding/c-src/libA_C.a(A_C.o): In function `A_member_function':

     ../CPlusPlusBinding/c-src/A_C.cpp:7:0:
        undefined reference to `A::member_function()'
     ../CPlusPlusBinding/c-src/libA_C.a(A_C.o): In function `A_new':

     ../CPlusPlusBinding/c-src/A_C.cpp:10:0:
        undefined reference to `operator new(unsigned long)'
     collect2: error: ld returned 1 exit status

Running cabal configure, and cabal build will compile the C++ and C bindings and reproduce the error.

Update

I've solved the problem as outlined in the comment below. It appears I can't answer my own question, though.

回答1:

community-wiki answer for future reference. The solution was to change the order the libraries were presented to the linker, by altering the cabal file like so.

include-dirs: ./c-src
ghc-options: -Wall -threaded -lHSCPlusPlusBinding-0.1.0.0 -lA_C -lA -lstdc++