Tuesday, February 3, 2009

De transportatione supprocedurarum omnium ex bibliotheca statica

Interesting fact: with Visual Studio, there appears to be no way for a dynamic library to automatically reëxport functions imported from a static library.

With the GCC toolchain this is relatively easy: ld supports an argument --whole-archive which causes any following static libraries to be slurped entire into the output assembly (and a corresponding --no-whole-archive tag, if you need to interleave this with ordinary archive inclusions). Then all you need is to ensure that you're exporting the functions properly (e.g. with -fvisibility=default or __attribute__((visibility("default")))).

Visual Studio's linker does not make this so easy. Even if you have __declspec(dllexport) in the headers in all the right places, functions et al. thus tagged will only be included and exported if they were actually used in the assembly being built. This means you have to explicitly name every exported function in the library.

It's possible to get around doing this manually by using a .def or .cpp file autogenerated from the output of DUMPBIN, but at that point it's probably easier just to convert the static library to a dynamic one and distribute that alongside your DLL instead.

No comments: