5

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

Triang3l
  • 1,230
  • 9
  • 29
  • 3
    In short, you don't, you'll have to link from C++ using an identical compiler. – slugonamission Sep 11 '12 at 13:56
  • 3
    Note that you possibly have to adapt for calling convention too, as well as that the types used in that function signature are not compatible. Why don't you wrap them in some extern C functions? – PlasmaHH Sep 11 '12 at 13:57

4 Answers4

12

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
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • extern int foo(int, int), extern is implicit and not required to be stated. – Sammy Jul 20 '18 at 17:40
  • As @ slugonamission mentions above: make sure to use an identical compiler. Probably most open source compilers are binary compatible with GCC, but Visual Studio Compiler is likely to generate different mangled function names. – Johan Jan 18 '21 at 14:52
5

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

I'm not aware of any other way.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

The mangled name from the .lib file can be called from within your c program. If the .lib that you link to is stable, and not continually recompiled/updated, this solution might work for you.

I am not so familiar with windows, but How to See the Contents of Windows library (*.lib) or other searches should show how this information can be obtained from a .lib

Search for the name of the function in the output, most mangling will leave the name intact and just decorate it with all sorts of other information.

Place that name in your C code with an explanatory comment ...

Community
  • 1
  • 1
Chaim Geretz
  • 826
  • 5
  • 23
  • Well, my problem was that I wanted to call a C++ (not `extern "C"`) mangled function from C. But it seems that a C++ wrapper is the only option. – Triang3l Dec 02 '14 at 21:29
  • 1
    What this answer is suggesting, is to call not "foo()" but rather "MangledFoo()" directly. – Petr Oct 04 '16 at 13:38
  • No it's not. Look at the solution I have provided. Exactly what you need ;) @Triang3l – Sammy Jul 20 '18 at 17:41
1

Let us assume that you have a .c file (FileC.c) and you wish to call a function defined in .cpp (FileC++.cpp). Let us define the function in C++ file as:

void func_in_cpp(void) 
{ 
  // whatever you wanna do here doesn't matter what I am gonna say!
}

Do the following steps now (to be able to call the above function from a .c file):

1) With you regular C++ compiler (or www.cpp.sh), write a very simple program that includes your function name (func_in_cpp). Compile your program. E.g.

$ g++ FileC++.cpp -o test.o

2) Find the mangled name of your function.

$ nm test.out | grep -i func_in_cpp
[ The result should be "_Z11func_in_cppv" ]

3) Go to your C program and do two things:

void _Z11func_in_cppv(void);  // provide the external function definition at the top in your program. Function is extern by default in C.

int main(void) 
{
    _Z11func_in_cppv();   // call your function to access the function defined in .cpp file
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sammy
  • 257
  • 2
  • 8
  • Yes, it probably works for GCC mangling, but Visual Studio creates names with invalid characters. – Triang3l Jul 20 '18 at 17:41
  • 1
    The above answer is valid for Unix/Linux/MacOS environments. For e.g. you could use the above instructions while building for x86 or ARM on Ubuntu machine. – Sammy Jul 21 '18 at 06:02