What do we mean by the term “static library”? What is a DLL? How can I create a new DLL dynamic library? Can I create a new DLL using the FMX framework? How can I create a function in a dynamic library? Where can I find a simple DLL example which uses C++ Builder FMX? Can we develop Dynamic Link Libraries in C++ Software for Windows FireMonkey Applications? Is it possible to use DLLs in multi-device applications?
Table of Contents
What is a library in C and C++?
A library in C and C++ is a collection of functions, methods, classes and many other features of C++ exposed for use by other programs. In C++ we can use libraries by adding their header files.
In C++ programming, a library consists of an interface expressed in a .h
pp file (header) and an implementation expressed in a .cpp file. The.c
pp file might be precompiled as a lib file that can be used with its header by other applications.
In C programming, a library consists of an interface expressed in a .h
file (“header) and an implementation expressed in a .c file. Just like C++ the .c
file might be precompiled as a lib file that can be used with its header by other applications.
Both C and C++ library sources and headers are text files. When they get compiled , the compiler generates (outputs) library files.
What is a static library?
A static library is a C or C++ library that can be used by other applications’ source code, incorporated into the final output which makes up the finished program. Standard C++ libraries are good example to static libraries. A static library’s code is made a part of your application when you compile your program. That means, if you are using a static library, it uses memory to hold all the final compiled instructions of the library’s functions whether you use those functions or not. The compiler compiles a static library every time you compile them. Thus, we should be careful about how we use static libraries to avoid using extra unnecessary computer memory and making our finished application file sizes bigger than they need to be.
What is a dynamic link library (DLL)?
Dynamic Link Libraries (DLLs) are modules of compiled code that work in conjunction with an executable to provide functionality to an application.
There are two typical uses for dynamic link libraries.
- The first is to use a repository of external DLLs written by a third party (for example, Microsoft).
- The second is to write your own DLLs that you want to share between your programs or perhaps another application in a different programming language.
How do I create a new static library in C++ Builder?
1. We can create a new library by using File -> New -> C++ Builder menu,
2. Select Static Library, and press “OK” Save all to a folder (i.e. “MyStaticLibrary”)
3. Now we need a Unit to hold our functions, classes , methods and properties etc. Add a new unit from File->New->Unit C++ Builder
4. Save this as “Static_Library_VCL1” , now you will have “Static_Library_VCL1.cpp” and “Static_Library_VCL1.cpp” files,
Static_Library_VCL1.cpp will be as below,
1 2 3 4 5 6 7 |
#pragma hdrstop #include "Static_Library_VCL1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) |
Static_Library_VCL1.h will be as below,
1 2 3 4 5 6 7 |
#ifndef Static_Library_VCL1H #define Static_Library_VCL1H //--------------------------------------------------------------------------- #endif |
Are there any special considerations about compiler settings when using DLLs and static libraries in C++?
Note that DLL and your application should have to use the same compiler, so in the Project Options->Compiler Options; Use ‘classic’ Borland Compiler should be checked false, or true in both library and the application that uses this lib. Here let’s use CLANG Compiler. Coding with CLANG Compiler for VCL applications is better for future C++ changes in standards.
Goto Project->Options menu, in Building->C++ Compiler section you will see that Use ‘classic’ Borland Compiler is checked for the static library. Disable it ! That means we will use CLANG compiler.
How do I use a static library in my C++ code?
Let’s add #include <vcl.h> and a new custom function that combines two UnicodeStrings with a “+” to a new UnicodeString. Let’s name this function CombineStr() and define it. cpp file of our static library will be as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <vcl.h> //--------------------------------------------------------------------------- #pragma hdrstop #include "Static_Library_VCL1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) UnicodeString CombineStr(UnicodeString s1, UnicodeString s2) { return s1+"+"+s2; } |
and the header file of our static library will be as below,
1 2 3 4 5 6 7 8 9 |
#ifndef Static_Library_VCL1H #define Static_Library_VCL1H //--------------------------------------------------------------------------- UnicodeString CombineStr(UnicodeString s1, UnicodeString s2); #endif |
Should I create debug and release mode libraries for my libraries in C++ Builder?
We need to create both release and debug versions of the libraries so that they can be used both in the debugger and as part of the final release version which has any extra debugging removed to create the most optimal compiled version without the extra overheads of code and speed added to make the debugging of apps possible.
We can Build our example function in Debug and Release modes by changing its Build Configuration from the Project Window. Thus our other applications can use these .lib files.
Note that DLL and your application should have to use the same compiler (i.e Borland Compiler 32). In these examples we used latest RAD Studio 11. Previous versions mostly about same to create a new static library.
If you need more examples, you can check this example below,
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition