Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C++ mangled functions from C

I have a .lib file, source code of which I don't have.

I need an exported function from it, but I'm writing in C, and the function is C++ name-mangled. I can't write extern "C", because I don't have the source code.

How do I link mangled function without source code and switching to C++?

like image 915
Triang3l Avatar asked Sep 11 '12 13:09

Triang3l


2 Answers

Make C++ wrapper:

wrapper.cpp:

#include "3rdparty.hpp"

extern "C" int foo(int a, int b)
{
    return third_party::secret_function(a, b);
}

consumer.c:

extern int foo(int, int);

// ...

Build: (e.g. with GCC)

g++ -o wrapper.o wrapper.cpp
gcc -o consumer.o consumer.c
g++ -o program consumer.o wrapper.o -l3rdparty
like image 136
Kerrek SB Avatar answered Oct 05 '22 06:10

Kerrek SB


Write your own C++ wrapper over those functions and declare your wrapper functions with extern "C".

I'm not aware of any other way.

like image 40
Luchian Grigore Avatar answered Oct 05 '22 05:10

Luchian Grigore