Category Archives: C++

Article in Technical Magazine Programmez N° 218 – May 2018

In May 2018, you will discover a technical article How to build under 64 bits environment. It contains tips&tricks for handling the migration because the source code will not compile as easy you can think…

 

Advertisements

LMDB for Windows

When I discovered the fantastic history of LMDB, It fascinated me. For a R&D projet, we need to present something that looks sexy and with a strong architecture. I proposed to study LMDB and bring to it the distributed engine and the clustering stuff with nodes. There are plenty ways of acheiveing this.

First, OpenLDAP-LMDB need to be ported on the Windows platform. It’s a C based code so there are no some many changes to fix for building on Windows. There was a path concat that made some problems but it’s defined as constants so I fixed it rapidly. I put it in Github after the operation was done.: https://github.com/ChristophePichaud/LMDBWindows

I was able to build a static lib for LMDB. But in Windows, we love shared modules called DLL so I have to put some little things on the source code to enable loading shared modules DLL (Dynamic Link Library).

#ifdef LMDBWINDOWSDLL_EXPORTS
#define LMDBWINDOWSDLL_API __declspec(dllexport)
#else
#define LMDBWINDOWSDLL_API __declspec(dllimport)
#endif

With theses expanded macros, it can export functions. Every headers and bodys need to be decorated. Here is the explaination. When you build the library, you define de constant LMDBWINDOWSDLL_EXPORTS in the project settings then every macros instruct the compiler to export the functions. When you build an application who needs the dll, the macro is expanded as an import operation and at the link phase, you need to link with the little stub to attach the library.

If you are new to Windows DLLs making, check this portion of doc on MSDN: https://msdn.microsoft.com/en-us/library/a90k134d.aspx

Enter in the world of NoSQL engines

Sometimes ago, I had to discover a source code, as is. It was a database engine. A big software, powered by C/C++ using linux tools. I always install cygwin on my machines so there was no problem. Tools where Bison, lex and sed. Some bash scripts to put files on common folders & some other stuff. The storage engine was powered with LMDB. It’s the fastest engine on the market, they said in a README.MD. There was also a data structure that was associated with a registered patent.

I have read a lot of wikipedia on NoSQL engines and I have inspected a lot of source code like LMDB and Redis. It’s all about C, it’s brutal and very efficient. It fascinates me. And so what for ?

I currently work on a R&D project to put a NoSQL engine on a Windows service and install it on a Docker image and run it in Azure Container Instances (ACI). As a Micrcosoft partner, my company has to work with Azure and win competencies. Because it’s a R&D project, we have ambitions : make a Redis like software. With distributed features. We want superior performance versus DocumentDB. It will be powered by LMDB. More to come in coming weeks. Stay tuned.

Unit tests in C++

For magazine Programmez, I have made a review of Unit Testing Frameworks. I have described CPPUnit and Google Tests with it’s adapter for Visual Studio. There are two ways to tests the UT : console or Visual Studio Test Exeplorer Window.

I must admit I prefer the console version for running my tests.

Building Boost x86 and x64 – HOWTO

Just a reminder (for me). The command line to rebuild boost library with VS2017 is:

> boostrap.bat

> bjam toolset=msvc-14.1 variant=debug,release threading=multi link=shared address-model=64

Building Log4cpp with VS2017

First, download log4cpp on sourceforge at http://log4cpp.sourceforge.net/ .Latest version is 1.1.3. Let’s decompress the archive.
Go to logcpp/msvc10 folder. Open the mvc10.sln and upgrade the project to latest SDK and Visual Studio 2017 (v141). Done.
Build the log4cpp project… There is an error about a custom build step on file NTEventLogCategories.mc.

We need to go into the folder msvc10 of the src and compile the event viewer definition file (.mc). You need to open Developer Command VS2017:

cd D:\Dev\log4cpp-1.1.3\log4cpp\msvc10>
D:\Dev\log4cpp-1.1.3\log4cpp\msvc10>mc NTEventLogCategories.mc

MC: Compiling NTEventLogCategories.mc

D:\Dev\log4cpp-1.1.3\log4cpp\msvc10>rc -r -fo NTEventLogCategories.res NTEventLogCategories.rc Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384 Copyright (C) Microsoft Corporation. All rights reserved.

D:\Dev\log4cpp-1.1.3\log4cpp\msvc10>link /MACHINE:IX86 -dll -noentry -out:NTEventLogAppender.dll NTEventLogCategories.res Microsoft (R) Incremental Linker Version 14.12.25830.2
Copyright (C) Microsoft Corporation. All rights reserved.

Then in Visual Studio, remove the NTEventLogCategories.mc file from the project and let’s BUILD !
There is a compiler error. To solve it: add HAVE_SNPRINTF to the Preprocessor definitions… and REBUILD ! Now it is successful:

1> Creating library Debug\log4cpp.lib and object Debug\log4cpp.exp
1>log4cpp.vcxproj -> D:\Dev\log4cpp-1.1.3\log4cpp\msvc10\log4cpp\Debug\log4cpp.dll 1>Done building project “log4cpp.vcxproj”.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Unicode, TCHAR, STL std::string & std::stringw and building in mode x64

When you build a Visual Studio project, you have to consider multiple project options:

The Character Set option is very important in the way you will write code. Microsoft wants every developer to build with “Use Unicode Character Set” but bad habits were taken by developers.
They have preferred the option “Use Multi-Byte Character Set (MBCS)” where strings are things you put in “” and that’s all !

So what is the problem Doctor ? Handling Unicode allows you to target multiples languages and it’s the way we build modern softwares.
To help you achieve your migration or your new software, Microsoft provides TCHAR.h with a lot of macros:

– TCHAR means char in MBCS and wchar_t in Unicode

– _T(“toto”) means “toto” in MBCS and L”toto” in Unicode
To properly call routines of the CRT using TCHAR, you have to notice the name of the functions that care of TCHAR : https://docs.microsoft.com/en-us/cpp/c-runtime-library/routine-mappings Example: printf -> Unicode: wprintf, TCHAR: _tprintf
If you use _tprintf and #include , the function _tprintf will be expanded using the right function name.

TCHAR szLisa[10] = _T(“Lisa”);
_tprintf(_T(“%s\n”), szLisa);

But the question is : How do I switch from one mode to another. Example, I use STL, I can have std::string or std::wstring. Using STL strings has a mode that let you recover the characters using the c_str() function.

std::wstring s1 = szLisa;
std::wcout << s1 << std::endl;

If you want to dump from one world to another, the STL has a way:

std::string s2(s1.begin(), s1.end());
std::cout << s2 << std::endl;