Skip to main content
DK.7
Associate III
March 28, 2022
Solved

How to fix "multiple definition of `errno"'?

  • March 28, 2022
  • 6 replies
  • 13488 views

I try to create a new project, nothing is written in main.c! It is a first check after setting up 'LWIP', 'SDIO' and Freertos in the GUI and got this mistake!

multiple definition of `errno'; ./Middlewares/Third_Party/LwIP/system/OS/sys_arch.o:/home/denis/STM32CubeIDE/workspace_1.9.0/FreeRtos_SDIO_Web/Debug/../Middlewares/Third_Party/LwIP/system/OS/sys_arch.c:45: first defined here

STM32CubeIDE Version: 1.9.0

PS - This error not appear then I was on STM32CubeIDE Version: 1.8.0

Best answer by CKok.1

0693W00000LxZFrQAN.png

6 replies

Tesla DeLorean
Guru
March 28, 2022

>>How to fix "multiple definition of `errno"'?

Remove all but one? Make some weak?

Make sure multiple instances of the same object aren't pulled in

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
DK.7
DK.7Author
Associate III
March 28, 2022

The file "sys_arch.o" is not readable.

And if I will comment three lines in the file sys_arch.c is it will correct?

I'm asking because according to the error /Middlewares/Third_Party/LwIP/system/OS/sys_arch.c:45: first defined here0693W00000Lw2S7QAJ.png0693W00000Lw2RnQAJ.png

Ken CK
Associate II
December 4, 2022

You can just add extern before the int errno.

waclawek.jan
Super User
March 28, 2022
DK.7
DK.7Author
Associate III
March 28, 2022

Thanks for the link! But, in my case, I have nothing in main.c and there is nothing in headers files and so on. It is a first check after setting up in GUI what I do wrong or how it is fix?

Pavel A.
Super User
March 28, 2022

If you're using the ST toolchain, errno is defined by its C library (newlib).

So just #include <errno.h> ; remove all other definitions of errno,

waclawek.jan
Super User
March 28, 2022

From the thread I linked to:

> ... use -fcommon to compile ...

JW

DK.7
DK.7Author
Associate III
March 28, 2022

Could you please make screen? Because I don't understand what you are mean...

CKok.1
CKok.1Best answer
Associate
April 11, 2022

0693W00000LxZFrQAN.png

Pavel A.
Super User
March 28, 2022

"extern" is not good at all. The C library can be thread-enabled and define its errno like this:

struct thread_context_s {
 ......
 int m_errno;
};
extern thread_context_s * thread_ctx_ptr;
#define errno (thread_ctx_ptr->m_errno)

DK.7
DK.7Author
Associate III
March 29, 2022

Thank you for the explanation but, I am not from those who can change rules in the standard libraries of 'C'.

How do I need to do it right according your mention?

Below on the screenshots, I showed the current state.0693W00000Lw5ApQAJ.png0693W00000Lw5AaQAJ.png

Pavel A.
Super User
March 29, 2022

> How do I need to do it right according your mention?

Just follow the "single definition" rule: #include <errno.h> everywhere, remove any other definitions of errno.

LwIP is not a "standard library", it is a add-on component. It is not in position to override errno for the rest of application.

heveskar
Senior
April 5, 2022

Don't you think it is a bug in LwIP configuration settings in the Cube? I think we should be able to remove the define LWIP_PROVIDE_ERRNO and set LWIP_ERRNO_STDINCLUDE or define LWIP_ERRNO_INCLUDE to <sys/errno.h>. Those settings could be in Key Options of the LwIP stack.

I noticed that this fixes the issue, in cc.h:

//#define LWIP_PROVIDE_ERRNO
#define LWIP_ERRNO_INCLUDE <sys/errno.h>

But this is lost with every code re-generation which is not very good, is it.

@Community member​ 's solution of building with -fcommon also works for me.

Karel

Pavel A.
Super User
April 5, 2022

> #define LWIP_ERRNO_INCLUDE <sys/errno.h>

Just <errno.h> , not <sys/errno.h> or something else.

 > But this is lost with every code re-generation which is not very good, is it.

File cc.h is part of libraries (LwIP) that get copied over to generated source tree.

If you choose to create references to the libraries instead of copying every time, your changes in cc.h or other library files should stay.

Unfortunately the life is not ideal.

heveskar
Senior
April 5, 2022

<errno.h> is not working for me as it probably uses lwip/errno.h. Only <sys/errno.h> solved the compilation issue, that's why I wrote it. Maybe you are right that it is not clean, probably the compilation flag method is better.

As for the second part, I did not know that library files can be referenced and not copied, thank you for that.

Life is not ideal especially if one has to use the ST products that seem to be always full of bugs, it just never ends.

I think that my point still stands, in cube there should be setting which errno to use. Or just do it somehow that the project can be configured from cube and built properly, without editing library files.

Visitor II
July 24, 2025

Try this solution:

LWIP\Target\lwipopts.h

#undef LWIP_PROVIDE_ERRNO
#define LWIP_ERRNO_STDINCLUDE