How to export characters to window DLLs using MinGW?

I am trying to create a C ++ library that has one public function using MinGW.

I think that I read enough textbooks , browsed the Internet, but nothing works, the error is always the same. Undefined function reference.

Here is the library title (findPoints.h):

#ifndef FIND_POINTS_H #define FIND_POINTS_H #ifdef BUILDING_FIND_POINTS_DLL #define FIND_POINTS_DLL_PREFIX __declspec(dllexport) #else #define FIND_POINTS_DLL_PREFIX __declspec(dllimport) #endif #ifdef __cplusplus extern "C" { #endif FIND_POINTS_DLL_PREFIX void findPoints(uint8_t* data, int height, int width, int* cx, int* cy, int* lx, int* ly, int* rx, int* ry, int* quality); #ifdef __cplusplus } #endif #endif 

Here is the function declaration in the source file (findPoints.h):

 #include "findPoints.h" //... void FIND_POINTS_DLL_PREFIX findPoints(uint8_t* data, int height, int width, int* cx, int* cy, int* lx, int* ly, int* rx, int* ry, int* quality) { std::thread worker(findDroplet, data, height, width, cx, cy, lx, ly, rx, ry, quality); worker.detach(); } 

The file has a length of about 1000 lines (all are part of the findDroplet declaration, depending only on standard libraries), so I skipped this, the error does not seem to exist.

To make sure this is correct, I include it in the main.cpp file. Although it compiles when I use:

 gcc main.cpp findPoints.cpp -o findPoints.exe 

I cannot create a dll that could be used (I need a DLL). This is what I do:

 g++ -c -O3 -DBUILDING_FIND_POINTS_DLL findPoints.cpp g++ -c main.cpp g++ -shared -o findPoints.dll -Wl,--out-implib,libfindPoints.a g++ -o findPoints.exe main.o -L. -lfindPoints 

Error with error message:

 main.o:main.cpp:(.text+0x1d5): undefined reference to `_imp__findPoints' 

I tried all kinds of permutations of prefixes, but nothing works. It is always not possible to find findPoins, depending on the prefixes it may have some prefixes or suffixes, but it never exists. I tried dlltool, but it never lists this function at all.

It seems that if the findPoints function from the cpp file was completely ignored, the error message remains unchanged if I change its name. They have the same signature, because otherwise their compilation will not work.

EDIT: More research: I was able to create a dll library and an exe program that uses it with Visual Studio. It works (and crashes if the DLL is missing). But if I try to use MingGW to compile a program and associate a DLL with it, the error remains the same. The problem is solved now, but at the cost of a hard workaround. However, I would like to know the answer because I do not like Visual Studio.

+5
source share
1 answer

When you create a DLL, you are not passing any objects to g++ .

Replace

 g++ -shared -o findPoints.dll -Wl,--out-implib,libfindPoints.a 

with

 g++ -shared -o findPoints.dll findPoints.o -Wl,--out-implib,libfindPoints.a 

in your build sequence.

+1
source

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


All Articles