Using cmake to build LibreOffice C++ SDK examples

These days, many C++ projects are built using build tools like cmake and meson in addition to GNU make (gmake). In this blog, I have already written on how to compile and run LibreOffice SDK examples using gmake. Now I want to discuss instructions for compiling and running C++ examples using cmake as the build tool. For the previous instructions using gmake, see this older post:

Working with LibreOffice SDK Examples

Compiling and Running using cmake

Using Official LibreOffice Binaries and SDK

To be able to compile and run the C++ examples using cmake, you should have installed LibreOffice and LibreOffice SDK, then you should set LOROOT in CMakeLists.txt to appropriate folder. For LibreOffice 7.3 SDK, you should use this line:

set (LOROOT /opt/libreoffice7.3)

On Windows, you may write:

set (LOROOT c:/progra~1/libreoffice)

And you should also add “c:\program files\libreoffice\programs” to the system path.

Compiling and running the C++ programs would be easy. For some of the examples, you need to run an instance of LibreOffice to listen for the incoming connections. So, you have to invoke:

$ libreoffice7.3 "--accept=socket,port=2083;urp;"

and then just open the project file in Qt Creator (or any other IDE of your choice that supports cmake), and click Build and then Run.

Local Build

If you have built LibreOffice yourself, use the instdir path for LOROOT:

set(LOROOT = /home/hossein/Projects/libreoffice/core/instdir)

If you use a local build, you may need a running instance of LibreOffice. For this purpose, invoke:

$ ./instdir/program/soffice.bin "--accept=socket,port=2083;urp;"

And execute the project from your IDE.

Building and Running from Command Line using cmake

Here I assume that the project is named example. To build and run the example from the command line using cmake, go to the source folder, and then invoke:

$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./example

The CMakeLists.txt file containing the instructions for building the the example project can be as following:

cmake_minimum_required(VERSION 3.5)

project(example LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(LOROOT /opt/libreoffice7.3)

add_executable(example main.cpp)

SET(CMAKE_INCLUDE_CURRENT_DIR ON)

include_directories(
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}/com/sun/star
    ${LOROOT}/sdk/include
    )
target_link_directories(example PRIVATE
    ${LOROOT}/program
    ${LOROOT}/sdk/lib
    )
target_link_libraries(example
    uno_sal
    uno_cppu
    uno_cppuhelpergcc3
    uno_salhelpergcc3
    unoidllo
    xmlreaderlo
    reglo
    mergedlo
    )

add_definitions(-DLINUX)
execute_process(COMMAND ${LOROOT}/sdk/bin/cppumaker -Gc -O. ${LOROOT}/program/types.rdb ${LOROOT}/program/types/offapi.rdb)

Note that you should use add_definitions(-DWIN32) on Windows instead of add_definitions(-DLINUX). Also, link libraries are different:

target_link_libraries(loconvertor
    uno_sal
    uno_cppu
    uno_cppuhelpergcc3
    uno_salhelpergcc3
    )

Essentially, the above file specifies these:

1) Include directories: sdk/include, com/sun/star and also the source directory.

2) Library directories: sdk/lib and program.

3) Name of the linked libraries: uno_sal uno_cppu uno_cppuhelpergcc3 uno_salhelpergcc3 unoidllo xmlreaderlo reglo mergedlo

4) Using cppumaker utility to generate UNO headers from the binary description offapi.rdb. You can read more about LibreOffice SDK utilities in LibreOffice DevGuide chapter 4.

Important Environment Variables

Several environment variables are set when you use the original scripts provided by the LibreOffice SDK. When using cmake, you may have to set these environment variables:

On Linux:

export UNO_PATH=/opt/libreoffice7.3/program
export URE_BOOTSTRAP=vnd.sun.star.pathname:/opt/libreoffice7.3/program/fundamentalrc

and on Windows:

SET UNO_PATH=C:/Progra~1/LibreOffice/program
SET URE_BOOTSTRAP=vnd.sun.star.pathname:C:/Progra~1/LibreOffice/program/fundamental.ini

LibOCon 2021 Presentation

I have previously talked about using cmake to build LibreOffice SDK examples in LibOCon 2021. You can see the presentation below:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

Presentation: LibreOffice SDK Examples OverhaulHossein Nourikhah

This talk was presented in the LibreOffice Conference 2021 (LibOCon 2021) (Slides)

LibreOffice Conference 2021

LibreOffice Conference 2021