Str Library In A Shared DLL

Web Site

This information applies only to Windows.

Applications often need to have multiple DLLs loaded simultaneously and passing objects to each other, including objects implemented in Str Library. To achieve this, the library supports a special conditional define, STR_EXPORT. Here are the steps necessary to implement properly this configuration:

1. Choose a DLL for the Str Library implementation

Str Library consists of two types of source code: inlined functions declared in H files, and regular functions in CPP files.

Applications that do not need to pass Str, Char and other objects between DLLs share a copy of all code in each DLL (if there are any), as well as the main EXE file. In the shared configuration, however, you need to choose exactly one DLL that will implement non-inline class methods.

When compiling that DLL, add Str Library to the DLL project as usual, but be sure to include the following declaration in str_config.h or stdafx.h:

    #define STR_EXPORT __declspec(dllexport)

This will cause the compiler and linker to generate code for non-inline methods that is callable from other modules.

2. Modify the configuration of all other DLLs and the main executable

In all other DLLs and the EXE file for the project, you should also add Str Library with the Wizard (so that you get a proper configuration file and inline implementations), but you must remove or comment out the references to any CPP files from Str Library. This is needed because only one copy of these CPPs will ever get compiled, and that is the one described in the previous step.

Also, when compiling, make sure you include the following in str_config.h or stdafx.h:

    #define STR_EXPORT __declspec(dllimport)

If you do not want to have multiple copies of Str Library's header files across your multiple projects, we recommend that you place just one copy in your common project include directory, and remove all local copies of the source from the folders of the individual DLLs and the main executable.

Also, make sure that when sharing Str Library code across multiple DLLs, all configuration settings for the library are exactly the same across all modules (except for STR_EXPORT, explained above). The opposite will cause errors that cannot be easily diagnosed.

3. Use a shared version of the C runtime libraries

This step is necessary for all multiple-DLL projects, not only for those using Str Library. We repeat it here because skipping it is a very common source of problems, especially among novice developers.

 

See alsoStr conditional defines