Free Learn To Code Week – Great for Students

If you want a chance to learn to code (or maybe pick up ideas about how to teach coding?) maybe this “Embarcadero Coding Summer Camp” which officially started yesterday is for you…..

https://blogs.embarcadero.com/day-1-learn-to-code-summer-camp-2021/

Tips For Using GoogleTests with Embarcadero Clang64 for Windows VCL Projects.

In my previous posting I covered how to install GoogleTests framework ready for use with Embarcadero C++. Now I am going to offer some advice on how to write simple tests for your own VCL projects using the GoogleTests framework framework. I will assume Clang64 but it all applies equally to Clang32 projects.

If you try to use the GoogleTests files where they were installed you end up with very long include paths and you also lose your project independance. It seems much more convenient to create a copy of the key files in a directory local to your project. I suugest:

  1. Create a directory in your project called “GoogleTest”
  2. Inside “GoogleTest create a directory “LibWin64”
  3. Inside “LibWin64” copy the four files gmock.a, gmock_main.a, gtest.a and gtest_main.a (only gtest.a is actually required to run simple tests) from the lib folder C:\Users\ [ your account name ] \Documents\Embarcadero\Studio\21.0\CatalogRepository\GoogleTest-2021.03\cbuilder\lib\Win64\Release.
  4. Inside “GoogleTest create a directory “Include”
  5. Inside “Include” copy the directory “gtest” from C:\Users\ [ your account name ] \Documents\Embarcadero\Studio\21.0\CatalogRepository\GoogleTest-2021.03\googletest\include\gtest, making sure you include the “Internal” folder and all it’s contents (including further sub-directories).
  6. In your project group create a new windows64 bit VCL console application. As well as the files you want to test and the files containing the testing code you need to add to the project the library file …GoogleTest\LibWin64.
  7. In your project options add the include path ….GoogleTest\Include\gtest.

Having configured your project in this way you then need to add the line “#include “gtest.h” at the begining of each of your *.cpp files that you generate to test your project (convention suggests one test file for each *.cpp unit in your main project). In addition, in your main VCL console cpp file, you need to change the “main()” program entry point so that it becomes:

int _tmain(int argc, _TCHAR* argv[])
{
  printf("Running main() from %s\n", __FILE__);
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

One key point to be aware of is that you must build your test project (you still choose which ever setting you want for projects you ship) using dynamic run time linking of packages set to true. Set this in project | options | C++ Linker.

Using GoogleTests with Embarcadero Clang64 for Windows VCL Projects

The old C++ Builder “classic” win32 compiler worked well with the associated DUnit framework for unit testing.

With the clang compilers (both clang32 and clang64) there are issues when using the DUnit framework. It makes sense to use a framework that is up to date and receiving on going support. Such a framework is GoogleTests.

There are two stages to using GoogleTests with your own Windows clang64 C++ project.

Firstly you have to download it, install it and confirm that all is well. After doing that you have to work out how to use it with your own project.

Downloading is easy if you have C++ Builder 10.4.2 as GoogleTests appears in the Getit package manager. Using Getit the test framework along with sample programs can be automatically downloaded and installed.

The resultant files are stored in

C:\Users\[your account name here] \Documents\Embarcadero\Studio\21.0\CatalogRepository\GoogleTest-2021.03

We will call this long path ”…GT2021.03” for the rest of this blog.

Now build and run the files as follows.

Using the C++ Builder IDE, open the project group GoogleTest.groupproj in …GT2021.03\cbuilder
You can now build all the projects (they are set for clang32). But for our discussion we need it to run with clang64. Edit each of the project settings for each of the projects in the project group so that each project uses clang64.

Now do a build all projects to build everything using clang64.

The exe file resulting from the sample8_unittest.cbproj is file sample8_unittest.exe in directory …GT2021.03\cbuilder\bin\Win64\Release

Open a DOS command line shell in this directory and then run sample8_unittest.exe. You will see that it reports 12 test passes.

Great ! You’ve got GoogleTests installed and looking good! In my next blog I will make some suggestions about how to use it with your C++ Builder VCL clang64 project.

More on Order of Events When Creating and Destroying a Form in C++ Builder

The last two blogs have been about Embarcadero C++ Builder events that are fired when a form is created or destroyed.

As an addition to these postings I refer everyone to Remy’s stack overflow comments about the order of events and the fact that it’s good to put constructor stuff in the constructor

https://stackoverflow.com/questions/67189840/stdofstream-doesnt-work-with-short-enums-in-cbuilder-10-30-3

(Scroll down to the end of the long list of comments posted on this stack overflow question)

As well as pointing out that in the past (early versions of C++ Builder) there may have been an issue with the event firing order Remy wisely advises that it is always good C++ programming practice to put initialisation code in the constructor. Similarly it’s always advisable to put any close down tidying up code in the destructor.

I completely endorse Remy’s wise words.

Order of Events When Destroying a Form in C++ Builder

Following on from my previous blog it may be useful to know the order of events when a form is destroyed.

So, using C++ Builder 10.4.2 with a VCL project and the “classic compiler” here’s the sequence that the events get called when destroying the form…..

FormClose
FormHide
FormDestroy
Destructor

Order of Events When Creating a Form in C++ Builder

There are times when it is important to be aware of the order of events that are fired when a form is created. I’ve found this particularly useful when dealing with the main form.

So, using C++ Builder 10.4.2 with a VCL project and the “classic compiler” here’s the sequence that the events get called when creating the form…..

Constructor
FormCreate
FormShow
FormActivate

Make your Source Code more Readable When Passing a Boolean.

Suppose you have a C++ function declared as follows:

void UpdateDisplay(bool WithDelay);

and somewhere later you want to use this, calling it without the delay.

You could write:

UpdateDisplay(false);

But a simple way to make your source code more readable (and hence more maintainable by yourself and your team in the future) is to write:

const bool WithDelay{false};
UpdateDisplay(WithDelay);

When you compile this code for release the local variable “WithDelay” will be optimised away so this improvement to source code readability incurs no performance cost penalty.

RAD Studio 10.4.2 Is Alive ! and Wonderful

The latest version of Embarcadero RAD Studio is 10.4.2. and the upgrade to this version is definitely worthwhile. The user experience of the IDE has been smoothed out / polished up significantly. It’s a joy to use!

New users can download a trial by clicking here https://www.embarcadero.com/products/rad-studio

Existing users can download 10.4.2 by going to https://my.embarcadero.com/#login

RAD Studio 10.4.2 Is Here !

The latest version of Embarcadero RAD Studio is 10.4.2. This was promised in the most recent Embarcadero road map for “first half of 2021”.

It’s available now !

https://blogs.embarcadero.com/announcing-the-availability-of-rad-studio-10-4-2-sydney-release-2/

For those that missed the “what’s in 10.4.2” webinar (see previous blog) this is now available as a replay on YouTube at

https://www.youtube.com/watch?v=eeiO2uEHlKYf