This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Using static library


Are there no differences between static and dynamic libraries in way of
using ??

-----Original Message-----
From: Joseph D. Wagner [mailto:wagnerjd@prodigy.net]
Sent: Monday, December 09, 2002 9:35 PM
To: 'Wojtek DHAS'; gcc-help@gcc.gnu.org
Subject: RE: Using static library


IMPORTING FUNCTION CALLS USING __declspec(dllimport)

The following code example shows how to use _declspec(dllimport) to import
function calls from a DLL into an application. Assume that func1 is a
function that resides in a DLL separate from the .EXE file that contains the
main function.

Without __declspec(dllimport), given this code:

void main(void)
{
   func1();
}
the compiler generates code that looks like this:

call func1
and the linker translates the call into something like this:

call 0x4000000         ; The address of 'func1'.
If func1 exists in another DLL, the linker can't resolve this directly
because it has no way of knowing what the address of func1 is. In 16-bit
environments, the linker adds this code address to a list in the .EXE that
the loader would patch at run time with the correct address. In 32-bit
environments, the linker generates a thunk of which it does know the
address. The thunk looks like:

0x40000000:    jmp DWORD PTR __imp_func1
Here imp_func1 is the address for func1's slot in the import address table
of the .EXE file. All of the addresses are thus known to the linker. The
loader only has to update the .EXE file's import address table at load time
for everything to work correctly.

Therefore, using __declspec(dllimport) is better because the linker does not
generate a thunk if it is not required. Thunks make the code larger (on RISC
systems, it can be several instructions) and can degrade your cache
performance. If you tell the compiler the function is in a DLL, it can
generate an indirect call for you.

So now this code:

__declspec(dllimport) void func1(void);
void main(void)
{
   func1();
}
generates this instruction:

call DWORD PTR __imp_func1
There is no thunk and no jmp instruction, so the code is smaller and faster.

On the other hand, for function calls inside a DLL, you don't want to have
to use an indirect call. You already know a function's address. Time and
space are required to load and store the address of the function before an
indirect call, so a direct call is always faster and smaller. You only want
to use __declspec(dllimport) when calling DLL functions from outside the DLL
itself. Don't use __declspec(dllimport) on functions inside a DLL when
building that DLL.

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Wojtek DHAS
Sent: Monday, December 09, 2002 12:27 PM
To: gcc-help@gcc.gnu.org
Subject: Using static library

Hi,
I have probably very easy question. I have some static windows library
(file.a) and I would like to use some function from it. How to call function
from static lib ?

Regards
Wojciech W.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]