Mqtt using c on openwrt

I'm trying to compile a custom mqtt C program on an openwrt router [ar71xx COMFAST E375AC openwrt 19.07.10] using the header #include <mosquitto.h>.
I'm able to compile the same file in my ubuntu system using normal compilation "gcc test.c -o test" after installing essential libraries such libmosquitto-dev,ssl etc
I have already added and compiled the firmware for my target device including
mosquitto-client-ssl
mosquitto-ssl
libmosquitto-ssl
libmosquittopp
i tried the cross compilation manually using the toolchain /home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/bin/mips-openwrt-linux-gcc test.c -o test -I/usr/include -L/usr/lib
the error was
/home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: /tmp/ccv9rcus.o: in function on_connect': test.c:(.text+0x50): undefined reference to mosquitto_subscribe'
/home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: test.c:(.text+0x64): undefined reference to mosquitto_connack_string' /home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: /tmp/ccv9rcus.o: in function main':
test.c:(.text+0xb0): undefined reference to mosquitto_lib_init' /home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: test.c:(.text+0xc4): undefined reference to mosquitto_new'
/home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: test.c:(.text+0x120): undefined reference to mosquitto_connect_callback_set' /home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: test.c:(.text+0x13c): undefined reference to mosquitto_connect'
/home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: test.c:(.text+0x184): undefined reference to mosquitto_loop_start' /home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: test.c:(.text+0x190): undefined reference to mosquitto_destroy'
/home/source/staging_dir/toolchain-mips_24kc_gcc-11.2.0_musl/lib/gcc/mips-openwrt-linux-musl/11.2.0/../../../../mips-openwrt-linux-musl/bin/ld: test.c:(.text+0x198): undefined reference to `mosquitto_lib_cleanup'
collect2: error: ld returned 1 exit status
then i tried to make a package following openwrt standard procedures for making package of c(https://openwrt.org/docs/guide-developer/helloworld/start).
I updated all the packages and downloaded required packages and added the package on make menuconfig then tried compiling the package.
I folllowed all the procedures but while compiling the package its giving some error.
/home/source/build_dir/target-mips_24kc_musl/helloworld-1.0/helloworld.c:3:10: fatal error: mosquitto.h: No such file or directory
3 | #include <mosquitto.h>
| ^~~~~~~~~~~~~
compilation terminated.
make[2]: *** [Makefile:53: /home/source/build_dir/target-mips_24kc_musl/helloworld-1.0/.built] Error 1
make[2]: Leaving directory '/home/mypackages/examples/helloworld'
time: package/feeds/mypackages/helloworld/compile#0.11#0.03#0.13
ERROR: package/feeds/mypackages/helloworld failed to build.
make[1]: *** [package/Makefile:116: package/feeds/mypackages/helloworld/compile] Error 1
make[1]: Leaving directory '/home/source'
make: *** [/home/source/include/toplevel.mk:230: package/helloworld/compile] Error 2
Then I thought the issue was of me using old version of openwrt toolchain and then tried latest version of stable openwrt available for the router [ath79 COMFAST E375AC openwrt 22.03.5]
the same error was visible here also. Can anyone help me out to fix this issue:)

Please use the </> button located ^^^ when posting code/cli output.

This release is EOL, you're on your own.

After upgrading to a newer OpenWrt build:

You need to package the library first - this has been asked before, please see here and here. The second thread contains a link to some example code.

1 Like