Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Using CMake with the free Embarcadero C++ compiler

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, DocumentsCppProjectsCMakeTest.)

Simple.cpp – this is the main module:

 

[crayon-6740722353f6b305160192/]

Funcs.cpp:

[crayon-6740722353f74406450462/]

and funcs.h:

[crayon-6740722353f76624940978/]

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:

[crayon-6740722353f78915391658/]

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:

[crayon-6740722353f79943108615/]

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:

[crayon-6740722353f7c432390237/]

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.

Exit mobile version