How to properly export symbols to DLL with MinGW

56 Views Asked by At

I want to export specific symbols to a dynamic library for Windows and hide the others.

My simple example is as follows (running on Mac M2 and brew-installed mingw-64):

test.c

#include <stdio.h>

#define DLL_EXPORT __declspec(dllexport)

DLL_EXPORT void exported_function(void) {
    printf("This is an exported function\n");
}

void private_function(void) {
    printf("This is a private function\n");
}

Build

$ brew install mingw-w64
$ x86_64-w64-mingw32-gcc -c test.c -fvisibility=hidden -shared -O3 -s -o test.dll
$ nm test.dll
         U __imp___acrt_iob_func
         U __mingw_vfprintf
00000050 T exported_function
00000060 T private_function

Why is the private_function visible? What am I missing?

Edit: Tried -Wl,--exclude-all-symbols, does not work

Edit2: -Wl,--exclude-all-symbols -Wl,--retain-symbols-file=test.sym works, still wondering why declspec doesn't do the trick.

test.sym

exported_function

Edit3: This works as well:

$ x86_64-w64-mingw32-gcc -shared -O3 -s test.c -o test.dll
& strings test.dll | grep private_function || echo "Private function not visible"
0

There are 0 best solutions below