[Gdal-dev] DLL exports in static windows apps?

Kirk McKelvey kmckelvey at lizardtech.com
Wed Aug 2 19:41:07 EDT 2006


We recently discovered our shipping apps were exporting GDAL DLL
symbols.  Not a huge deal, but in the name of cleanliness I investigated
the problem and wanted to post here in case anyone else is interested.

The problem is that the static library is built using objects that were
compiled for a DLL.  All exported functions are declared using
__declspec(dllexport), and that exported-ness carries through with the
object into the final executable or DLL.

There is no provision in the GDAL build harness at the moment for
building *both* kinds of objects so that the DLL will export its symbols
and static lib not export them.  But if all you care about is the static
lib, this is what I had to do to tailor the build harness (Warning:
making these changes will also produce a DLL without any exports!):

1. #define CPL_DISABLE_DLL in cpl_config.h.vc (normally commented out) -
This will stop the symbols from being exported.

2. Comment out the DLLBUILD=1 in nmake.opt - This will make the apps
link against the static library instead of the DLL stub library.  Or at
least it *should* (see #B below).

3. Remove "-DBUILD_AS_DLL" from frmts/gtiff/libgeotiff/makefile.vc -
This will stop the tiff symbols from being exported (tiff uses a
different macro for its __declspec(dllexport)).


...Simple enough.  But I encountered a couple of broken things in the VC
build harness that might warrant fixing:

A. I wanted to be able to run "nmake /f makefile.vc staticlib" to build
only the static libraries, but the staticlib target has no dependencies.
To fix this I reassigned the relevant dependencies from the "default"
target to "staticlib".  See makefile.vc.patch.txt.

B. Then I wanted to run "nmake /f makefile.vc apps_dir" to build
gdal_translate et al., but the apps/makefile.vc is hardwired with the
name of the DLL-import stub library.  To fix this I made it use
$(GDALLLIB) instead, which is set to either the stub library or the
static library depending on the value of DLLBUILD.  See
apps_makefile.vc.patch.txt.


After these changes, I could run:

 nmake /f makefile.vc staticlib
 nmake /f makefile.vc apps_dir

... and got a statically-linked gdal_translate that exported no symbols.


Maybe these patches would be useful to apply permanently?  Frank?

Cheers,

Kirk.

-------------- next part --------------
Index: makefile.vc
===================================================================
RCS file: /cvs/maptools/cvsroot/gdal/makefile.vc,v
retrieving revision 1.62
diff -r1.62 makefile.vc
59c59
< default:      port_dir ogr_dir core_dir frmts_dir $(VB6_TARGET) $(GDAL_DLL) \
---
> default:      $(GDAL_DLL) \
62c62
< staticlib:
---
> staticlib: port_dir ogr_dir core_dir frmts_dir $(VB6_TARGET)
-------------- next part --------------
Index: apps/makefile.vc
===================================================================
RCS file: /cvs/maptools/cvsroot/gdal/apps/makefile.vc,v
retrieving revision 1.27
diff -r1.27 makefile.vc
6c6
< LIBS  =       $(GDAL_ROOT)\gdal_i.lib
---
> LIBS  =       $(GDALLIB)


More information about the Gdal-dev mailing list