In this post, you’ll get answers to these questions:
- What is Static Library?
- What is DLL? How do I create a new DLL Dynamic Library?
- Can I create a new DLL using FMX framework?
- How can I create a function in a Dynamic Library?
- Where can I find a simple DLL example for C++ Builder FMX?
- Can we develop Dynamic Link Libraries in C++ Builder for Windows FireMonkey Applications?
- Is it possible to use DLLs in multi-device applications?
By learning how to create a new Windows FMX Static Library in c++, and how to compile c++ in windows. It will help you to easily build C++ 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. This .c
pp file might be precompiled as a lib file that can be used with it’s 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. This .c
file might be precompiled as a lib file that can be used with it’s header by other applications.
Both C and C++ library sources and headers are in text forms. When they compiled , compiler generates library files.
What is a Static library in C and C++?
Static Library is a C or C++ library that can be used by other applications sources in compilations. Standard C++ libraries are good example to static libraries. Static library is a part of your application when you compile your application. That means, if you are using a static library, it costs a memory whatever you use or not. Compiler compiles static library every time you compile them. Thus, we should be careful about static libraries to avoid using extra memory and having extra file size.
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 of 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, and another application in a different programming language.
How to Create a new static library?
1. We can create a new DLL 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 New Unit from File->New->Unit C++ Builder
4. Save this as “Static_Library_FMX1.cpp” , now you will have “Static_Library_FMX1.cpp” and “Static_Library_FMX1.cpp” files,
Static_Library_FMX1.cpp will be as below,
1 2 3 4 5 6 7 8 |
//--------------------------------------------------------------------------- #pragma hdrstop #include "Static_Library_FMX_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) |
Static_Library_FMX1.h will be as below,
1 2 3 4 5 6 7 |
#ifndef Static_Library_VCL1H #define Static_Library_VCL1H //--------------------------------------------------------------------------- #endif |
5. 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 FMX Multi-Device 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. Same option should be used in the applications that uses this .lib
6. Now let’s add #include <fmx.h> and a new custom function that combines two UnicodeStrings with a “+” to a new UnicodeString. Let’s name this function CombineStr() and lets 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 <fmx.h> //--------------------------------------------------------------------------- #pragma hdrstop #include "Static_Library_FMX_Unit1.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 |
7. Now we can Build this function in Debug and Release mods by changing its Build Configuration from the Project Window. Thus our other applications can use these .lib files.
If you have a lot of libs in a directory you can add their path from:
1 |
Project -> Options -> C++ (Shared Options) -> Library Path |
In these examples we used latest RAD Studio 11. Previous versions mostly about same to create a new static library.
Still if you need more examples, for the previous versions, you can check this old example video 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