cancel
Showing results for 
Search instead for 
Did you mean: 

getchar/putchar linker problem

johan239955_st
Associate II
Posted on January 16, 2009 at 11:05

getchar/putchar linker problem

10 REPLIES 10
johan239955_st
Associate II
Posted on May 17, 2011 at 12:56

Hello,

I have problems when trying to link my own getchar into my project.

I am using IAR EW, and putchar works without any problems.

The code is the following:

/////////////////////////////////////////

int putchar(int ch)

{

// My implementation to send a char over Uart

return ch;

}

int getchar(void)

{

int ch;

// My implementation to get a char over Uart

return ch;

}

/////////////////////////////////////////

Results from linker:

Error[Li006]: duplicate definitions for ''getchar''; in ''C:\dev\Embedded\SensoNor\SIMU202\Debug\Obj\Uart.o'', and ''getchar.o(dl7M_tl_if.a)''

How do I make the linker use my own implementation instead of the one given in ''getchar.o(dl7M_tl_if.a)''?

hervett
Associate II
Posted on May 17, 2011 at 12:56

Hi !

I think that you have an already definition of your getchar somewhere in your code, you define int getchar( ...) and maybe ''getchar'' exist in an other part of your code. Just try to remplace YOUR getchar by Getchartest or something like that and you will see

johan239955_st
Associate II
Posted on May 17, 2011 at 12:56

This is the only getchar in my application, but there is also one in the IAR library dl7M_tl_if.a. I have also a putchar in my application and in the library.

The linking problem only occurs if I call scanf.

If I call getchar somewhere inside my program, the linking works just fine.

Using printf also works fine...

Any ideas?

hervett
Associate II
Posted on May 17, 2011 at 12:56

If I was you, I would change all the same the name of your getchar and putchar. Remplacethem by mygetchar and myputchar or something like this. I'm sure it was that, because if there is already a getchar in the lbrary IAR don't understand and tell you that there is another,and I think IAR don't see the problem because when you don't call your printf or scanf, IAR don't compile your putchar or getchar if they are in the scanf or printfcode. So if you don't want problemes change names.

relaxe
Associate II
Posted on May 17, 2011 at 12:56

There is an example somewhere that solves that, but I don't remeber wich one... Could be in the RTC section...

In the meantime, you could download my DosFS over SDIO I've posted in the ''SDIO+FAT16/FAT32'' topic, where I have copy-pasted all the bits necessary for correct getchar/putchar/printf/scanf in IAR 5.20

Have fun :p

johan239955_st
Associate II
Posted on May 17, 2011 at 12:56

Replacing getchar with mygetchar makes things link, of course, but it doesn´t solve my problems. What I want to do is to provide low-lewel I/O functionality for printf/scanf.

In some way, printf manages to call my own putchar even though putchar is defined in the library. I have been searching without finding out why this works..

My reason for trying to make it work using the names putchar and getchar is to get ability to use printf and scanf together with my implemented putchar/getchar.

Is there something I miss? Is it possible to make printf/scanf to call myputchar and mygetchar as standard I/O instead?

Do you have any suggestions to make printf/scanf work with my own I/O implementation?

vinicius_acvasconcelos
Associate II
Posted on May 17, 2011 at 12:56

I have this problem to, I can`t solve it for now.

C:/Arquivos de programas/Rowley Associates Limited/CrossWorks for ARM 1.7/lib/libc_targetio_impl_v7m_t_le.a(printf.o): In function `printf':

printf.c:(.text+0x30): undefined reference to `__putchar'

Any other ideas?

johan239955_st
Associate II
Posted on May 17, 2011 at 12:56

I skipped using getchar()...

instead you can provide your own fgetc and fputc

To do this you have to link with file descriptors (if you are using IAR).

If you want to write your own putchar I think you have to switch some settings to the linker. I will reply when I have checked my settings.

[ This message was edited by: johan.stridkvist on 15-01-2009 23:12 ]

vinicius_acvasconcelos
Associate II
Posted on May 17, 2011 at 12:56

Try to inset in your code:

#include

int __putchar(int ch)

{

return debug_putchar(ch);

}

For me it solves the problem. For more information see this

http://www.rowley.co.uk/documentation/arm_1_7/hcc_customizing_putchar.htm

.