-2

I am not sure if this even possible, but I am trying to link a static library (.lib) to a dll during compile time, but I am not having any luck.

The main dll contains an export called exportfunc. I am running procmon to monitor rundll32.exe, and I see it attempt to load my.dll from disk, but that's not aligning with what I am trying to accomplish as I am trying to statically link the external functions from my.lib so that it gets resolved at compile time.

Here's how I am generating the .lib file.

# Generate an object file using x86_64-w64-mingw32-gcc

x86_64-w64-mingw32-gcc -c -o myobj.o myprogram.c -I /tmp/include

# Generate dll using x86_64-w64-mingw32-gcc

x86_64-w64-mingw32-gcc -o my.dll -shared -Wall -Wno-pointer-to-int-cast -Os -I ../include -Wl,--enable-stdcall-fixup -Wl,--entry=WinDivertDllEntry myobj.o mydef.def -nostdlib -lkernel32 -ladvapi32 -luser32 -lmsvcrt -include stdio.h

# Generate lib file using x86_64-w64-mingw32-dlltool

x86_64-w64-mingw32-dlltool --dllname my.dll --def windivert.def --output-lib my.lib 2>/dev/null

# Use cl.exe to compile the final dll which statically links the .lb file cl.exe /W0 /D_USRDLL /D_WINDLL *.c *.cpp /MT /link /DLL /OUT:final.dll my.lib

user310457
  • 19
  • 4
  • 2
    Are you sure my.lib isn't just an import library? Why not just build a static library in the first place? – Miles Budnek Aug 23 '23 at 19:11
  • 1
    From the [dlltool docs](https://sourceware.org/binutils/docs/binutils/dlltool.html): _"The third file needed for DLL creation is the library file that programs will link with in order to access the functions in the DLL (an ‘import library’). This file can be created by giving the -l option to dlltool when it is creating or reading in a .def file."_ So yes, my.lib is just an import lib with shims to load the real code from my.dll. – Miles Budnek Aug 23 '23 at 19:15
  • ***I am not sure if this even possible*** It's definitely possible to link a static library to a dll. This is no different from lining a static library to an executable. – drescherjm Aug 23 '23 at 22:08
  • Looks like you are generating an import library for your dll. If you wanted a static library you have to build a static library not build a dll and generate an import library for it. – drescherjm Aug 23 '23 at 22:10
  • my.lib is not a static library. Do you want to link mylib.dll to final.dll? – Minxin Yu - MSFT Aug 24 '23 at 08:18

0 Answers0