__attribute__((used)) not working

Hello everyone. I compiled a project in platformIO. I asked the compiler to generate a “test.map” file. I found that some of my things were discarded by the compiler, but I want to keep it. How do I do it in platformIO?

I added “attribute((used))” but it doesn’t seem to work

I want to keep “rti_fn.1”


Sure it’s not


? Also per https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/sections.html and https://www.avrfreaks.net/comment/867028#comment-867028 the linker may still throw it away if it’s not in KEEP() section.

You might also try to disable garbage collection by adding

build_unflags = -gc-sections

or maybe -Wl, -gc-sections, not sure exactly.

Thank you for your answer

I found the “keil” version of my project. This is to compile this project with “keil”, I would like to ask if the “gcc” compiler also has these instructions.

I found that adding the “build_unflags = -gc-sections” option seems to have no effect.

GCC is a bit difficult for me.:no_mouth::no_mouth:


I feel that the problem is not as simple as I thought.,I feel I have to take time to understand this library. Thank you for your answer

What part of memory do you want to place rti_fn in?
Another option you can try to add the next line to your build_flags:

build_flags = -Wl,--undefined=your_symbol

Another option would be delete flags -fdata-sections, -ffunction-sections at least for src folder so that linker wouldn’t be able to discard the entire section.

Thank you for your answer, the problem is solved. The PlatFormIO team is great

Would be great if you describe your solution here.

1 Like

Mr. maxgerhardt said “attribute (( used))” has no problem. So I think there is a problem with the compile option. The RT-Thread real-time operating system has a GCC build environment. I started to view its Python script, found the relevant compile options, and then wrote “platformio.ini”. The compilation works fine, but the system still doesn’t work.:no_mouth::no_mouth:

I am sorry, I don’t want to show you a wrong solution.:no_mouth::no_mouth:

The “RT-Thread” real-time operating system is similar to “UNIX.” I originally thought that I only need to put its source code file into “platformIO” to compile it. It seems that my thoughts are wrong now.:no_mouth::no_mouth:

I found a “RT-Thread” library based on the Arduino framework in “platformIO”. I don’t know how he did it. I will refer to his source code tomorrow.

Hi, I have same problem like @tuzik, I also use RT-thread RTOS.

I have fixed the problem by update Keil MDK from v5.25-> v5.29. Hope this may help you. And I guess the problem is caused by the Arm Clang complier which is Integrated in Keil MDK.