C++Introduction to C++Language FeatureLearn C++

How To Make A New Windows VCL Static Library In C++

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++ Builder for Windows FireMonkey Applications? Is it possible to use DLLs in multi-device applications?

What is a library in C and C++?

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 .hpp file (header) and an implementation expressed in a .cpp file. The.cpp 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,

Static_Library_VCL1.h will be as below,


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.

This image has an empty alt attribute; its file name is RADS_CompilerOptions_General.png

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,

and the header file of our static library will be as below,

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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsLanguage FeatureLearn C++

How To Add Shadow Effects To Your C++ Apps

C++ComponentsLanguage FeatureLearn C++

How To Make Controls Have A Glow Effect In C++?

C++Learn C++

What Is The sscanf Function In C++ And How Can I Use It?

C++C++17Introduction to C++Language FeatureLearn C++

This Is How To Use Parallel Programming in C++ Builder

en_USEnglish