Can't build code with ArduinoJson

I’m trying to build my code with ArduinoJson and ArduinoFake for narive platform, but I’m getting errors.

As tracked per Using ArduinoFake on platformio with native platform but arduino framework · Issue #6 · FabioBatSilva/ArduinoFake · GitHub and fixed by Fix compile error in pgm_read_ptr by tomec-martin · Pull Request #21 · FabioBatSilva/ArduinoFake · GitHub – maybe specifying the latest Git version instead of the latest stable helps.

lib_deps =
   https://github.com/FabioBatSilva/ArduinoFake/archive/refs/heads/master.zip

@maxgerhardt it helps, but I have more errors in my second project(console returns a lot of errors and warnings).

The programs seem to need some adaption. When I run (adapted from StringExample.ino)

#include <stdio.h>
#include <Arduino.h>
#include <ArduinoJson.h>

int main() {
	printf("Hello, world!\n");

	DynamicJsonDocument doc(1024);

	// You can use a String as your JSON input.
	// WARNING: the string in the input  will be duplicated in the JsonDocument.
	//error with String as type, good with std::string
	std::string input =
	  "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
	deserializeJson(doc, input);
	JsonObject obj = doc.as<JsonObject>();
	
    //error:
	//long time = obj[String("time")];
	long time = obj[std::string("time")];
	printf("Time: %ld\n", time);

	std::string sensor = obj["sensor"];
	printf("Sensor: \"%s\"\n", sensor.c_str());

	return 0;
}

as src\main.cpp is compiles and executes just fine.

>.pio\build\native\program.exe
Hello, world!
Time: 1351824120
Sensor: "gps"

But using String in the types instead of std::string seems to upset ArduinoJson. It likely has some checks for whether it’s in a native environment or not and disables some types? Not sure. One can open issues about this, but in principle the library is runnable and works on native.

Oh actually, scrap that, it’s an enablable option: Can't compile ArduinoJson with GNU++11 · Issue #548 · bblanchon/ArduinoJson · GitHub.

[env:native]
platform = native
build_flags = -std=gnu++11 -DARDUINOJSON_ENABLE_ARDUINO_STRING=1
lib_deps =
     https://github.com/FabioBatSilva/ArduinoFake/archive/refs/heads/master.zip
     https://github.com/bblanchon/ArduinoJson/archive/refs/heads/6.x.zip

with

#include <stdio.h>
#include <Arduino.h>
#include <ArduinoJson.h>

int main() {
        printf("Hello, world!\n");

        DynamicJsonDocument doc(1024);

        // You can use a String as your JSON input.
        // WARNING: the string in the input  will be duplicated in the JsonDocument.
        String input =
          "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
        deserializeJson(doc, input);
        JsonObject obj = doc.as<JsonObject>();

        long time = obj[String("time")];
        printf("Time: %ld\n", time);

        String sensor = obj["sensor"];
        printf("Sensor: \"%s\"\n", sensor.c_str());

        return 0;
}

works exactly the same.

>.pio\build\native\program.exe
Hello, world!
Time: 1351824120
Sensor: "gps"

It helps. Unfortunately I can’t start unit tests this time😔.
Errors from terminal: https://drive.google.com/file/d/1xRvDj-prqLvHtmkvPPwuG1K8claVhctn/view?usp=sharing

                                                  ^
In file included from test/test_main.cpp:25:
In file included from test/NativeProcess.hpp:1:
In file included from test/natUnitTest_SymbolsBase/natUnitTest_SymbolsBase.hpp:1:
In file included from include/Symbols/Symbols.hpp:2:
In file included from include/Symbols/SymbolsBase.hpp:6:
In file included from .pio/libdeps/native/ArduinoFake/src/ArduinoFake.h:7:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/map:481:

the project seems kind of complex, does the minimal example above compile? (Regular build, not test).

Normal build returns one error(warning is normal - I didn’t finish one method yet).

When I remove quotation, I get this:

Your main code likely has setup() and loop(), but in native compilation, there needs to be a main() function.

But that’s not what I meant – I meant to create a new blank project and put the exact platformio.ini and code referenced above in it, to check whether it behaves exactly as on my machine. There might be errors in your test code preventing this from compiling, or a general compiler failure if the minimal example does not compile.

Unfortunately on windows machine I get error everywhere I included ArduinoFake.

Would you like me to send the source code?

I found something. When I move methods bodies to hpp files error was vanishing. I suppose, that gcc doesn’t compile correctly cpp files.

It’s very hard to say what’s going on without seeing the source files, can you upload those, or a minimal set that reproduce the error? Or is it all solved now?

Branch: bug/nativeCompilingError

In the last commit in this branch are every change that I did during solving this problem.

@maxgerhardt Did you find anything that could make these errors?