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.
Table of Contents
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:
- Two C++ files and a header – this is the source we want to build
- CMakeLists.txt
- 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:
1 2 3 4 5 6 7 8 9 |
#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:
1 2 3 4 5 6 |
#include "funcs.h" std::string GetMessage() { return "Hello world."; } |
and funcs.h:
1 2 3 4 5 6 7 8 9 10 |
#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:
1 |
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:
1 2 3 4 5 6 7 8 9 |
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:
1 2 3 4 5 6 7 8 |
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.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
I have followed the instructions provided but still I have bumped into an error that is currently blocking me from using CMake paired with Embarcadero. Hoping you guys have already figured this problem out and have a working soluton as it will really help me with the current plan of automating building our embarcadero application. BTW, we have a paid license using your IDE and wanting to make it work in integrating into a CI/CD pipeline.
/* Results below */
>cmake_bcc32c.bat
>REM Make sure cmake.exe , bcc32c.exe and borland make.exe are in the Path
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument value or use a … suffix to tell
CMake that the project does not need compatibility with older versions.
— The C compiler identification is Embarcadero 7.70.38315
— The CXX compiler identification is Embarcadero 7.70.38315
— Detecting C compiler ABI info
— Detecting C compiler ABI info – failed
— Check for working C compiler: C:/Program Files (x86)/Embarcadero/Studio/23.0/bin/bcc32c.exe
— Check for working C compiler: C:/Program Files (x86)/Embarcadero/Studio/23.0/bin/bcc32c.exe – broken
CMake Error at C:/Strawberry/c/share/cmake-3.26/Modules/CMakeTestCCompiler.cmake:67 (message):
The C compiler
“C:/Program Files (x86)/Embarcadero/Studio/23.0/bin/bcc32c.exe”
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/GBF/source/repos/test_automation/build/CMakeFiles/CMakeScratch/TryCompile-86sgbu
Run Build Command(s):C:/Strawberry/c/bin/cmake.exe -E env VERBOSE=1 make -f Makefile cmTC_47d9f\fast && Makefile:32: *** missing separator. Stop.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
— Configuring incomplete, errors occurred!
MAKE Version 5.43 Copyright (c) 1987, 2019 Embarcadero Technologies, Inc.
Fatal: Unable to open makefile
CMakeBCC: Results available in ‘build’ folder
Hi Bryan,
I’m not sure about that specific error, but we have seen problems with newer versions of CMake. Some things have changed internally in Cmake since we added support. Can you try an older version, such as 3.19, please?
And just to check, did you patch the Embarcadero CMake support, as noted here? This is a detailed guide on using CMake: https://web.archive.org/web/20220509204137/https://docwiki.embarcadero.com/RADStudio/Sydney/en/Using_CMake_with_C%2B%2B_Builder