On 30/04/2021 09:03, David Brown wrote:
> On 29/04/2021 21:43, Ian Molton wrote:
>> Hi,
>> I'm attempting to build my project, which does not use libc, or libm,
>> but I do want to link with libgcc.
>> additionally, I don't want any startup code to be linked - the project
>> is intended to be fully standalone.
> If you don't use any functions from these libraries, nothing from them
> will be linked into your project - it doesn't matter if the linker has them.

It matters a lot is the user does not have avr-libc installed, as the
linker will fail, despite not requiring functions from those libraries.

I also don't want users to inadvertently link with libraries outside the

> (I can't remember the details for the avr, but often "libm" is actually
> empty, and exists only for compatibility - all the maths functions are
> frequently included in the main libc.)

I believe in libgcc, yes. I still wish to link with libgcc, as the
functiosn therein are tied to the compiler being used.

> Sometimes (again, this is from other targets rather than the avr, which
> I have not used for many years) it is useful to provide stubs of
> functions like "exit()" and "atexit()" - just add these as extra empty
> functions in your code.  That will ensure the library versions are not
> used, and they do not pull in any other bits and pieces.

I've found libgcc doesnt appear to contain mem{set,copy,etc.}, but thats
OK, I can place implementations in my project.

What I need, if for avr-gcc to NOT attempt to link with the crt, libc,
or libm.

I've found that when compiling my objects, I can avoid pollution from
outside the compiler with:

avr-gcc -nostdinc -isystem include/

which covers pollution from headers, and when linking, I can use:

avr-gcc -nostdlib -mmcu=$(CPU_MODEL) -T./$(CPU_MODEL).lds
        -Wl,--gc-sections -mrelax $(OBJS)

which results in gcc calling the linker with something like (gcc -v output):

collect2 -plugin liblto_plugin.so -plugin-opt=lto-wrapper
 -mavr6 -Tdata 0x800200
 --relax -o output.elf
 --gc-sections objects.o more_objects.o -T ./atmega2560.lds

Which almost results in what I want, but not quite. It *actually*
results in a failed link, since --nostdlib has prevented linking with
-lgcc -lc and -lm, as well as the crt. This is correct as documented.

What it *looks like* I want to do is add -lgcc to the commanline, which
it actually says to do in the gcc manual, but it makes no difference to
the parameters passed to collect2.


Its working. The problem was that adding -lgcc to the link command has
to be done *after* the objects.

Heres the "before":
$(CC) -v $(LINK_OPTS) -lgcc -o $@ $(OBJS)

And the (working) after:
$(CC) -v $(LINK_OPTS) -o $@ $(OBJS) -lgcc

Thanks for being rubber duckies, folks.

Posting this in case anyone else trips over it. order *matters*.

Plus I can now avoid the (apparently unsupported in 5.4) -nolibc option.


Reply via email to