2

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.

4

1 回答 1

1

community-wiki 答案以供将来参考。解决方案是通过像这样更改 cabal 文件来更改库呈现给链接器的顺序。

include-dirs: ./c-src
ghc-options: -Wall -threaded -lHSCPlusPlusBinding-0.1.0.0 -lA_C -lA -lstdc++
于 2016-03-19T23:26:39.850 回答