[FFmpeg-devel] Fix libssh static linkage on Windows

Matt Oliver protogonoi at gmail.com
Wed Feb 5 15:21:34 CET 2014

On windows there are 2 ways to link to a function found in an external
shared library (.dll on windows). The first is to generate an export
library with your shared dll. This export library looks just like a static
lib but all the functions found in it point out to the external dll. The
second method is to declare each function as dllimport. This bypasses
needing to link to the export lib by explicitly stating its from a dll in
the code.

dllimport is placed in front of any function or data declarations in code
to specify that they are found in an external .dll (.so shared library on
linux). If a function is declared as dllimport then the linker will only
look for that function in an external dll even if that function has been
provided with a static library. So linking will always fail if using a
static library to provide a function as the linker will only allow for an
external shared function.

libssh by default falls back to using dllimport if no explicit option is
passed to it to tell it otherwise. The patch just adds that option so
dllimport isnt used by default. This means that a static library can be
used for linking while still supporting dlls if required as the export lib
can be linked in instead. Since a dll export lib looks exactly the same as
a static lib then shared/static functionality is determined simply by how
that .lib was generated. So I may not have explained it very well but by
adding the patch its possible to use either static or shared linkage of
libssh just by linking against whichever .lib you have. Whereas without the
patch it is impossible to statically link.

The current build script links against the provided .lib so this does not
break anything in anyway.

On 6 February 2014 01:04, Nicolas George <george at nsup.org> wrote:

> Le septidi 17 pluviôse, an CCXXII, Matt Oliver a écrit :
> > Currently unless told to do otherwise libssh.h will automatically set all
> > declarations to dllimport linkage. This means it is not possible to link
> > against libssh statically. Previously I dont think this has been a
> problem
> > as due to libssh's cmake it creates shared libraries by default. But if
> you
> > manually specify static linking it is currently not possible to link on
> > Windows.
> >
> > This patch fixes this by simply adding a define before the inclusion of
> the
> > libssh header file that disables automatically setting dllimport. With
> this
> > static linkage will work. Shared linkage still works as the shared lib
> > (libssh.dll.a etc.) is still included during linking and so the dllimport
> > are handled explicitly there instead.
> >
> > This is a single line patch that allows for both static and shared linage
> > to now work with libssh on Windows. Tested with MinGW and msvc/icl. This
> > patch has no affect on any other platforms.
> I do not know anything about linking on windws. Are you saying that these
> "dllimport" are useless and harmful? If so, what is the stance of upstream
> about it?
> Regards,
> --
>   Nicolas George
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

More information about the ffmpeg-devel mailing list