I'm using a C library in my C++ project, which uses Cmake. In that project, I'm building a plugin from that library. During the development of the project, I used to add files like this to my plugin library in cmake:
add_library(plugin SHARED
impl.cpp
Interface.h
${LIBBTC_REPO}/src/base58.c
${LIBBTC_REPO}/src/bip32.c
${LIBBTC_REPO}/src/block.c
${LIBBTC_REPO}/src/ripemd160.c
...
)
And everything was going fine. But after using the function ripemd160()
, my shared library started giving an undefined symbol error:
plugin.so: undefined symbol: _Z9ripemd160PKhjPh
When studying the contents of the plugin with nm -g plugin.so | grep -i ripe
, I got:
000000000002e1cb T ripemd160
U _Z9ripemd160PKhjPh
which shows that the original C function is not mangled and is not available, but C++ is expecting a mangled function name. That I don't understand.
What surprises me is that I also call other functions in other files. For example the function utils_hex_to_bin()
, but I don't get that undefined symbol error there.
Why is C++ expecting this function to specifically be mangled, while all other C functions not? Is there a better way to do what I'm doing? Am I linking this incorrectly?
Btw, that file, ripemd160.c
is the only file in the library that has the header file beside it, instead of being in the include directory. I don't know if that makes a difference.
I also tried creating another module for that library in CMake, but that doesn't help.
Please advise.
EDIT:
Just to be clear, I never decalred any of the functions in the library. I just include headers from that library and that takes care of declarations. That's why I never mentioned extern "C"
.
Solved: Wrapping the include with extern "C"
solved the problem. Apparently the library dev forgot to do that.