Multiple definition of free and malloc

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple definition of free and malloc

Stephan Uhle
Hi everybody,

when I try to link my ethernut program I get a linker error that malloc
and free are defined twice(in libc.a and libnutcrt.a).

I'm linking my program this way:

avr-gcc -mmcu=atmega128 -Wl,--defsym=main=0,-Map=./obj/myprog.map,--cref
-L~/nut/lib ./obj/*.o ~/nut/lib/nutinit.o -lnutos -lnutcrt -lnutdev
-lnutfs -lnutnet -lnutos -lnutpro -lnutcpp -o obj/myprog.elf

I already tried to change the order of the -l arguments and I also tried
to use -nostdlib and -nodefaultlibs but that produced a lot of undefined
definitions.

thanks for your help,

stephan
--

_______________________________________________
En-Nut-Discussion mailing list
[hidden email]
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Multiple definition of free and malloc

Harald Kipp
Hi Stephan,

avr-gcc uses a one-pass linker. In some situations you have to
add a lib twice. So, simply add -lnutcrt once more at the end
of the list.

Beside that, I tried to replace NutHeapAlloc/Free at several
places in the kernel to make it look more familiar. Actually
this wasn't a good idea. Now even app/simple needs to be linked
to libnutcrt.a. I'll revert these changes. That doesn't mean,
it should be replaced everywhere. For example, most modules
in pro/ use libnutcrt.a anyway (printf etc.) and should prefer
the standard functions (malloc/calloc/free).

Harald

At 11:58 13.06.2005 +0200, you wrote:

>Hi everybody,
>
>when I try to link my ethernut program I get a linker error that malloc
>and free are defined twice(in libc.a and libnutcrt.a).
>
>I'm linking my program this way:
>
>avr-gcc -mmcu=atmega128 -Wl,--defsym=main=0,-Map=./obj/myprog.map,--cref
>-L~/nut/lib ./obj/*.o ~/nut/lib/nutinit.o -lnutos -lnutcrt -lnutdev
>-lnutfs -lnutnet -lnutos -lnutpro -lnutcpp -o obj/myprog.elf
>
>I already tried to change the order of the -l arguments and I also tried
>to use -nostdlib and -nodefaultlibs but that produced a lot of undefined
>definitions.
>
>thanks for your help,
>
>stephan
>--
>
>_______________________________________________
>En-Nut-Discussion mailing list
>[hidden email]
>http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion

_______________________________________________
En-Nut-Discussion mailing list
[hidden email]
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Multiple definition of free and malloc

Stephan Uhle
In reply to this post by Stephan Uhle
Hi Harald,

thanks for the immediate answer, unfortunately this doesn't work either.
Maybe I should have mentioned in my previous message that I got two
additional warnings, saying that the sizes of the symbols free and malloc have
changed, but I think this belongs to the multiple definition errors.

Here is the complete error message I get:

~/avr/gcc/bin/../lib/gcc-lib/avr/3.3/../../../../avr/lib/avr5/libc.a(malloc.o)(.text+0x0):
In function `malloc':
~/avr/avr-libc-1.2.3/avr5/obj-avr/libc/stdlib/../../../../libc/stdlib/malloc.c:67: multiple definition of `malloc'
/cc/rbc/OMSDevelopment/TCB/stephan/nut/lib/libnutcrt.a(malloc.o)(.text+0x0): first defined here

~/avr/gcc/bin/../lib/gcc-lib/avr/3.3/../../../../avr/bin/ld: Warning: size of symbol `malloc' changed from 6 in
~/nut/lib/libnutcrt.a(malloc.o) to 336 in ~/nut/lib/libnutcrt.a(malloc.o)

~/avr/gcc/bin/../lib/gcc-lib/avr/3.3/../../../../avr/lib/avr5/libc.a(malloc.o)(.text+0x150):
In function `free':
~/avr/avr-libc-1.2.3/avr5/obj-avr/libc/stdlib/../../../../libc/stdlib/malloc.c:190: multiple definition of `free'
/cc/rbc/OMSDevelopment/TCB/stephan/nut/lib/libnutcrt.a(malloc.o)(.text+0x6): first defined here

~/avr/gcc/bin/../lib/gcc-lib/avr/3.3/../../../../avr/bin/ld: Warning: size of symbol `free' changed from 6 in
~/nut/lib/libnutcrt.a(malloc.o) to 198 in ~/nut/lib/libnutcrt.a(malloc.o)
*** Error code 1


the best solution I've got so far is:

avr-gcc -mmcu=atmega128 -Wl,--defsym=main=0,-Map=./obj/myprog.map,--cref -L~/nut/lib -nostdlib obj/*.o \
~/avr/gcc/avr/lib/avr5/crtm128.o ~/nut/lib/nutinit.o -lc -lgcc -lnutos -lnutcrt -lnutdev -lnutfs -lnutnet \
-lnutos -lnutpro -lnutcpp -lnutcrt -lc -o obj/myprog.elf

but I still get a "symbol size changed" message of `__iob' within
libc.a(fdevopen.o) and libnutcrt.a(fopen.o) and due to this (I suppose so)
printf doesn't print out anything. :o/

I guess I have to go on and play around with the link order of the libraries.

thanks,

Stephan

At 12:22 13.06.2005 +0200, you wrote:

>Hi Stephan,
>
>avr-gcc uses a one-pass linker. In some situations you have to
>add a lib twice. So, simply add -lnutcrt once more at the end
>of the list.
>
>Beside that, I tried to replace NutHeapAlloc/Free at several
>places in the kernel to make it look more familiar. Actually
>this wasn't a good idea. Now even app/simple needs to be linked
>to libnutcrt.a. I'll revert these changes. That doesn't mean,
>it should be replaced everywhere. For example, most modules
>in pro/ use libnutcrt.a anyway (printf etc.) and should prefer
>the standard functions (malloc/calloc/free).
>
>Harald
--

_______________________________________________
En-Nut-Discussion mailing list
[hidden email]
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Multiple definition of free and malloc

Harald Kipp
Hello Stephan,

All Nut/OS libs must come first. I do not think you'd need to
specify -lc -lgcc. But if, put it at the end only. And do you
really need -lnutcpp? I don't think so.

How about
~/nut/lib/nutinit.o  -lnutnet -lnutpro -lnutfs -lnutos -lnutdev \
-lnutnet -lnutcrt -o obj/myprog.o

Any reason why you don't use the same Makefiles as the sample
applications?

Harald

At 09:48 14.06.2005 +0200, you wrote:

>avr-gcc -mmcu=atmega128 -Wl,--defsym=main=0,-Map=./obj/myprog.map,--cref
>-L~/nut/lib -nostdlib obj/*.o \
>~/avr/gcc/avr/lib/avr5/crtm128.o ~/nut/lib/nutinit.o -lc -lgcc -lnutos
>-lnutcrt -lnutdev -lnutfs -lnutnet \
>-lnutos -lnutpro -lnutcpp -lnutcrt -lc -o obj/myprog.elf

_______________________________________________
En-Nut-Discussion mailing list
[hidden email]
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Multiple definition of free and malloc

Stephan Uhle
Hi Harald,

I still get the multiple definition error. I've started adding the
object-files one by one to the link command and found out that this
error occurs when I'm linking a object file that uses realloc.

Mustn't I use realloc?


Btw, I need -lnutcpp because I'm using C++.

stephan

On Tue, 2005-06-14 at 10:34, Harald Kipp wrote:

> Hello Stephan,
>
> All Nut/OS libs must come first. I do not think you'd need to
> specify -lc -lgcc. But if, put it at the end only. And do you
> really need -lnutcpp? I don't think so.
>
> How about
> ~/nut/lib/nutinit.o  -lnutnet -lnutpro -lnutfs -lnutos -lnutdev \
> -lnutnet -lnutcrt -o obj/myprog.o
>
> Any reason why you don't use the same Makefiles as the sample
> applications?
>
> Harald



--

_______________________________________________
En-Nut-Discussion mailing list
[hidden email]
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Multiple definition of free and malloc

Harald Kipp
I see. No, realloc is not yet available with Nut/OS.
If it's used, realloc from avr-libc is linked in. Possibly
they use the malloc in the same linker module and bang,
we have multiple mallocs.

For now, you can write your own realloc.

Be aware, that Nut/OS C++ support is limited. Not many
people use it and it may introduce problems. I'm also
not sure, if all function prototypes are C declared.

Harald

At 16:18 14.06.2005 +0200, you wrote:

>Hi Harald,
>
>I still get the multiple definition error. I've started adding the
>object-files one by one to the link command and found out that this
>error occurs when I'm linking a object file that uses realloc.
>
>Mustn't I use realloc?
>
>
>Btw, I need -lnutcpp because I'm using C++.
>
>stephan
>
>On Tue, 2005-06-14 at 10:34, Harald Kipp wrote:
> > Hello Stephan,
> >
> > All Nut/OS libs must come first. I do not think you'd need to
> > specify -lc -lgcc. But if, put it at the end only. And do you
> > really need -lnutcpp? I don't think so.
> >
> > How about
> > ~/nut/lib/nutinit.o  -lnutnet -lnutpro -lnutfs -lnutos -lnutdev \
> > -lnutnet -lnutcrt -o obj/myprog.o
> >
> > Any reason why you don't use the same Makefiles as the sample
> > applications?
> >
> > Harald

_______________________________________________
En-Nut-Discussion mailing list
[hidden email]
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Multiple definition of free and malloc

Stephan Uhle
Thanks for the help, writing my own realloc solved the problem.
I still can't send data via serial port with printf, but that shall
be the topic of a different thread! ;o)

Stephan

On Tue, 2005-06-14 at 16:31, Harald Kipp wrote:

> I see. No, realloc is not yet available with Nut/OS.
> If it's used, realloc from avr-libc is linked in. Possibly
> they use the malloc in the same linker module and bang,
> we have multiple mallocs.
>
> For now, you can write your own realloc.
>
> Be aware, that Nut/OS C++ support is limited. Not many
> people use it and it may introduce problems. I'm also
> not sure, if all function prototypes are C declared.
>
> Harald

_______________________________________________
En-Nut-Discussion mailing list
[hidden email]
http://www.egnite.de/mailman/listinfo.cgi/en-nut-discussion