Import C DLL functions into a C ++ program

I have a third-party library written in C. It exports all its functions to a DLL.

I have a .h file and I'm trying to load a DLL from my C ++ program.

The first thing I tried was the environment of those parts where I # included a third-party library in

#ifdef __cplusplus extern "C" { #endif 

and in the end

 #ifdef __cplusplus } // extern "C" #endif 

But the problem was that all the functions related to the DLL files looked like this in their header files:

 a_function = (void *)GetProcAddress(dll, "a_function"); 

In fact, a_function was of type int (*a_function) (int *) . Apparently, the MSVC ++ compiler does not like this, while the MSVC compiler does not seem to mind.

So, I went through (brutal torture) and recorded them all on the model

 typedef int (*_x_a_function) (int *); // using _a_function will not work, C uses it! _x_a_function a_function ; 

Then, to associate it with the DLL code, in main ():

 a_function = (_x_a_function)GetProcAddress(dll, "a_function"); 

This is SEEMS to make the MUCH compiler, MUCH happier, but STILL complains about this final set of 143 errors, each of which says for every attempt to make a DLL link:

 error LNK2005: _x_a_function already defined in main.obj main.obj 

Errors detecting multiple characters .. sounds like work for extern ! So, I went and made ALL declarations of function pointers as follows:

function_pointers.h

  typedef int (* _x_a_function) (int *);
   extern _x_a_function a_function;

And in the cpp file:

function_pointers.cpp

  #include "function_pointers.h"
   _x_a_function a_function;

ALL are small and dandy .. besides linker errors now form:

 error LNK2001: unresolved external symbol _a_function main.obj 

Main.cpp includes "function_pointers.h", so it needs to know where to find each of the functions.

I am bewitched. Does anyone have any pointers to make me function? (Sorry, pun.)

+4
source share
4 answers

Similar linker errors assume that you defined all the functions in function_pointers.cpp, but forgot to add it to the project / makefile.

Either that, or you also forgot the "extern C" function in function_pointers.cpp.

+2
source

Usually you declare a function in your .h library, for example extern "C" __declspec(dllexport) int __cdecl factorial(int); , then create this function in yourlibrary.c file:

 extern "C" __declspec(dllexport) int __cdecl factorial(int x) { if(x == 0) return 1; else return x * factorial(x - 1); } 

After compiling your DLL you will get .dll and .lib files. The latter is used when you want to import your functions into a project. You put #include "yourlibrary.h" and #pragma comment(lib, "yourlibrary.lib") in your project, after which you can use int factorial(int) in your application.

+1
source

I believe that if you declared typedefs and / or a prototype as extern "C", you should also remember the definition of extern "C".

+1
source

When you bind C functions, prototypes will get a leading _ in front of them by default, so when you typedef with the same name

 typedef int (*_a_function) (int *); _a_function a_function 

you will get problems because the DLL already has a function called _a_function.

+1
source

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


All Articles