External C libraries - how to include them in the package


I'm developing a service for Cortex A7 powered device running under OpenWrt. Now, I managed to go through entire procedure to bring "HelloWorld" binary to the target device and make it work. Then I started with real stuff and it seems I'm stuck. I'm using the following libraries - json-c, curl, modbus, paho-mqtt - except for the curl, those libs do not exist within the device SDK folder structure.

After some research I understood that I should cross compile those libraries using target platform gcc version which is within the SDK, and the procedure should be like this:

  1. Clone lib repositories from git sources
  2. Cross compile them - here I do not know how should I run make script to use arm-openwrt-linux-gcc located under staging_dir instead of ordinary gcc - should I export PATH and just run make build?
  3. Copy header files and lib binaries to the /lib and /include within the SDK
  4. Include dependencies in my project's makefile
  5. Make it and upload package to the device
  6. Install the package, the binaries will be distributed within the device FS and the application can be executed

Am I right? Is the right procedure?


They have been packaged for OpenWrt for a long time now. Just include the dependencies in your custom package's definition.


My makefile looks like that:

include $(TOPDIR)/rules.mk


TARGET_LDFLAGS:= -lcurl -ljson-c -lmodbus -lpaho-mqtt3c

include $(INCLUDE_DIR)/package.mk

define Package/vtscd

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)

define Build/Compile
	$(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/vtscd.o -c $(PKG_BUILD_DIR)/vtscd.c

define Package/vtscd/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/vtscd $(1)/usr/bin

$(eval $(call BuildPackage,vtscd))

The make fails, because there are no header files for those libraries. I don't have either includes or those nor compiled binaries within this SDK and I don't know how to add them there.

Sorry, I'm a bit new for embedded development and cross compiling.

You're not defining dependencies, you're just telling GCC what you want to link to. You'd add dependencies under define Package/vtscd with DEPENDS

As an example, I recently added Hexedit to OpenWrt's packages and Hexedit depends on libncurses. As such, I defined the package as:

define Package/hexedit
  TITLE:=Viewer and editor in hexadecimal or ASCII

I really recommend you to take a look at how other packages are doing things. Also, use the wiki as well, like e.g. https://openwrt.org/docs/guide-developer/packages

OK, getting somewhere.

I added:

DEPENDS:= +libcurl +libjson-c +libpaho-mqtt3c +libmodbus 

and the first libraries were downloaded, compiled successfully, but the other two was not included (libpaho-mqtt3c and libmodbus). I believe, that I should add some repositories reference to the SDK. But how?

I was wrong: there is python-paho-mqtt package in the repos, but not the C-library. Doesn't look like it'd be a lot of work to add, though.

libmodbus is in the repositories, though; you just probably didn't see it being downloaded, because the build-system failed at finding libpaho-mqtt3c before it reached the dependency for libmodbus.

no, when I first run make after added dependencies it crashed on #include modbus.h, when I changed the order of those declarations - first libpaho, then libmodbus it crashed on libpaho.
I checked build_dir and I found packages curl, json, nghttp2, openssl, - those that were downloaded, but no traces of either modbus nor mqtt

What is the procedure to add those packages to the SDK?

There is for example package for modbus:

But how do I add one to the SDK?

Update - I managed to find a Makefile for libmodbus package, compiled it, added and it works.

For Paho Mqtt - there is simply no package for OpenWrt, so the only think to do is to switch to libmosquitto, which is there.


Have one last question on above. What can I do to build my project with all the libs being static?