Windows Static Library with Default Functions

2020-04-07 15:45发布

问题:

I would like to create a static library (.lib) in Windows that can be used in subsequent builds as a "backup" for undefined functions.

For instance let's say I have foobar.lib which has definitions for:

FOO
BAR

and I have some other program that defines FOO only and is to be built into a DLL that must export FOO and BAR. I want to be able to use foobar.lib to automatically export the default definition of BAR in the resulting DLL (and ignore the definition of FOO in foobar.lib).

I have tried sending foobar.lib to the linker but I get a multiple defined symbols error (/FORCE is supposed to override that but with strong warnings that it probably won't work as expected). I've also tried using /NODEFAULTLIB:foobar.lib but then it completely ignores the library and says BAR is undefined.

I am almost 100% certain there is a way to do this because I use an application that does this (Abaqus) to allow users to write plug-ins and not have to define all of the required exports for the plug-in DLL. And they do not use the /FORCE option.

回答1:

I figured out a solution (not sure if it is the only or best solution).

I was trying to define FOO and BAR in foobar.lib using one object file (foobar.obj). If I split it up into foo.obj and bar.obj and then use those to create foobar.lib the linker can effectively ignore the appropriate .obj files.

So the short answer is: one function per object file for the static library.