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:
Which is the right way to include libraries and compile that on the cloud?
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.
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.
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).
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:
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).
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?
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 ) your code compiles.
.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