Using CMake with the free Embarcadero C++ compiler

by Jul 19, 2016

Do you want to use CMake with the newly released free compiler? This post quickly runs you through installing both the compiler and CMake, and shows an example build using two C++ files and the corresponding CMakeLists.txt.

CMake is a popular third-party build tool.

Ensuring the compiler is installed

To start, make sure you have the compiler in your system path. (When you download and extract it, you need to add the \bin folder to your system path. Doing so is outside the scope of this blog post but there are full instructions in the installation readme in the download.) To test this, open a command prompt by typing 'cmd' in the Windows Search box and selecting 'Command Prompt'. In it, type 'bcc32c' and you should see a message with the version number, followed by a warning that you didn't give it any files to compile. (If you want more details, by the way, type 'bcc32 –version'.) If you get an error that bcc32c is not found, you need to add the extracted download's \bin folder to your system path.

Installing CMake

CMake is a third-party tool and you need to install it separately. Head to cmake.org's download page, and choose the latest version's binary installer – 32 or 64-bit, up to you. For this post, I downloaded cmake-3.6.0-win64-x64.msi. Run it, and after installation make sure it is installed correctly by again opening a command prompt and this time typing 'cmake'. You will get a message telling you about its usage. If you don't, you may need to restart or log and and log in again for the program to be found.

Using CMake

CMake builds based on a text file called CMakeLists.txt. You can find more information in their developer documentation.

We're going to make a sample project that consists of three parts:

  1. Two C++ files and a header – this is the source we want to build
  2. CMakeLists.txt
  3. A batch script to drive it

Let's start with the source.

C++ source

Using Notepad or another text editor, save the following three files to disk in a good location (say, Documents\CppProjects\CMakeTest.)

Simple.cpp – this is the main module:

 

#include "funcs.h" 

int main(void) {
	const auto a = 4;
	const std::string str = GetMessage();

	std::cout << str << std::endl;
	PrintResult([=]() { return a + str.length(); });
}

Funcs.cpp:

#include "funcs.h"

std::string GetMessage()
{
	return "Hello world.";
}

and funcs.h:

#include <string>
#include <iostream>

template <typename F>
void PrintResult(F f)
{
	std::cout << f() << std::endl;
}

std::string GetMessage();

Together these form a program that demonstrates a couple of C++11 features: auto, lambdas, and passing a lambda to a template function. In fact you can compile these manually without using CMake by calling bcc32c on the command line:

bcc32c simple.cpp funcs.cpp

and you will find a 'simple.exe' file in the same folder as the source.

However, we want to use CMake, so you can skip that and create a CMakeLists.txt file.

CMakeLists.txt

This file tells CMake which files to build – and can contain a lot more complex information, such as dependencies. We're going to write a very simple one, though, that tells CMake to build all the .cpp files in the current folder.

In Notepad, create a new file and paste in the following, saving it as CMakeLists.txt in the same folder as the source:

cmake_minimum_required (VERSION 2.6)
project (Example)

file(GLOB Example_SRC
    "*.h"
    "*.cpp"
)

add_executable(Example ${Example_SRC})

This defines a project called Example, tells it to find all .cpp and .h files, and build them in one executable. A full CMake file might list all file individually, but this is simple and easy to demonstrate.

Batch script to drive Cmake

Finally, we want a batch file to drive CMake. Create another new text file in Notepad, and paste in the following:

REM Make sure cmake.exe , bcc32c.exe and borland make.exe are in the Path
@echo off
mkdir build
cd build 
cmake -G"Borland Makefiles" -DCMAKE_CXX_COMPILER="bcc32c.exe" -DCMAKE_C_COMPILER="bcc32c.exe" -DCMAKE_VERBOSE_MAKEFILE=1 ..
make
cd ..
echo CMakeBCC: Results available in 'build' folder

Save this file in the same folder as the source and CMakeLists.txt, with the filename 'cmake_bcc32c.bat'. Make sure it really does have the .bat extension, not .txt.

This batch file creates a subfolder called 'build', where all results will be placed. (CMake can create a large number of extra files.) It then invokes CMake using the Borland generator – that's because bcc32c has the same command-line interface (same flags) as the old, classic bcc32 compiler.

On the command line, run this by typing 'cmake_bcc32c.bat.'  You will see a lot of output, but at the end you should find a 'build' folder that contains 'Example.exe' along with many other files.

Final notes

We're using CMake's Borland generator here, because bcc32c is compatible with bcc32's command-line flags. Bcc64 isn't, so using it is more complicated. We're investigating working with CMake to get native support for the new Clang 64-bit compiler. Get in touch with us if this is important to you.

We're also running a C++ boot camp in the second week of August. It's a couple of hours long each day for five days, covering a wide range of C++ topics from using FMX, our cross-platform UI framework, to a C++ language deep dive, building games, and writing an app (or porting to) mobile devices like an iPhone or Android phone or tablet. Lots of interesting stuff – you can register here.