From: "Ronald S. Bultje" <rsbul...@gmail.com>

This requires the makedef perl script by Derek, which probably will
go into the c99-to-c89 repo. That scripts produces a .def file,
listing the symbols to be exported, based on the gcc version
scripts and the built object files.

To properly load non-function symbols from DLL files, the data
symbol declarations need to have the attribute __declspec(dllimport)
when building the calling code. (On mingw, the linker can fix this
up automatically, which is why it has not been an issue so far.)

The __declspec(dllimport) attribute is added via a macro. Since
it has to be present when building code that refer to these symbols,
but not be present while building the library that exports the symbols
themselves, the makefile is modified to add a define like
COMPILING_<libname> while building each library. This is used in the
definition of the AV<lib>_SYMBOL macro - on non-MSVC compilers, it is
empty. On MSVC, it is __declspec(dllimport) for declarations in other
libraries than the object file currently compiled.

This also implies that code that is built to link to a certain
library as a DLL can't link to the same library as a static library.
Therefore, we only allow building either static or shared but not
both at the same time.

Also, linking to DLLs is slightly different from linking to shared
libraries on other platforms. DLLs use a thing called import
libraries, which is basically a stub library allowing the linker
know which symbols exist in the DLL and what name the DLL will
have at runtime.

In mingw/gcc, the import library is usually named libfoo.dll.a,
which goes next to a static library named libfoo.a. This allows gcc
to pick the dynamic one, if available, from the normal -lfoo switches,
just as it does for libfoo.a vs libfoo.so on unix. On MSVC however,
you need to literally specify the name of the import library instead of
the static library.
---
 configure                  |   27 ++++++++++++++++++++++++---
 libavcodec/ac3tab.h        |    3 ++-
 libavcodec/dca.h           |    3 ++-
 libavcodec/mjpeg.h         |   15 ++++++++-------
 libavcodec/mpeg12data.h    |    3 ++-
 libavcodec/mpeg4audio.h    |    3 ++-
 libavcodec/mpegaudiodata.h |    6 ++++--
 libavcodec/symbols.h       |   32 ++++++++++++++++++++++++++++++++
 library.mak                |    1 +
 9 files changed, 77 insertions(+), 16 deletions(-)
 create mode 100644 libavcodec/symbols.h

diff --git a/configure b/configure
index 99830d8..5480ce0 100755
--- a/configure
+++ b/configure
@@ -2778,14 +2778,33 @@ case $target_os in
         shlibdir_default="$bindir_default"
         SLIBPREF=""
         SLIBSUF=".dll"
+        if enabled_all msvc shared; then
+            # Link to the import library instead of the
+            # normal static library.
+            LD_LIB='%.lib'
+        fi
         SLIBNAME_WITH_VERSION='$(SLIBPREF)$(FULLNAME)-$(LIBVERSION)$(SLIBSUF)'
         SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
-        SLIB_EXTRA_CMD=-'$(DLLTOOL) -m $(LIBTARGET) -d $$(@:$(SLIBSUF)=.def) 
-l $(SUBDIR)$(SLIBNAME:$(SLIBSUF)=.lib) -D $(SLIBNAME_WITH_MAJOR)'
+        if enabled msvc; then
+            # The libtarget names for lib.exe differ from the ones for dlltool
+            if enabled x86_64; then
+                LIBTARGET=x64
+            fi
+            SLIB_CREATE_DEF_CMD='makedef $(SUBDIR)lib$(NAME).ver $(OBJS) > 
$$(@:$(SLIBSUF)=.def)'
+            SLIB_EXTRA_CMD='-lib.exe -machine:$(LIBTARGET) 
-def:$$(@:$(SLIBSUF)=.def) -out:$(SUBDIR)$(SLIBNAME:$(SLIBSUF)=.lib)'
+        else
+            SLIB_EXTRA_CMD=-'$(DLLTOOL) -m $(LIBTARGET) -d 
$$(@:$(SLIBSUF)=.def) -l $(SUBDIR)$(SLIBNAME:$(SLIBSUF)=.lib) -D 
$(SLIBNAME_WITH_MAJOR)'
+        fi
         SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
         SLIB_INSTALL_LINKS=
         SLIB_INSTALL_EXTRA_SHLIB='$(SLIBNAME:$(SLIBSUF)=.lib)'
