3. Compiling client applications

3.1. Compiling the library

Anjay uses CMake for project configuration. To compile the library with default settings:

cmake . && make

3.1.1. Cross-compiling

First, prepare a CMake toolchain file (see CMake documentation), then pass CMAKE_TOOLCHAIN_FILE when configuring Anjay:

cmake -DCMAKE_TOOLCHAIN_FILE=$YOUR_TOOLCHAIN_FILE . && make

An example CMake toolchain file for an ARM Cortex-M3-powered STM3220 platform may look like so:

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)

set(CMAKE_C_COMPILER arm-none-eabi-gcc)

# CMAKE_C_FLAGS set in a toolchain file get overwritten by CMakeCInformation.cmake
# unless they are FORCEfully set in the cache
# See http://stackoverflow.com/a/30217088/2339636
unset(CMAKE_C_FLAGS CACHE)
set(CMAKE_C_FLAGS "-mcpu=cortex-m3 -mthumb -msoft-float -ffunction-sections -fdata-sections -fno-common -fmessage-length=0 -std=gnu99 --specs=nosys.specs" CACHE STRING "" FORCE)

set(CMAKE_EXE_LINKER_FLAGS "-Wl,-gc-sections")

3.2. Installing the library

To install Anjay headers and libraries in /usr/local:

cmake . && make && sudo make install

A custom installation prefix may be set using CMAKE_INSTALL_PREFIX:

cmake -DCMAKE_INSTALL_PREFIX=/custom/path . && make && make install

3.3. Including the library in an application

3.3.1. CMake projects

The preferred method of using Anjay in custom projects is to use CMake find_package command after installing the library:

find_package(anjay)
include_directories(${ANJAY_INCLUDE_DIRS})
target_link_libraries(my_executable ${ANJAY_LIBRARIES}) # or ANJAY_LIBRARIES_STATIC for a static library

Note

If a custom installation path is used, you need to set anjay_DIR CMake variable to $YOUR_INSTALL_PREFIX/lib/anjay.

3.3.2. Alternative build systems

For other build systems, necessary flags can be retrieved using cmake command:

cmake --find-package -DNAME=anjay -DLANGUAGE=C -DCOMPILER_ID=Generic -DMODE=<mode>

Where <mode> is one of:

  • EXIST - check whether the library can be found,
  • COMPILE - print compilation flags,
  • LINK - print linking arguments.

Note

If a custom installation prefix is used, you need to also pass -Danjay_DIR=$YOUR_INSTALL_PREFIX/lib/anjay.