Domanda

So, I'm trying to make a Pebble app that generates a random string when you press a button. I'm pretty sure I have the Pebble code right, but I'm not sure what to do with this error:

/sdk2/[long stuff here]/ In function `_sbrk_r':
/home/[more long stuff]: undefined reference to `_sbrk'
collect2: error: ld returned 1 exit status
Waf: Leaving directory `/tmp/tmpX94xY7/build'
Build failed

And here's my code:

#include <pebble.h>
#include <stdlib.h>
#include <stdio.h>

Window *window;
TextLayer *text_layer;

char* one[] = {"string1", "stringone", "stringuno"};
char* two[] = {"string2", "stringtwo", "stringdos"};
char* three[] = {"string3", "stringthree", "stringtres"};
char* four[] = {"string4", "stringfour", "stringcuatro"};

int length1 = sizeof(one)/sizeof(*one);
int length2 = sizeof(two)/sizeof(*two);
int length3 = sizeof(three)/sizeof(*three);
int length4 = sizeof(four)/sizeof(*four);

char* gen()
{
    char out[256];
    sprintf(out, "%s, and then %s %s %s.", one[rand() % length1], two[rand() % length2], three[rand() % length3], four[rand() % length4]);

    char* result = malloc(strlen(out) + 1);
    strcpy(result, out);
    return result;
}

static void select_click_handler(ClickRecognizerRef recognizer, void *context)
{
    char* stringGen = gen();
    text_layer_set_text(text_layer, stringGen);
    free(stringGen);
}

static void click_config_provider(void *context)
{
    window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
    window_single_click_subscribe(BUTTON_ID_UP, select_click_handler);
    window_single_click_subscribe(BUTTON_ID_DOWN, select_click_handler);
}

static void window_load(Window *window)
{
    Layer *window_layer = window_get_root_layer(window);
    GRect bounds = layer_get_bounds(window_layer);

    text_layer = text_layer_create((GRect) { .origin = { 0, 72 }, .size = { bounds.size.w, bounds.size.h } });
    text_layer_set_text(text_layer, "Press >>>");

    text_layer_set_text_alignment(text_layer, GTextAlignmentCenter);
    layer_add_child(window_layer, text_layer_get_layer(text_layer));
}

static void window_unload(Window *window)
{
    text_layer_destroy(text_layer);
}

void handle_init(void)
{
    window = window_create();
    window_set_click_config_provider(window, click_config_provider);
    window_set_window_handlers(window, (WindowHandlers) {
        .load = window_load,
        .unload = window_unload,
    });
    const bool animated = true;
    window_stack_push(window, animated);    
}

void handle_deinit(void)
{
      text_layer_destroy(text_layer);
      window_destroy(window);
}

int main(void)
{
    handle_init();
    app_event_loop();
    handle_deinit();
}

I can't figure out why I'm getting that error. It's a simple application, I just have these little tweaks.

Thank you in advance for your help!

È stato utile?

Soluzione

According to this (old) FAQ, that error happens when you try to use a C standard library function that hasn't been implemented in the SDK. If you look in the API reference, snprintf is available, but not sprintf. You can replace your call to sprintf in gen with something like

snprintf(out, 256, "%s, and then %s %s %s.", one[rand() % length1], two[rand() % length2], three[rand() % length3], four[rand() % length4]);

I just tried this out and it builds fine.

(As an aside, it may be a better a idea to declare out a global static buffer and just write over it each time, instead of constantly dynamically allocating memory.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top