-        SLIB_INSTALL_EXTRA_LIB='lib$(SLIBNAME:$(SLIBSUF)=.dll.a) 
$(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.def)'
-        SHFLAGS='-shared -Wl,--output-def,$$(@:$(SLIBSUF)=.def) 
-Wl,--out-implib,$(SUBDIR)lib$(SLIBNAME:$(SLIBSUF)=.dll.a) 
-Wl,--enable-runtime-pseudo-reloc -Wl,--enable-auto-image-base'
+        if enabled msvc; then
+            SLIB_INSTALL_EXTRA_LIB='$(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.def)'
+            SHFLAGS='-dll -def:$$(@:$(SLIBSUF)=.def) 
-implib:$(SUBDIR)lib$(SLIBNAME:$(SLIBSUF)=.dll.a)'
+        else
+            SLIB_INSTALL_EXTRA_LIB='lib$(SLIBNAME:$(SLIBSUF)=.dll.a) 
$(SLIBNAME_WITH_MAJOR:$(SLIBSUF)=.def)'
+            SHFLAGS='-shared -Wl,--output-def,$$(@:$(SLIBSUF)=.def) 
-Wl,--out-implib,$(SUBDIR)lib$(SLIBNAME:$(SLIBSUF)=.dll.a) 
-Wl,--enable-runtime-pseudo-reloc -Wl,--enable-auto-image-base'
+        fi
         objformat="win32"
         dlltool="${cross_prefix}dlltool"
         ranlib=:
@@ -3521,6 +3540,8 @@ elif enabled pathscale; then
     add_cflags -fstrict-overflow -OPT:wrap_around_unsafe_opt=OFF
 elif enabled msvc; then
     enabled x86_32 && disable aligned_stack
+    enabled_all shared static &&
+        die "Cannot build shared and static libraries at the same time with 
MSVC"
 fi
 
 enabled_any $THREADS_LIST      && enable threads
diff --git a/libavcodec/ac3tab.h b/libavcodec/ac3tab.h
index 8ed5052..2e20b4f 100644
--- a/libavcodec/ac3tab.h
+++ b/libavcodec/ac3tab.h
@@ -24,6 +24,7 @@
 
 #include "libavutil/common.h"
 #include "ac3.h"
+#include "symbols.h"
 
 #if CONFIG_HARDCODED_TABLES
 #   define HCONST const
@@ -33,7 +34,7 @@
 
 extern const uint16_t ff_ac3_frame_size_tab[38][3];
 extern const uint8_t  ff_ac3_channels_tab[8];
-extern const uint16_t avpriv_ac3_channel_layout_tab[8];
+extern AVCODEC_SYMBOL const uint16_t avpriv_ac3_channel_layout_tab[8];
 extern const uint8_t  ff_ac3_enc_channel_map[8][2][6];
 extern const uint8_t  ff_ac3_dec_channel_map[8][2][6];
 extern const uint16_t ff_ac3_sample_rate_tab[3];
diff --git a/libavcodec/dca.h b/libavcodec/dca.h
index 1515270..13eb790 100644
--- a/libavcodec/dca.h
+++ b/libavcodec/dca.h
@@ -26,6 +26,7 @@
 #define AVCODEC_DCA_H
 
 #include <stdint.h>
+#include "symbols.h"
 
 /** DCA syncwords, also used for bitstream type detection */
 #define DCA_MARKER_RAW_BE 0x7FFE8001
@@ -36,6 +37,6 @@
 /** DCA-HD specific block starts with this marker. */
 #define DCA_HD_MARKER     0x64582025
 
-extern const uint32_t avpriv_dca_sample_rates[16];
+extern AVCODEC_SYMBOL const uint32_t avpriv_dca_sample_rates[16];
 
 #endif /* AVCODEC_DCA_H */
diff --git a/libavcodec/mjpeg.h b/libavcodec/mjpeg.h
index 1374ab3..df56fbb 100644
--- a/libavcodec/mjpeg.h
+++ b/libavcodec/mjpeg.h
@@ -35,6 +35,7 @@
 
 #include "avcodec.h"
 #include "put_bits.h"
+#include "symbols.h"
 
 
 /* JPEG marker codes */
@@ -137,16 +138,16 @@ static inline void put_marker(PutBitContext *p, int code)
         case 7: ret= (left + top)>>1; break;\
     }
 
-extern const uint8_t avpriv_mjpeg_bits_dc_luminance[];
-extern const uint8_t avpriv_mjpeg_val_dc[];
+extern AVCODEC_SYMBOL const uint8_t avpriv_mjpeg_bits_dc_luminance[];
+extern AVCODEC_SYMBOL const uint8_t avpriv_mjpeg_val_dc[];
 
-extern const uint8_t avpriv_mjpeg_bits_dc_chrominance[];
+extern AVCODEC_SYMBOL const uint8_t avpriv_mjpeg_bits_dc_chrominance[];
 
-extern const uint8_t avpriv_mjpeg_bits_ac_luminance[];
-extern const uint8_t avpriv_mjpeg_val_ac_luminance[];
+extern AVCODEC_SYMBOL const uint8_t avpriv_mjpeg_bits_ac_luminance[];
+extern AVCODEC_SYMBOL const uint8_t avpriv_mjpeg_val_ac_luminance[];
 
