Exporting '& Importing classes and functions when building multiple DLLs

Randy More's previous article (posted on March 8, 1998) provided a solution to solve the problem of using one extension dll in another. Basically, he suggested that we redefine the macro AFX_EXT_CLASS in one dll's header file before including a header file for classes exported by a second dll , and restore the previous macro definition afterwards.

His solution does not work in the situation where dll A uses classes in dll B and dll B also uses classes in dll A. I have found a much simpler solution which solves these problems elegantly.

Suppose we want to build a dll called MyDllA. First we define the following macros:

#define MYDLLA_EXPORT __declspec(dllexport) 
#define MYDLLA_EXPORT __declspec(dllimport)

Then we declare classes, functions, and data in MyDllA using the macro MYDLLA_EXPORT, for example:


When compiling MyDllA, we define the macro BUILD_MYDLLA in the project setting. When compiling a dll that uses classes in MyDllA, we define the macro USE_MYDLLA in its project setting. The same trick can be applied when build MyDllB, MyDllC, etc. Note that the source code doesn't have to be changed if we decide to make MyDllA a static library instead (in that case, don't define the macros BUILD_MYDLLA and USE_MYDLLA when compiling the project).

Date Last Updated: April 18, 1999


  • Lightweight perceptive – Nike Unshackled TR Attack in divulge 2013 3 series

    Posted by Tufffruntee on 04/19/2013 10:56pm

    Nike Emancipated TR Suited 3 noteworthy features is to exploit the additional design: Nike Free 5 soles improved bending Striation; supplemental tractor layout making training more focused when; lighter preponderance, the permeability is stronger, and more in fashion shoe designs not only exhort shoes [url=http://markwarren.org.uk/goodbuy.cfm]nike free run[/url] more smug wearing, barefoot training feel, but also more in vogue appearance. Nike On the house TR Fit 3 provides supreme lateral solidity, you can take the legs in the part during training. Strong vamp nobles breathable mesh, lower soap up's solitary lay out can be [url=http://fossilsdirect.co.uk/glossarey.cfm]nike huarache[/url] seen through it. Lightweight, difficult, reduce bubbles means occupied by very some seams, more flexible, help is stronger. Requirement more help, department of a training irritate, bubbles make inaccessible in more parts of the need in return flexibility, effervescence loose. Say double say nothing moisture wicking mock materials, unshiny on your feet, refrain from keep feet sear and comfortable. Phylite [url=http://turbo-vac.co.uk/components_13.cfm]nike free uk[/url] midsole offers lightweight surprise level, special durability and even outsole can do to greatly turn the all-embracing dialect heft of the shoe. Qianzhang pods on the outsole and heel-shaped Na媣e rubber enhances the shoe multi-directional gripping power on sundry surfaces.

  • Exporting C++ classes using .def files.

    Posted by shalmi on 09/21/2007 01:18am

    Hi, .def files are used when we do not want to use __declspec(dllexport). But how can we export whole class using .def files? I tried to do so but then there is linking error in .def file for constructor and destructors. Why so? Is there any solution to this? Thanks in advance.

  • Very good!!!

    Posted by Legacy on 09/01/2003 12:00am

    Originally posted by: CarioLee

    Very good!!!Worked well!

  • Export class from DLL

    Posted by Legacy on 08/25/2002 12:00am

    Originally posted by: Jonathan Liu

    I try to export a class containing a template member function. I got "error LNK2001: unresolved external symbol ..." linking error when compiling a exe using that dll. Appriciate any idea to solve the problem!

  • A good web link for DLL Exporting & Importing

    Posted by Legacy on 08/20/2002 12:00am

    Originally posted by: Tiny Young

    //your MyDll.h
    #ifndef _DEFINE_EBE7C480_A601_11d4_BC36_006067709674_
    #define _DEFINE_EBE7C480_A601_11d4_BC36_006067709674_
    #if _MSC_VER>1000
    #pragma once
    #ifdef __cplusplus
    extern "C" {
    #endif //__cpluspuls
    #ifndef _INTERFACE_
    #define INTERFACE __declspec(dllexport)
    #define INTERFACE __deslspec(dllimport)
    #endif //_INTERFACE_

    #undef INTERFACE
    #ifdef __cplusplus
    #endif //__cplusplus
    #endif //_DEFINE_EBE7C480_A601_11d4_BC36_006067709674_

    //your MyDll.cpp
    #define _INTERFACE_
    extern "C" __declspec(dllexport) WINAPI BOOL StartService()

    //your App.cpp

    Sorry ,my english is very poor!

  • A Example pleae

    Posted by Legacy on 10/06/2001 12:00am

    Originally posted by: Henry Casta�eda

    Hellow, I am from Lima-Peru (South America) , my name is Henry Casta�eda, I was read your article "Exporting & Importing classes and functions when building multiple DLLs " this is just I needed, please I need a example of this article.
    I need a dll_A call and use a object.method of other dll_B, but my dll_A call this from a function .
    Please help me,:
    Thanks You

  • C++, classes, DLLs and destructor

    Posted by Legacy on 05/18/2001 12:00am

    Originally posted by: Christophe


    I have some problems with DLL:
    I am creating a DLL which have a class definition (C++).
    All is working (export, import, and execution) but when the destructor is called, I've got an error message (EAccessViolation...)

    Please!! I need some help.

    Christophe (France).

  • exporting a class from a DLL

    Posted by Legacy on 03/12/2001 12:00am

    Originally posted by: Jason

    I'm building a class (in Visual C++ 5.0) for complex
    variables called Complex. It's not a class template, but
    its member functions do use functions from an external
    static library. I have a header file that contains the

    #ifndef importexport_h
    #define importexport_h

    #ifdef _IMPORTEXPORT
    #define IMPORTEXPORT_CLASS_DECL __declspec(dllexport)
    #define IMPORTEXPORT_CLASS_DECL __declspec(dllimport)


    This code is #included into my Complex.h file. Then I use
    the following to declare the class in Complex.h:

    // a bunch of data members and member functions

    Then, of course, the Complex.cpp file contains the member
    functions' definitions.

    I have no problem compiling this class into a DLL, but when
    I compile an executable that uses this DLL, I get errors
    main.obj : error LNK2001: unresolved external
    symbol "__declspec(dllimport) public: __thiscall
    Complex::Complex(float,float)" (__imp_??0Complex@@QAE@MM@Z)

    I'm quite sure that I have the correct directories listed
    in the Project Settings and the Tools Options. What might
    be the problem?

  • Exporting DLL's and headers

    Posted by Legacy on 05/26/2000 12:00am

    Originally posted by: IMAD MURAD

    I am working on an image processing software written in visual C++ 6.0

    One problem I am having is with the _export statement. Replaciong it with _declspec(dllexport)is not solving my problem.

    Can someone help me correct the code in the following line

    long FAR PASCAL _export ImageWndProc (HWND, UINT, UINT, LONG);

    I think that is the reason why it is also giving me an error saying " huge is obsolete"


  • How to use class used in my extension DLL in my VC++ Application?

    Posted by Legacy on 05/12/2000 12:00am

    Originally posted by: Revati

    I have made an extension DLL with shared MFC support. It has COM support also as I have imported msado15.dll in it.

    Now I want to use this DLL in my application. But whenever I #include .h files in my application or try to import my DLL in my stdafx.h compiler gives me DLL not found error. I have corretly included DLL in Systems directory and .lib file in project settings.

    If anybody has solution to this please mail me.

    Thanking you all in advance.

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Entire organizations suffer when their networks can't keep up and new opportunities are put on hold. Waiting on service providers isn't good business. In these examples, learn how to simplify network management so that your organization can better manage costs, adapt quickly to business demands, and seize market opportunities when they arise.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date