Page 1 of 1
Multiple definition of func
Posted: Fri Jan 10, 2020 5:46 am
by R3DC0DE
Hello,
I have a compiling error : multiple definition of func.
Those function are port_byte_in, port_byte_out, port_word_in and port_word_out. They are declare inside drivers/low_level.h.
The errors are raised whenever low_level.h is included : in drivers/io.c, drivers/screen.c and cpu/idt.c.
I have trouble understanding why this is happening. I thought at first that it might come from the linker file, but it doesn't looks like it. So I checked the header guards and they are fine. I tried to move the inclusion in the headers file but it didn't help either.
The last thing I tried was to inline the functions but it resulted with the same bug when I finally made the inline work with an 'external inline'.
git :
https://github.com/MrScriptX/OS/tree/alpha_interrupts
Thanks in advance for the help.
Re: Multiple definition of func
Posted: Fri Jan 10, 2020 5:53 am
by nielsd
Include in C means that the contents of the included file are copied into the current file. Your implementation is inside your header, so when you are including, the implementations are copied into each file where you do the include. The linker will see multiple definitions of the same function.
Re: Multiple definition of func
Posted: Fri Jan 10, 2020 6:13 am
by R3DC0DE
But header guards shouldn't prevent the fact that it is included multiple times? Or those it protect from multiples inclusions only for one file the current specific file.
Re: Multiple definition of func
Posted: Fri Jan 10, 2020 6:18 am
by klange
Presumably, the errors you are seeing are at link time. You are likely including that header from multiple sources that get built into object files and then linked together, at which point they all have their own definitions of the functions. If you want to use inline, use static inline.
Re: Multiple definition of func
Posted: Fri Jan 10, 2020 6:24 am
by R3DC0DE
I see. So the thing is, the linker makes the individual .o files which contains their own definition of the functions. Then the linker puts them together which cause the error.
One last question : is it better to static inline them or to move them in their own .c file ?
Re: Multiple definition of func
Posted: Fri Jan 10, 2020 11:32 am
by Schol-R-LEA
Shameless Self-Promotion dept.: You might want to read the wiki page
Why function implementations shouldn't be put In header files for a fuller discussion on this topic.
R3DC0DE wrote:One last question : is it better to static inline them or to move them in their own .c file ?
For inlining, it really depends on the function; mostly, how large they are, how often they are called and where, and whether there is a reason to avoid having a function call/return pair and the necessary preamble/exit code which goes with a fully-fledged function.
Looking at the specific functions you mentioned, I would have the
port_* functions as
static inline, since the goal is to insert a single inline assembly instruction into the code referring to them (modulo some automagically generated register shuffling, perhaps).
However, these are precisely the sort of use case
static inline is intended for. In general, you don't want to inline a function which doesn't need to be inlined.
(Also, as I said to someone else
in another thread recently, it is worth bearing in mind that
inline functions and inline assembly are two separate things, even if they sometimes go together.)