[Solved] JSMN compile failed due to multiple definitions

I appreciate getting some direction here; I’m not sure at all what these errors mean.

When I run “particle compile p *” I get:

Compile failed. Exiting.
../../../build/target/services-dynalib/arm//libservices-dynalib.a(services_dynalib.o): In function `jsmn_init':
/spark/compile_service/shared/workspace/6_platform_6_24_2/firmware/services-dynalib/../services/inc/services_dynalib.h:45: multiple definition of `jsmn_init'
../../../build/target/user/platform-6/libuser.a(jsmn.o):jsmn.c:307: first defined here
../../../build/target/services-dynalib/arm//libservices-dynalib.a(services_dynalib.o): In function `jsmn_parse':
/spark/compile_service/shared/workspace/6_platform_6_24_2/firmware/services-dynalib/../services/inc/services_dynalib.h:46: multiple definition of `jsmn_parse'
../../../build/target/user/platform-6/libuser.a(jsmn.o):jsmn.c:152: first defined here
collect2: error: ld returned 1 exit status
make: *** [0c4048cbdd82f7e65953889586c223607d16e955a65f1c6aa66de7683a51.elf] Error 1

I’ve narrowed it down to this class:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Switchjson.hpp"
#include "application.h"
#include "jsmn.h"
    
Switchjson::Switchjson(){}

String Switchjson::get_value(String message, String key) {
	int i;
	int r;
	const char *msg = message;
	const char *ky = key;
	String value;
	jsmn_parser p;
	jsmntok_t t[60]; 
   	jsmn_init(&p);
	r = jsmn_parse(&p, msg, strlen(msg), t, sizeof(t)/sizeof(t[0]));
   	for (i = 1; i < r; i++) {
		if (jsoneq(msg, &t[i], ky) == 0) {
			value = message.substring(t[i+1].start, t[i+1].end);
			i++;
		}
	}
	return value;
}
   
int Switchjson::jsoneq(const char *json, jsmntok_t *tok, const char *s) {
	if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
		strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
		return 0;
	}
	return -1;
}

Hopefully I’m just doing something very dumb.

Thanks,

Hi @amconcepts

This looks like a case where an include file is included more than once and is not protected by an #ifdef or #pragma once so when it gets included again it causes an error like this.

Can you check the jsmn.h file?

Another possibility is that the compiler does not .hpp files. If you change that to .h it might fix it.

Hey thanks for the response. I think I just figured it out.

It looks like jsmn is now included in the photon’s firmware: github particle/photon_firmware/firmware/services/src/jsmn.c

And that some functions have been tweaked for some reason (comparing it to: zserge’s code on Oct 17, 2015).

My code compiled using #include <jsmn.h> but I’m too tired to test it out now. Thanks again!

(Also for reference, I’ve had no problems hpp files. And jsmn.h is using #ifdef)

1 Like