Attempting a C++ binding to Haskell: getting undefined reference errors

半城伤御伤魂 提交于 2019-12-11 02:48:58

问题


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++


来源:https://stackoverflow.com/questions/18562881/attempting-a-c-binding-to-haskell-getting-undefined-reference-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!