Page 1 of 1

ChibiOS linking problem for printf

Posted: Thu Oct 29, 2020 10:50 pm
by Rakibul007
I'm using a very simple program to print something using UART2. Although the code is compiling, it is throwing me the following error. I'm using STM32F410RB Nucleo 64. I'm a total newbie in ChibiOS.


Code: Select all

#include "hal.h"
#include "ch.h"
#include <stdio.h>


#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
{
  sdWrite(&SD2, (uint8_t *)&ch, 0xFFFF);
  return ch;
}

/* Serial configuration. */
static const SerialConfig myserialcfg = {
  115200,
  0,
  USART_CR2_STOP1_BITS,
  0
};

int main(void)
{
  halInit();
  chSysInit();
  sdStart(&SD2, &myserialcfg);
  int a = 1;

  while(1){
    printf("Hello - %d\n",a);
    a++;
    chThdSleepMilliseconds(1000);
  }
}



Code: Select all

Linking build/ch.elf
c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-sbrkr.o): in function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'
c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-writer.o): in function `_write_r':
writer.c:(.text._write_r+0x14): undefined reference to `_write'
c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-closer.o): in function `_close_r':
closer.c:(.text._close_r+0xc): undefined reference to `_close'
c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-fstatr.o): in function `_fstat_r':
fstatr.c:(.text._fstat_r+0x12): undefined reference to `_fstat'
c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-isattyr.o): in function `_isatty_r':
isattyr.c:(.text._isatty_r+0xc): undefined reference to `_isatty'
c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-lseekr.o): in function `_lseek_r':
lseekr.c:(.text._lseek_r+0x14): undefined reference to `_lseek'
c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/chibistudio/tools/gnu tools arm embedded/9.0 2019q4/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m/nofp\libg.a(lib_a-readr.o): in function `_read_r':
readr.c:(.text._read_r+0x14): undefined reference to `_read'
collect2.exe: error: ld returned 1 exit status
make: *** [../../chibios203/os/common/startup/ARMCMx/compilers/GCC/mk/rules.mk:217: build/ch.elf] Error 1
"make -j24 all" terminated with exit code 2. Build might be incomplete.

Re: ChibiOS linking problem for printf

Posted: Fri Oct 30, 2020 8:53 am
by Giovanni
Hi,

There is a "syscalls.c" module to be included in makefile if you want to use the standard library, it is located under /os/various.

Giovanni

Re: ChibiOS linking problem for printf

Posted: Fri Oct 30, 2020 4:07 pm
by Rakibul007
Thanks Giovanni. The code now compiles and creates the .elf file. However, it doesn't print anything in the Serial. I know about chprintf. So, I tried the following code -

Code: Select all

#include "hal.h"
#include "ch.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <chprintf.h>

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

PUTCHAR_PROTOTYPE
{
  sdWrite(&SD2, (uint8_t *)&ch, 0xFFFF);
  return ch;
}

static BaseSequentialStream* chp = (BaseSequentialStream*) &SD2;

static const SerialConfig myserialcfg = {
  115200,
  0,
  USART_CR2_STOP1_BITS,
  0
};

int main(void) {
  halInit();
  chSysInit();

  sdStart(&SD2, &myserialcfg);
  int a = 1;

  while(1){
    printf("Hello from printf- %d\n",a);
    a++;
    chprintf(chp,"Hello from chprintf - %d\n",a);
    chThdSleepMilliseconds(1000);
  }
}


I get the result from chprintf (just to make sure that my Serial Driver is working), but nothing from the printf function. Do you have any idea what might cause this problem?

Re: ChibiOS linking problem for printf

Posted: Fri Oct 30, 2020 4:52 pm
by Giovanni
No, you need to place a breakpoint in syscalls.c and see if printf() gets in there and data is written as expected.

Anyway, chprintf() is there exactly because the library is problematic to get to work and eats a lot of code space.

Giovanni

Re: ChibiOS linking problem for printf

Posted: Fri Oct 30, 2020 5:25 pm
by Rakibul007
Sounds good. Thanks Giovanni for your support.