Не удалось проверить «значение» в блоках кода с Linux - PullRequest
0 голосов
/ 20 сентября 2018

Извините, мой плохой английский.Я работаю с образцом кода Raspberry pi 3.Я пытаюсь запустить пример кода на кодовых блоках, и я работаю для обучения.Я настроил параметр отладки на основе Makefile примера кода.Я настраиваю настройку компоновщика.Когда я строю код на Code Blocks, он не показывает ошибку.Но когда я запускаю код.Я взял ошибку на консоли.Ошибка:

Пример шрифта: /home/pi/Desktop/Font_example/main.c: 101: main: Утверждение 's == 0' не выполнено.

Я заметил строку.Код:

// Test app for VG font library.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>

#include "/opt/vc/src/hello_pi/libs/vgfont/bcm_host.h"
#include "/opt/vc/src/hello_pi/libs/vgfont/vgfont.h"

static const char *strnchr(const char *str, size_t len, char c)
{
   const char *e = str + len;
   do {
      if (*str == c) {
         return str;
      }
   } while (++str < e);
   return NULL;
}

int32_t render_subtitle(GRAPHICS_RESOURCE_HANDLE img, const char *text, const int skip, const uint32_t text_size, const uint32_t y_offset)
{
   uint32_t text_length = strlen(text)-skip;
   uint32_t width=0, height=0;
   const char *split = text;
   int32_t s=0;
   int len = 0; // length of pre-subtitle
   uint32_t img_w, img_h;

   graphics_get_resource_size(img, &img_w, &img_h);

   if (text_length==0)
      return 0;
   while (split[0]) {
      s = graphics_resource_text_dimensions_ext(img, split, text_length-(split-text), &width, &height, text_size);
      if (s != 0) return s;
      if (width > img_w) {
         const char *space = strnchr(split, text_length-(split-text), ' ');
         if (!space) {
            len = split+1-text;
            split = split+1;
         } else {
            len = space-text;
            split = space+1;
         }
      } else {
         break;
      }
   }
   // split now points to last line of text. split-text = length of initial text. text_length-(split-text) is length of last line
   if (width) {
      s = graphics_resource_render_text_ext(img, (img_w - width)>>1, y_offset-height,
                                     GRAPHICS_RESOURCE_WIDTH,
                                     GRAPHICS_RESOURCE_HEIGHT,
                                     GRAPHICS_RGBA32(0xff,0xff,0xff,0xff), /* fg */
                                     GRAPHICS_RGBA32(0,0,0,0x80), /* bg */
                                     split, text_length-(split-text), text_size);
      if (s!=0) return s;
   }
   return render_subtitle(img, text, skip+text_length-len, text_size, y_offset - height);
}

int main(void)
{
   GRAPHICS_RESOURCE_HANDLE img;
   uint32_t width, height;
   int LAYER=1;
   bcm_host_init();
   int s;

   s = gx_graphics_init(".");
   assert(s == 0);                  //101. line

   s = graphics_get_display_size(0, &width, &height);
   assert(s == 0);

   s = gx_create_window(0, width, height, GRAPHICS_RESOURCE_RGBA32, &img);
   assert(s == 0);

   // transparent before display to avoid screen flash
   graphics_resource_fill(img, 0, 0, width, height, GRAPHICS_RGBA32(0,0,0,0x00));

   graphics_display_resource(img, 0, LAYER, 0, 0, GRAPHICS_RESOURCE_WIDTH, GRAPHICS_RESOURCE_HEIGHT, VC_DISPMAN_ROT0, 1);

   uint32_t text_size = 10;
   while (1) {
      const char *text = "The quick brown fox jumps over the lazy dog";
      uint32_t y_offset = height-60+text_size/2;
      graphics_resource_fill(img, 0, 0, width, height, GRAPHICS_RGBA32(0,0,0,0x00));
      // blue, at the top (y=40)
      graphics_resource_fill(img, 0, 40, width, 1, GRAPHICS_RGBA32(0,0,0xff,0xff));

      // green, at the bottom (y=height-40)
      graphics_resource_fill(img, 0, height-40, width, 1, GRAPHICS_RGBA32(0,0xff,0,0xff));

      // draw the subtitle text
      render_subtitle(img, text, 0, text_size,  y_offset);
      graphics_update_displayed_resource(img, 0, 0, 0, 0);
      text_size += 1;
      if (text_size > 50)
         text_size = 10;
   }

   graphics_display_resource(img, 0, LAYER, 0, 0, GRAPHICS_RESOURCE_WIDTH, GRAPHICS_RESOURCE_HEIGHT, VC_DISPMAN_ROT0, 0);
   graphics_delete_resource(img);

   return 0;
}

1- Что не так?
2- Почему эта ошибка на консоли, почему я не мог видеть во время на компиляторе?

ПРИМЕЧАНИЕ: Я могу запустить этот код на терминале (сначала запустите файл и используйте make на консоли после использования ./hello_font.bin), все работает нормально.

Спасибо, что уделили время.С наилучшими пожеланиями.

Ответы [ 2 ]

0 голосов
/ 21 февраля 2019

Как в комментариях @Scheff.Вы должны добавить макрос.Я добавил макрос NDEBUG (в настройках сборки моего проекта и добавил макрос во вкладку другого компилятора под опциями).Так что моя проблема решена.

0 голосов
/ 20 сентября 2018

1- Что не так?
2- Почему эта ошибка на консоли, почему я не мог видеть во время на компиляторе?

assert вы используете это утверждение во время выполнения.
Если вы хотите утверждение во время компиляции, вы должны использовать static_assert.
Использование static_assert позволяет вам распечатать свой собственныйсообщение, которое появится как ошибка компилятора.

Простой пример:

int main()
{
    static_assert(0 == 1, "Zero is not equal to one");
}

Выход компилятора:

main.cpp: In function 'int main()':
main.cpp:3:21: error: static assertion failed: Zero is not equal to one
     static_assert(0 == 1, "Zero is not equal to one");
...