-extern const uint8_t avpriv_mjpeg_bits_ac_chrominance[];
-extern const uint8_t avpriv_mjpeg_val_ac_chrominance[];
+extern AVCODEC_SYMBOL const uint8_t avpriv_mjpeg_bits_ac_chrominance[];
+extern AVCODEC_SYMBOL const uint8_t avpriv_mjpeg_val_ac_chrominance[];
 
 void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
                                   const uint8_t *bits_table,
diff --git a/libavcodec/mpeg12data.h b/libavcodec/mpeg12data.h
index 86ba3ec..dc9c374 100644
--- a/libavcodec/mpeg12data.h
+++ b/libavcodec/mpeg12data.h
@@ -31,6 +31,7 @@
 #include <stdint.h>
 #include "libavutil/rational.h"
 #include "rl.h"
+#include "symbols.h"
 
 extern const uint16_t ff_mpeg1_default_intra_matrix[64];
 extern const uint16_t ff_mpeg1_default_non_intra_matrix[64];
@@ -48,7 +49,7 @@ extern const uint8_t ff_mpeg12_mbPatTable[64][2];
 
 extern const uint8_t ff_mpeg12_mbMotionVectorTable[17][2];
 
-extern const AVRational avpriv_frame_rate_tab[];
+extern AVCODEC_SYMBOL const AVRational avpriv_frame_rate_tab[];
 
 extern const float ff_mpeg1_aspect[16];
 extern const AVRational ff_mpeg2_aspect[16];
diff --git a/libavcodec/mpeg4audio.h b/libavcodec/mpeg4audio.h
index 1ee2d7e..fa1cd21 100644
--- a/libavcodec/mpeg4audio.h
+++ b/libavcodec/mpeg4audio.h
@@ -25,6 +25,7 @@
 #include <stdint.h>
 #include "get_bits.h"
 #include "put_bits.h"
+#include "symbols.h"
 
 typedef struct MPEG4AudioConfig {
     int object_type;
@@ -40,7 +41,7 @@ typedef struct MPEG4AudioConfig {
     int ps;  ///< -1 implicit, 1 presence
 } MPEG4AudioConfig;
 
-extern const int avpriv_mpeg4audio_sample_rates[16];
+extern AVCODEC_SYMBOL const int avpriv_mpeg4audio_sample_rates[16];
 extern const uint8_t ff_mpeg4audio_channels[8];
 
 /**
diff --git a/libavcodec/mpegaudiodata.h b/libavcodec/mpegaudiodata.h
index d1a8841..c027f27 100644
--- a/libavcodec/mpegaudiodata.h
+++ b/libavcodec/mpegaudiodata.h
@@ -29,11 +29,13 @@
 
 #include <stdint.h>
 
+#include "symbols.h"
+
 #define MODE_EXT_MS_STEREO 2
 #define MODE_EXT_I_STEREO  1
 
-extern const uint16_t avpriv_mpa_bitrate_tab[2][3][15];
-extern const uint16_t avpriv_mpa_freq_tab[3];
+extern AVCODEC_SYMBOL const uint16_t avpriv_mpa_bitrate_tab[2][3][15];
+extern AVCODEC_SYMBOL const uint16_t avpriv_mpa_freq_tab[3];
 extern const int ff_mpa_sblimit_table[5];
 extern const int ff_mpa_quant_steps[17];
 extern const int ff_mpa_quant_bits[17];
diff --git a/libavcodec/symbols.h b/libavcodec/symbols.h
new file mode 100644
index 0000000..3c98d46
--- /dev/null
+++ b/libavcodec/symbols.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2012 Ronald S. Bultje <rsbul...@gmail.com>
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_SYMBOLS_H
+#define AVCODEC_SYMBOLS_H
+
+#include "config.h"
+
+#if CONFIG_SHARED && defined(_MSC_VER) && !defined(COMPILING_avcodec)
+#define AVCODEC_SYMBOL __declspec(dllimport)
+#else
+#define AVCODEC_SYMBOL
+#endif
+
+#endif /* AVCODEC_SYMBOLS_H */
diff --git a/library.mak b/library.mak
index b365935..318fe86 100644
--- a/library.mak
+++ b/library.mak
@@ -19,6 +19,7 @@ $(SUBDIR)x86/%.o: $(SUBDIR)x86/%.asm
        $(DEPYASM) $(YASMFLAGS) -I $(<D)/ -M -o $@ $< > $(@:.o=.d)
        $(YASM) $(YASMFLAGS) -I $(<D)/ -o $@ $<
 
+$(OBJS): CPPFLAGS := -DCOMPILING_$(NAME)=1 $(CPPFLAGS)
 $(OBJS) $(SUBDIR)%.h.o $(TESTOBJS): CPPFLAGS += -DHAVE_AV_CONFIG_H
 $(TESTOBJS): CPPFLAGS += -DTEST
 
-- 
1.7.9.5

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to