Exporting '& Importing classes and functions when building multiple DLLs | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 18, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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:

#ifdef BUILD_MYDLLA
#define MYDLLA_EXPORT __declspec(dllexport)
#else
#ifdef USE_MYDLLA
#define MYDLLA_EXPORT __declspec(dllimport)
#else
#define MYDLLA_EXPORT
#endif
#endif

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

class MYDLLA_EXPORT MyClass
{
…
};

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.