-1

I build a shared-object using Clang++. The major functions are located in 'extern "C"' block. The building is fine, and I can call these functions using JNA.

When I added the flag "-lstdc++" (for using some C++11 features) - I got runtime error:

undefined symbol: _ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm

I run

nm target/classes/linux-x86-64/libCloudCryptoLibrary.so  |grep init

then I get

                 U _ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm
                 U _ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEmc
0000000000004860 T _init
0000000000024360 T init

When I remove that flag, and run nm again, I get:

                 U _ZNKSt5ctypeIcE13_M_widen_initEv@@GLIBCXX_3.4.11
000000000026b920 b _ZStL8__ioinit
000000000026b9e0 b _ZStL8__ioinit
0000000000004508 T _init
0000000000021000 T init

What can I do for correct it?

Azriel Berger
  • 160
  • 4
  • 17
  • 5
    You've use a `std::string`. It's a c++ object. You can't `extern "C"` it. – Richard Hodges Sep 14 '16 at 08:58
  • 1
    @RichardHodges Yes you can use C++ objects in an extern C declaration, extern "C" only concerns the linkage. I guess if you could fix this by using `-stdlib=libstdc++ -static -lstdc++` to link that statically, which would at least resolve the depency. Not sure static linkage is the right solution though. – Klemens Morgenstern Sep 14 '16 at 10:45

1 Answers1

1

I found a solution: I have a function named "init" in the extern "C" block (as you can see the line "0000000000021000 T init"). I renamed it to "initMyClass" and the problem disappear.

Azriel Berger
  • 160
  • 4
  • 17
  • 2
    `extern "C"` cannot be used on overloaded functions, because name mangling is necessary to distinguish overloads. So this answer makes sense: it avoid overloading `init`. – MSalters Sep 14 '16 at 10:58