Using external C/CPP libraries - Cross Compiling


#1

I am trying to use an external library for a CPP project in Legato. I can create a C++ project and it compiles fine but I am not sure how to integrate and cross-compile an external library. I am able to use “headers only” libraries as they only contain templates but I am still unsure how to generate a .so to include it in the project.

I am trying to get up and running the PAHO project, more specifically the CPP version. The CPP version requires the C version to be installed first to compile. I can do it no problem on desktop but for sure the binaries won’t be working on legato.

They provide a CMakeList but I can’t find a way to use it with legato. Any help would really be appreciated. Thank you!


#2

I was able to figure it out, I was just inexperienced with CMake. This might sound basic to some of you but I’ll document what I did here if it could be of help for anyone in the future:

  1. Clone both Paho C and Paho C++ libs to different folders
  2. Open CMakeGui, select PahoC repo as the source, and create a folder called “bin_cross_compiled” inside it and use it as the target. Hit Configure -> Specify Toolchain files for cross-compiling
  3. Use what is called a toolchain file, which basically contains CMAKE variables for cross-compiling, like which C compiler to use.

Mine is

➜ toolchains cat arm_legato_wp85xx.cmake
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

SET(CMAKE_C_COMPILER /opt/swi/y17-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)
SET(CMAKE_CXX_COMPILER /opt/swi/y17-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++)

  1. Make sure to change your CMAKE_INSTALL_PREFIX afterwards, otherwise make install will install the cross-compiled binaries to your host system.
  2. You can activate various settings (I used SSL, disabled testing, doc, etc…)
  3. Hit Generate
  4. Navigate to your build folder described in step 1
  5. run make then make install and you should have your binaries for Paho-C in the CMAKE_INSTALL_PREFIX folder.

The steps are pretty much the same for Paho C++ but you’ll need to also set up a variable to point to the build folder of PahoC since it needs it to build.