mstorsjo / llvm-mingw

An LLVM/Clang/LLD based mingw-w64 toolchain
Other
1.89k stars 184 forks source link

Linking against libc++.dll with a different name #333

Open justanotheranonymoususer opened 1 year ago

justanotheranonymoususer commented 1 year ago

Hello, I hope that it's OK to ask this question here. I have a slightly special use case - I want to link dynamically with libc++, which is the default, but I can't have the dll be named libc++.dll because of a conflict. I need to rename it, let's say, to my_libcpp.dll.

Is there any Clang command line flag to achieve that? Something similar to --defsym, but for dll names and not symbols?

Alternatively, is there perhaps a simple way to do a small change to the toolchain to have executables with my_libcpp.dll in the import table?

My last resort is to use a third-party tool, post-build, to modify the import table of the built executable, but I prefer to avoid that if possible.

mstorsjo commented 1 year ago

The easiest way to achieve this is probably to regenerate the libc++ import libraries.

Locate the libc++.dll that you want to have renamed, run gendef libc++.dll, which will spit out a libc++.def. This file starts with LIBRARY "libc++.dll", replace that with LIBRARY "my_libcpp.dll". Then regenerate a new import library from it with e.g. x86_64-w64-mingw32-dlltool -d libc++.def -l libc++.dll.a. Then replace the existing libc++.dll.a with the new one.

When you rename libc++.dll to my_libcpp.dll on disk, the PE/COFF exports table does still list that "this is the exports for libc++.dll", but I'm not sure if this makes a difference or not - I'd be interested in hearing back about whether this rename hack worked for you, or whether the DLL itself needs to expose the right name too.

justanotheranonymoususer commented 1 year ago

Thank you for the detailed reply! Late follow-up: For now, I found a workaround that's not perfect, but is good enough to avoid modifying the toolchain. Your guide might come in handy in the future, I'll let you know if I have the chance to try it. P.S. if there'd be an option to achieve this without modifying the toolchain, I'd still prefer using it.