Using C++ mangled functions from C
Asked Answered
G

4

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

Glaciate answered 11/9, 2012 at 13:55 Comment(2)
In short, you don't, you'll have to link from C++ using an identical compiler.Maremma
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?Backup
U
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
Urbana answered 11/9, 2012 at 13:58 Comment(2)
extern int foo(int, int), extern is implicit and not required to be stated.Portsalut
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.Folkways
Y
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.

Yearling answered 11/9, 2012 at 13:57 Comment(0)
V
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 ...

Vino answered 2/12, 2014 at 21:27 Comment(3)
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.Glaciate
What this answer is suggesting, is to call not "foo()" but rather "MangledFoo()" directly.Ramiroramjet
No it's not. Look at the solution I have provided. Exactly what you need ;) @GlaciatePortsalut
P
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
}
Portsalut answered 20/7, 2018 at 15:24 Comment(2)
Yes, it probably works for GCC mangling, but Visual Studio creates names with invalid characters.Glaciate
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.Portsalut

© 2022 - 2024 — McMap. All rights reserved.