Microsoft KB Archive/141459

= PRB: Can't Export from Static Libraries w/_declspec(dllexport) =

Article ID: 141459

Article Last Modified on 12/1/2003

-

APPLIES TO

 The Microsoft Linker (LINK.EXE) 1.0, when used with:  Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q141459

<div class="symptoms_section">

SYMPTOMS
When you build a DLL from component libraries (.lib files) that have functions declared with _declspec(dllexport) to export them from the DLL, the functions aren't exported or even included in the DLL. If you use the linker /warn option, the linker will display the LNK4005 warning to indicate that no .obj files were included from the static libraries.

<div class="resolution_section">

RESOLUTION
For the functions to be exported and included in the DLL, you must list the functions to be exported in a .def file, or use the linker /export option when building the DLL.

NOTE: This is only necessary when the functions are contained in .lib files that are used to build a DLL.

The following sample code illustrates this scenario, and shows how you can make sure the exported functions are included.

Sample Code
/* Compile options needed: none */

/* EXPORT.C */

__declspec( dllexport ) void func {  }

@rem **** Buildit.bat

cl /MT /c export.c

@rem **** build static library

link /lib /out:static.lib export.obj

@rem **** build DLL

link /dll /machine:ix86 /warn /out:export.dll static.lib

@rem **** using the following link line instead of the previous one @rem **** solves the problem: have to force a reference to func so it is       **** included

@rem link /dll /machine:ix86 /warn /export:func /out:export.dll static.lib

dumpbin /exports export.dll

rem **** DUMPBIN should have reported func as exported

<div class="status_section">

STATUS
This behavior is by design.

Keywords: kbtshoot kbprb kbcode KB141459

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.