2025-12-08 5:54 PM - last edited on 2025-12-10 1:08 AM by Andrew Neil
When writing code, it is common to encounter the automatic inclusion of some unnecessary #include statements. For example, when I input "NULL" and press the Enter key, it will automatically add " #include <cstddef> ".
2025-12-09 4:59 PM
This is hard to follow. Where are you inputting NULL? Is this an autocomplete thing?
the IDE shows the file has been modified, but changing the file outside of user code blocks shouldn’t be done.
2025-12-09 5:54 PM
I entered "uint32_t *pi = NULL" in the user code section. When I pressed the Enter key while the code for handling the NULL input was being displayed, it automatically added "#include"
2025-12-10 1:10 AM
That seems a fairly reasonable thing to do: you've used NULL, so you will need a definition of it - it has just provided that definition for you.
What's the problem with that?
2025-12-10 1:22 AM
I think this is unreasonable. Firstly, if it is to be automatically added, it should be added to the user code area instead of the current location (because this would be overwritten when generating the code using STM32CubeMX). Secondly, the automatically added "#include <cstddef>" is also incorrect and cannot be compiled. It should be "#include <stddef.h>".
2025-12-10 1:34 AM
OK: so it's not the fact that it's added which is the problem; it's the fact that:
Yes?
in that case, I agree with you there.
2025-12-10 1:50 AM
My idea is to avoid automatically adding unnecessary #include statements, or it is not intelligent enough. For example, when I input the "osMessageQueuePut" function, it automatically adds #include "cmsis_os2.h", which is unnecessary because there is already #include "cmsis_os.h" in the current code file, and its internal part contains #include "cmsis_os2.h". Of course, if this cannot be solved, it would also be acceptable to address the issues of adding incorrect #include statements and placing them in the wrong location.
2025-12-10 2:04 AM
@t_j_l wrote:avoid automatically adding unnecessary #include statements,
But, in the example you gave, it was not unnecessary!
@t_j_l wrote:it automatically adds #include "cmsis_os2.h", which is unnecessary because there is already #include "cmsis_os.h" in the current code file
OK, so that's another fault with the implementation - but the basic idea remains useful.
Of course, having multiple #includes of the same header is not harmful - we have Include Guards to take care of that.
I've not used VSCode widely - is this just an ST thing, or is it generic VSCode behaviour ?
2025-12-10 3:54 AM
Yes, in my example, the "#include" is indeed necessary. I did this merely to clarify the specific situation.
According to my test, this issue is currently only occurring with the ST plugin.
Finally, I hope that the automatically added "#include" statements can be placed in the user code section, and that the correct "#include" code can be generated.