How to include library for cloud compile

Hi there,

I'm starting to work with VS Code and Particle Workbench and I think it is great.
Previosly I worked with the Web IDE because I need to update my device remotely.

I'm working a project that involves MQTT and for that I used the MQTT library installed via

Particle: Install Library.

I'm building a library that wraps the functions included into MQTT library in order to reuse this library in several project.
To do that I wrote a library "mqtt_service" and another library called "common" (common library includes some project #define and error code).

The local compile returns without errors, but when I start cloud compile I get this error:

fatal error: ..lib\local\common\common.h: No such file or directory

I included the common library in the *.ino files as:

#include "..\lib\local\common\common.h"

This is the project structure:

ProjectStructure

Which is the right way to include libraries and compile that on the cloud?

Thanks for the help.

Best regards,
Federico

Is there really a backslash in that library path (service \ mqtt)?
That’s never a wise move, nor are blanks in a path.

Also the sub-folder local can be considered superfluous as lib already is meant for local libraries - cloud-based libraries would be referenced via the a dependencies entrie in project.properties and these also have precedence over local libraries when building in the cloud.

When sticking your libraries in their dedicated directories directly (i.e. ./lib/myLib/myLib.h) you would only write #include "myLib.h") and Workbench will find the library just the same.

Hello @ScruffR,

thanks for the reply.

I never use blank space in the files name, I’m agree with you.
The space between “service \ mqtt” is added by VSCode and it is only for visualization because the service folder now has only one subfolder.

I added the folder “local” just to separe my libries from third party libraries installed from the cloud.

Currently, my project.propertis contains:

name=PowerAndEnvironmentMonitor
dependencies.SparkIntervalTimer=1.3.8
dependencies.SparkFun_BQ27441=1.0.1

How can I specify the path of my libraries?

Thanks for your tiem.

Best regards,
Federico

Since I haven’t pulled up a test project to replicate your issue I’m going to guess, but have you tried #include "local/common/common.h" (leaving the lib out as it will be used by default)?

When you write #include "lib/local/common/common.h" the file will be searched either in ./src/lib/local/common/ or in ./lib/lib/local/common. Since both places don’t exist, the file obviously cannot be found.

I tried to include the library in my *.ino file with #include "local\common\common.h" and #include "local/common/common.h" (I’m working on windows and I tried both slashes).

But I get the same error :arm-none-eabi-gcc -DSTM32_DEVICE -DSTM32F2XX -DPLATFORM_THREADING=1 -DPLATFORM_ID=6 -DPLATFORM_NAME=photon -DUSBD_VID_SPARK=0x2B04 -DUSBD_PID_DFU=0xD006 -DUSBD_PID_CDC=0xC006 -DSPARK_PLATFORM -g3 -gdwarf-2 -Os -mcpu=cortex-m3 -mthumb -DINCLUDE_PLATFORM=1 -DPRODUCT_ID=6 -DPRODUCT_FIRMWARE_VERSION=65535 -DUSE_STDPERIPH_DRIVER -DDFU_BUILD_ENABLE -DSYSTEM_VERSION_STRING=1.4.4 -DRELEASE_BUILD -I./inc -I../wiring/inc -I../system/inc -I../third_party/miniz/miniz -I../services/inc -I../third_party/nanopb/nanopb -I../communication/inc -I../hal/inc -I../hal/shared -I../hal/src/photon -I../hal/src/stm32f2xx -I../hal/src/stm32 -I../hal/src/photon/api -I../hal/src/photon/include -I../hal/src/photon/wiced/security/BESL/host/WICED/ -I../hal/src/photon/wiced/security/BESL/include -I../hal/src/photon/wiced/security/BESL -I../hal/src/photon/wiced/security/BESL/crypto -I../hal/src/photon/wiced/WWD/include/ -I../hal/src/photon/wiced/platform/include/ -I../hal/src/photon/wiced/platform/GCC/ -I../hal/src/photon/wiced/security/BESL/supplicant/ -I../hal/src/photon/libraries/crypto -I../hal/src/photon/libraries/daemons/DNS_redirect -I../platform/shared/inc -I../platform/MCU/STM32F2xx/STM32_USB_Host_Driver/inc -I../platform/MCU/STM32F2xx/STM32_USB_OTG_Driver/inc -I../platform/MCU/STM32F2xx/STM32_StdPeriph_Driver/inc -I../platform/MCU/STM32F2xx/STM32_USB_Device_Driver/inc -I../platform/MCU/STM32F2xx/SPARK_Firmware_Driver/inc -I../platform/MCU/shared/STM32/inc -I../platform/MCU/STM32F2xx/CMSIS/Include -I../platform/MCU/STM32F2xx/CMSIS/Device/ST/Include -I../dynalib/inc -Isrc -I./libraries -Ilib/SparkIntervalTimer/src -Ilib/SparkFun_BQ27441/src -Ilib/MQTT/src -Ilib/local/src -I. -MD -MP -MF ../build/target/user/platform-6-msrc/PowerAndEnvironmentMonitor.o.d -ffunction-sections -fdata-sections -Wall -Wno-switch -Wno-error=deprecated-declarations -fmessage-length=0 -fno-strict-aliasing -DSPARK=1 -DPARTICLE=1 -Wundef -DSTART_DFU_FLASHER_SERIAL_SPEED=14400 -DSTART_YMODEM_FLASHER_SERIAL_SPEED=28800 -DBOOTLOADER_SDK_3_3_0_PARTICLE -DPARTICLE_DCT_COMPATIBILITY -DSPARK_PLATFORM_NET=BCM9WCDUSI09 -fno-builtin-malloc -fno-builtin-free -fno-builtin-realloc -DLOG_INCLUDE_SOURCE_INFO=1 -DPARTICLE_USER_MODULE -DUSER_FIRMWARE_IMAGE_SIZE=0x20000 -DUSER_FIRMWARE_IMAGE_LOCATION=0x80A0000 -DMODULAR_FIRMWARE=1 -DMODULE_VERSION=6 -DMODULE_FUNCTION=5 -DMODULE_INDEX=1 -DMODULE_DEPENDENCY=4,2,1406 -DMODULE_DEPENDENCY2=0,0,0 -D_WINSOCK_H -D_GNU_SOURCE -DLOG_MODULE_CATEGORY="\"app\"" -fno-exceptions -fno-rtti -fcheck-new -std=gnu++14 -c -o ../build/target/user/platform-6-msrc/PowerAndEnvironmentMonitor.o src/PowerAndEnvironmentMonitor.cpp src/PowerAndEnvironmentMonitor.ino:28:33: fatal error: local\common\common.h: No such file or directory #include "local\common\common.h"

I also tried to modify the project.properties:

name=PowerAndEnvironmentMonitor
dependencies.SparkIntervalTimer=1.3.8
dependencies.SparkFun_BQ27441=1.0.1
dependencies.MQTT=0.4.29
dependencies.common='lib\local\common\common.h'

Adding the last line, but nothings has changed (I tried with single and double quotes).
Is that the right way to modify project.properties?

I was thinking about this other solution: If local compile works, is it possbile to compile locally and then flash via cloud?

Thank you and best regards,
Federico

Hello @ScruffR,

I think I solved the problem partially.
I read the Library file structure and I adapted my library structure.

This is how my library folder looks like

Now each library has a src folder, a library.properties file and a readme.

When compile, I get a different errors.
Now the errors regards the include chain.

In file included from ./inc/application.h:33:0,
                 from c:/Users/Federico/Documents/Progetti/Particle/PowerAndEnvironmentMonitor/lib/MQTT/src/MQTT.h:66,
                 from c:/Users/Federico/Documents/Progetti/Particle/PowerAndEnvironmentMonitor/lib/mqtt_service/src/mqtt_service.c:25:
../system/inc/system_version.h:253:19: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
     uint16_t size = sizeof(SystemVersionInfo);
                   ^
In file included from ../wiring/inc/spark_wiring.h:39:0,
                 from ./inc/application.h:40,
                 from c:/Users/Federico/Documents/Progetti/Particle/PowerAndEnvironmentMonitor/lib/MQTT/src/MQTT.h:66,
                 from c:/Users/Federico/Documents/Progetti/Particle/PowerAndEnvironmentMonitor/lib/mqtt_service/src/mqtt_service.c:25:
../wiring/inc/spark_wiring_constants.h:31:23: fatal error: type_traits: No such file or directory
compilation terminated.
make[3]: *** [../build/module.mk:289: ../build/target/user/platform-6-m/PowerAndEnvironmentMonitor/mqtt_service/src/mqtt_service.o] Error 1
make[2]: *** [../../../build/recurse.mk:12: user] Error 2
make[1]: *** [../build/recurse.mk:12: modules/photon/user-part] Error 2
make: *** [C:\Users\Federico\.particle\toolchains\buildscripts\1.9.2\Makefile:54: compile-user] Error 2
The terminal process terminated with exit code: 2

The include chain is: mqtt_service includes MQTT (installed from the cloud) includes:

  • #include “application.h”
  • #include “spark_wiring_string.h”
  • #include “spark_wiring_tcpclient.h”
  • #include “spark_wiring_usbserial.h”

Do you have any hint?

Thank you very much and best regards,
Federico

You don’t need the latter three includes and the first has been superseded with #include <Particle.h>.

These error messages look rather arbitrary and that often happens when there is something else “wrong” in the code or order of proprocessor directives that may the proprocessor (converting .ino to .cpp).

Hello @ScruffR,

I followed your instrucntion but the error remains:

:::: COMPILING APPLICATION

In file included from ./inc/application.h:33:0,
                 from ./inc/Particle.h:5,
                 from c:/Users/Federico/Documents/Progetti/Particle/PowerAndEnvironmentMonitor/lib/MQTT/src/MQTT.h:66,
                 from c:/Users/Federico/Documents/Progetti/Particle/PowerAndEnvironmentMonitor/lib/mqtt_service/src/mqtt_service.c:25:
../system/inc/system_version.h:253:19: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
     uint16_t size = sizeof(SystemVersionInfo);
                   ^
In file included from ../wiring/inc/spark_wiring.h:39:0,
                 from ./inc/application.h:40,
                 from ./inc/Particle.h:5,
                 from c:/Users/Federico/Documents/Progetti/Particle/PowerAndEnvironmentMonitor/lib/MQTT/src/MQTT.h:66,
                 from c:/Users/Federico/Documents/Progetti/Particle/PowerAndEnvironmentMonitor/lib/mqtt_service/src/mqtt_service.c:25:
../wiring/inc/spark_wiring_constants.h:31:23: fatal error: type_traits: No such file or directory
compilation terminated.
make[3]: *** [../build/module.mk:289: ../build/target/user/platform-6-m/PowerAndEnvironmentMonitor/mqtt_service/src/mqtt_service.o] Error 1
make[2]: *** [../../../build/recurse.mk:12: user] Error 2
make[1]: *** [../build/recurse.mk:12: modules/photon/user-part] Error 2
make: *** [C:\Users\Federico\.particle\toolchains\buildscripts\1.9.2\Makefile:54: compile-user] Error 2
The terminal process terminated with exit code: 2

For both local and cloud compile.
I also tried different toolchain 1.4.0, 1.4.4, 1.5.0 but these errors remains.

What kind of errors in my code can generate preprocessor errors?

Thanks for your time.
Federico

Can you zip your project and PM me a link?

I sent you a PM.

Thanks!

1 Like

You need to rename your mqtt_service.c to mqtt_service.cpp or add the directives to use correct calling conventions.

When you’ve done that you’ll get better error messages like this one here

lib/mqtt_service/src/mqtt_service.cpp:33:42: error: 'mqttCallback' was not declared in this scope
 static MQTT clientMQTT(mqttBroker, 1883, mqttCallback);

Once you address that and move your forward declaration/prototype of mqttCallback before it’s first use (that’s what forward declarations are for :wink: ) your code compiles.

2 Likes

Thank you very much @ScruffR!!!

Without your help I never find this solution!
With C files the error description is very crypthic.

So each library should be saved as CPP file. Am I right?

Thanks again!
Federico

.CPP is the default (especially when using C++ constructs), but you can still use .C files, but then you need to add the appropriate directives like this

#ifdef __cplusplus
extern "C"
{
#endif

//C code goes here

#ifdef __cplusplus
} // extern "C"
#endif