Create and use Static Library (.Lib) and Dynamic DLLs in C++ Builder
Introduction: This post describes how to create and use Static Library (.Lib) and Dynamic (Run-Time) DLLs in C++ Builder. A library is a collection of pre-compiled code that can be re-used by programs. There are 2 types of libraries: static library and dynamic library. Static Library (.lib) vs Dynamic Library (.dll) A static library (.LIB file) (or archive) contains code that is linked to users’ programs at compile time. The executable file generated keeps its own copy of the library code. A dynamic library (.dll) (or shared library) contains code designed to be shared by multiple programs. The content in the library is loaded to memory at runtime. Each executable does not maintain its replication of the library. What are the differences between static and dynamic libraries? Static libraries (.lib), while reusable in multiple programs, are locked into a program at compile time. Dynamic (.dll), or shared libraries, on the other hand, exist as separate files outside of the executable file. C++ Builder Dynamic Library (.DLL) and Static (.Lib) Library Let’s jump right into creating a C++ Builder Dynamic Library (.DLL) This post is using C++ Builder 11.3 Steps: 1. File | New | Other | C++ Builder | Dynamic Library , select Source type = C++ , check Multi-Threaded, and select Target Framework = No Framework 2. Save the generated project to a new folder: File | Save Project As | New Folder | CppMyDLL (C:UsersamannarinoDocumentsEmbarcaderoStudioProjectsCppMyDLL). Rename project to CppMyDLL.cbproj, and rename Unit1.cpp to CppMyDLL.cpp 3. The generated CppMyDLL.cpp file has this code: extern “C” int _libmain(unsigned long reason) The _libmain function is the entry point into the dynamic library. When the dynamic library is loaded, the _libmain function is called. This function usually holds initialization code. 4. To be able to use the DLL and the Client .EXE on any computer (that does not have C++ Builder installed), use: Project | Options | C++ Linker | Link with Dynamic RTL = False And Packages | RunTime Packages | Link with Run Time Packages = False 5. Build the application (Project | Build). You should get a success Build and Link! 6. Your source code output folder: (C:Users/amannarino/Documents/Embarcadero/Studio/Projects/CppMyDLL/Win32/Debug) should have a CppMyDLL.dll (Dynamic Library) and a CppMyDLL.lib (Static Library). The Static Library (.lib) is automatically generated from the Dynamic Library (,dll). 7. A helpful free utility for seeing the contents of the DLL is Dependency Walker. Dependency Walker is a free utility that scans any 32-bit or 64-bit Windows module (exe, dll, ocx, sys, etc.) and builds a hierarchical tree diagram of all dependent modules. The Download link for Dependency Walker is: https://www.dependencywalker.com/ 8. Next, lets add a function to the CppMyDLL.cpp : double Sum(double a, double b) { return a +b; } Note: DLLs can contain functions that are hidden from the clients, and other functions that are public to the clients. 9. To make this Sum function visible to clients, we add: __declspec(dllexport) like this: double __declspec(dllexport) Sum(double a, double b) { 10. Using Dependency Walker this function SUM shows as @Sum$add To remove the characters @ and the $add (that descibes the function) we add extern “C” to the function, like this: extern “C” double __declspec(dllexport) Sum(double a, double b) { 11. Now, using Dependency Walker, the Sum function shows as: _Sum 12. To remove the […]
