Using CMake with the free Embarcadero C++ compiler

Posted by on in Programming

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.



About
Gold User, No rank,
C++ Product Manager, looking after C++Builder as well as the IDE.

Comments

  • NormanDunbar
    NormanDunbar Thursday, 9 February 2017

    Sadly, however, it seems that "Cmake 3.8.0-rc1" doesn't like the latest free Embarcadero C++ compiler. Contrary to what is output, below, by the CMake batch file we created, the compiler is definitely on my PATH, and is used frequently in that manner.



    Running the batch file, cmake_bcc32c.bat gives the following:

    -- The C compiler identification is unknown
    -- The CXX compiler identification is unknown
    CMake Error at CMakeLists.txt:1 (project):
    The CMAKE_C_COMPILER:

    bcc32c.exe

    is not a full path and was not found in the PATH.

    Tell CMake where to find the compiler by setting either the environment
    variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
    the compiler, or to the compiler name if it is in the PATH.


    CMake Error at CMakeLists.txt:1 (project):
    The CMAKE_CXX_COMPILER:

    bcc32c.exe

    is not a full path and was not found in the PATH.

    Tell CMake where to find the compiler by setting either the environment
    variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
    to the compiler, or to the compiler name if it is in the PATH.


    CMake Warning (dev) in CMakeLists.txt:
    No cmake_minimum_required command is present. A line of code such as

    cmake_minimum_required(VERSION 3.8)

    should be added at the top of the file. The version specified may be lower
    if you wish to support older CMake versions for this project. For more
    information run "cmake --help-policy CMP0000".
    This warning is for project developers. Use -Wno-dev to suppress it.

    -- Configuring incomplete, errors occurred!
    See also "C:/SourceCode/cmake_test/CMakeTest/build/CMakeFiles/CMakeOutput.log".
    See also "C:/SourceCode/cmake_test/CMakeTest/build/CMakeFiles/CMakeError.log".

    Even if I add something like:

    set CC=bcc32c.exe
    set CXX=bcc32c.exe
    set CMAKE_C_COMPILER=bcc32c.exe
    set CMAKE_CXX_COMPILER=bcc32c.exe

    Where "bcc32c.exe" can be just the name, or the full path, it still fails miserably.

    Sigh.

    Cheers,
    Norm.

  • David Millington
    David Millington Tuesday, 21 March 2017

    Sounds like a CMake bug. Since it's a RC, have you filed a bug with them?

  • David Millington
    David Millington Wednesday, 15 February 2017

    Hi Norman - that's very strange. Do you know if CMake gives that same error (not found on path) in other circumstances? Also, are you running this from a command line started from the Start menu > Embarcadero RAD Studio 10.1 Berlin > RAD Studio Command Prompt? That sets up a number of environment variables and paths.

  • yulen38
    yulen38 Monday, 20 March 2017

    Hi David I have build success but in console message is:

    C:\dm\bin>cmake_bcc32c.bat
    -- The C compiler identification is Embarcadero 7.20.35653
    -- The CXX compiler identification is Embarcadero 7.20.35653
    -- Check for working C compiler: C:/dm/bin/bcc32c.exe
    -- Check for working C compiler: C:/dm/bin/bcc32c.exe -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working CXX compiler: C:/dm/bin/bcc32c.exe
    -- Check for working CXX compiler: C:/dm/bin/bcc32c.exe -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    CMake Error at CMakeLists.txt:9 (add_executable):
    add_executable called with incorrect number of arguments


    -- Configuring incomplete, errors occurred!
    See also "C:/dm/bin/build/CMakeFiles/CMakeOutput.log".
    MAKE Version 5.41 Copyright (c) 1987, 2014 Embarcadero Technologies, Inc.
    Fatal: Unable to open makefile
    CMakeBCC: Results available in 'build' folder

    I have make file in the same folder and th outpout log dont give any information about error.

    Thanks

  • David Millington
    David Millington Tuesday, 21 March 2017

    Build success, but you get an error about being unable to open the makefile? That's very odd.

    I'm not sure I can answer the reasons for that one. Maybe a CMake support forum would be better?

  • NormanDunbar
    NormanDunbar Thursday, 9 February 2017

    Hmmm. In order to get the code to compile initially with the free Embarcadero 10.1 C++ compiler, even before starting to test out cmake, the following was required:

    Simple.cpp:

    add the following:

    #include
    #include "funcs.h"

    Funcs.cpp:

    add:

    #include


    Once that was done, it compiled and ran ok.

  • TotteKarlsson
    TotteKarlsson Sunday, 20 November 2016

    I'm also in need of getting the clang 64bit compiler working with CMake. A large number of ThirdParty libraries are using CMake, and getting those available in the IDE will be great.

  • David Millington
    David Millington Friday, 25 November 2016

    We have this on the roadmap for an update next year.

  • OliWe
    OliWe Monday, 24 October 2016

    Any news about Cmake and bcc64 - it would be great if this combination would work :_)

  • David Millington
    David Millington Monday, 24 October 2016

    Not yet - it's in our public roadmap for first half next year.

  • Cristian A12917
    Cristian A12917 Tuesday, 19 July 2016

    The free compiler works also with ninja buildsystem: https://ninja-build.org/

    $ cmake -G Ninja -DCMAKE_CXX_COMPILER=bcc32c -DCMAKE_C_COMPILER=bcc32c [path_to_project]
    $ ninja

    I haven't tried it on big projects, only on a "hello world".

    Because it works with Ninja I was able to use it with Qt Creator, which after configuring the compiler kit to use clang error messages, it was able to pin point the line which had an error.

    The only thing which is missing is the debugger to have a full fledged alternative IDE.

  • David Millington
    David Millington Wednesday, 20 July 2016

    Fantastic - that's very cool.

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: