Using C ++ distorted functions from C

I have a .lib file whose source code is missing from me.

I need an exported function from it, but I write in C, and the function with a C ++ error. I cannot write extern "C" because I do not have the source code.

How to link a changed function without source code and switch to C ++?

+4
source share
3 answers

Make a C ++ shell:

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); // ... 

Assembly: (e.g. using GCC)

 g++ -o wrapper.o wrapper.cpp gcc -o consumer.o consumer.c g++ -o program consumer.o wrapper.o -l3rdparty 
+11
source

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

I do not know another way.

+5
source

The perverted name from the .lib file can be called from your program c. If the .lib you are referencing is stable and not constantly recompiled / updated, this solution may work for you.

I am not very familiar with windows, but How to view the contents of a Windows library (* .lib) or other search queries should show how this information can be obtained from .lib

Look for the function name in the output, most distortions will leave the name intact and simply decorate it with any other information.

Put this name in your C code with an explanatory comment ...

+1
source

Source: https://habr.com/ru/post/1433589/


All Articles