📄 autobook_255.html
字号:
<HTML><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- Created on September, 12 2004 by texi2html 1.64 --><!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others.Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>Send bugs and suggestions to <texi2html@mathematik.uni-kl.de> --><HEAD><TITLE>Autoconf, Automake, and Libtool: Handling Data Exports from DLLs</TITLE><META NAME="description" CONTENT="Autoconf, Automake, and Libtool: Handling Data Exports from DLLs"><META NAME="keywords" CONTENT="Autoconf, Automake, and Libtool: Handling Data Exports from DLLs"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META NAME="Generator" CONTENT="texi2html 1.64"><script language="Javascript"><!-- // Check the browser version. function checkVersion() { if (navigator.appVersion.charAt(0)>=3) return true; if (navigator.appVersion.charAt(0)>=4) return true; else return false; } if (checkVersion()) { homeon = new Image(); homeon.src = "homeon.png"; homeoff = new Image(); homeoff.src = "home.png"; tocon = new Image(); tocon.src = "tocon.png"; tocoff = new Image(); tocoff.src = "toc.png"; indexon = new Image(); indexon.src = "indexon.png"; indexoff = new Image(); indexoff.src = "index.png"; helpon = new Image(); helpon.src = "helpon.png"; helpoff = new Image(); helpoff.src = "help.png"; backon = new Image(); backon.src = "backon.png"; backoff = new Image(); backoff.src = "back.png"; forwardon = new Image(); forwardon.src = "forwardon.png"; forwardoff = new Image(); forwardoff.src = "forward.png"; prevon = new Image(); prevon.src = "prevon.png"; prevoff = new Image(); prevoff.src = "prev.png"; nexton = new Image(); nexton.src = "nexton.png"; nextoff = new Image(); nextoff.src = "next.png"; upon = new Image(); upon.src = "upon.png"; upoff = new Image(); upoff.src = "up.png"; } function img_act(imgName) { if (checkVersion()) { imgOn = eval(imgName + "on.src"); document [imgName].src = imgOn; } } function img_inact(imgName) { if (checkVersion()) { imgOff = eval(imgName + "off.src"); document [imgName].src = imgOff; } }// --></SCRIPT></HEAD><BODY LANG="EN" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#6688AA" VLINK="#336688" ALINK="#808080"><A NAME="SEC255"></A><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=10><TR VALIGN="TOP"><TD ALIGN="MIDDLE" WIDTH=50 BGCOLOR="#e6e6e6"><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_254.html#SEC254" onMouseover="img_act('prev')" onMouseout="img_inact('prev')"><IMG SRC="prev.png" BORDER="0" ALT="Back: A configure.in for DLLs" ALIGN="MIDDLE" NAME="prev"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_256.html#SEC256" onMouseover="img_act('next')" onMouseout="img_inact('next')"><IMG SRC="next.png" BORDER="0" ALT="Forward: Runtime Loading of DLLs" ALIGN="MIDDLE" NAME="next"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_256.html#SEC256" onMouseover="img_act('back')" onMouseout="img_inact('back')"><IMG SRC="back.png" BORDER="0" ALT="FastBack: Runtime Loading of DLLs" ALIGN="MIDDLE" NAME="back"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_251.html#SEC251" onMouseover="img_act('up')" onMouseout="img_inact('up')"><IMG SRC="up.png" BORDER="0" ALT="Up: DLLs with Libtool" ALIGN="MIDDLE" NAME="up"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_257.html#SEC257" onMouseover="img_act('forward')" onMouseout="img_inact('forward')"><IMG SRC="forward.png" BORDER="0" ALT="FastForward: Package Installation" ALIGN="MIDDLE" NAME="forward"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook.html#SEC_Top" onMouseover="img_act('home')" onMouseout="img_inact('home')"><IMG SRC="home.png" BORDER="0" ALT="Top: Autoconf, Automake, and Libtool" ALIGN="MIDDLE" NAME="home"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_toc.html#SEC_Contents" onMouseover="img_act('toc')" onMouseout="img_inact('toc')"><IMG SRC="toc.png" BORDER="0" ALT="Contents: Table of Contents" ALIGN="MIDDLE" NAME="toc"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_285.html#SEC285" onMouseover="img_act('index')" onMouseout="img_inact('index')"><IMG SRC="index.png" BORDER="0" ALT="Index: Index" ALIGN="MIDDLE" NAME="index"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_abt.html#SEC_About" onMouseover="img_act('help')" onMouseout="img_inact('help')"><IMG SRC="help.png" BORDER="0" ALT="About: About this document" ALIGN="MIDDLE" NAME="help"></A></TD></TR></TABLE></TD><TD ALIGN="LEFT"><H3> 25.4.4 Handling Data Exports from DLLs </H3><!--docid::SEC255::--><P>Unfortunately, things are not quite that simple in reality, except inthe rare cases where no data symbols are exported across a DLL boundary.If you look back at the example in <A HREF="autobook_254.html#SEC254">A configure.in for DLLs</A>, you will notice that the Libtool object,<TT>`hello.lo'</TT> was built with the preprocessor macro <SAMP>`DLL_EXPORT'</SAMP>defined. Libtool does this deliberately so that it is possible todistinguish between a static object build and a Libtool object build,from within the source code.</P><P>Lets add a data export to the DLL source to illustrate:</P><P>The <TT>`hello.h'</TT> header must be changed quite significantly:<TABLE width=100%><tr><td> </td><td class=example bgcolor=#6688aa><br><pre>#ifndef HELLO_H#define HELLO_H 1#if HAVE_CONFIG_H# include <config.h>#endif#ifdef _WIN32# ifdef DLL_EXPORT# define HELLO_SCOPE __declspec(dllexport)# else# ifdef LIBHELLO_DLL_IMPORT# define HELLO_SCOPE extern __declspec(dllimport)# endif# endif#endif#ifndef HELLO_SCOPE# define HELLO_SCOPE extern#endifHELLO_SCOPE const char *greet;extern int hello (const char *who);#endif /* !HELLO_H */</pre></td></tr></table></P><P>The nasty block of preprocessor would need to be shared among all thesource files which comprise the <TT>`libhello.la'</TT> Libtool library,which in this example is just <TT>`hello.c'</TT>. It needs to take care offive different cases:</P><P><DL COMPACT><DT>compiling <TT>`hello.lo'</TT><DD>When compiling the Libtool object which will be included in theDLL, we need to tell the compiler which symbols are exported dataso that it can do the automatic extra dereference required to refer tothat data from a program which uses this DLL. We need to flag thedata with <CODE>__declspec(dllexport)</CODE>, See section <A HREF="autobook_251.html#SEC251">25.4 DLLs with Libtool</A>.<P><DT>compilation unit which will link with <TT>`libhello-0-0-0.dll'</TT><DD>When compiling an object which will import data from the DLL, again weneed to tell the compiler so that it can perform the extra dereference,except this time we use <CODE>extern __declspec(dllimport)</CODE>. From thepreprocessor block, you will see that we need to define<SAMP>`LIBHELLO_DLL_IMPORT'</SAMP> to get this define, which I will describeshortly.<P><DT>compiling <TT>`hello.o'</TT><DD>When compiling the object for inclusion in the static archive, we must becareful to hide the <CODE>__declspec()</CODE> declarations from the compiler,or else it will start dereferencing variables for us by mistake atruntime, and in all likelihood cause a segmentation fault. In this casewe want the compiler to see a simple <CODE>extern</CODE> declaration.<P><DT>compilation unit which will link with <TT>`libhello.a'</TT><DD>Similarly, an object which references a data symbol which will bestatically linked into the final binary from a static archive must notsee any of the <CODE>__declspec()</CODE> code, and requires a simple<CODE>extern</CODE>.<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -