首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CloudPebble中出现错误,"ld返回1个退出状态“

CloudPebble中出现错误,"ld返回1个退出状态“
EN

Stack Overflow用户
提问于 2014-03-10 04:10:30
回答 1查看 1.2K关注 0票数 2

所以,我试图制作一个卵石应用程序,当你按下一个按钮时,它会产生一个随机字符串。我很确定我的鹅卵石代码是正确的,但我不知道该如何处理这个错误:

代码语言:javascript
复制
/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

这是我的密码:

代码语言:javascript
复制
#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();
}

我搞不懂我为什么会犯这个错误。这是一个简单的应用程序,我只是有一些小调整。

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-10 06:33:41

根据这个(旧的)常见问题,当您尝试使用未在SDK中实现的C标准库函数时,会发生此错误。如果您查看API引用snprintf是可用的,但sprintf是不可用的。您可以将对sprintfgen中的调用替换为

代码语言:javascript
复制
snprintf(out, 256, "%s, and then %s %s %s.", one[rand() % length1], two[rand() % length2], three[rand() % length3], four[rand() % length4]);

我刚试过这个,它做得很好。

(顺便说一下,最好将out声明为全局静态缓冲区,每次只写一遍,而不是不断地动态分配内存。)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22291937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档