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.
Multiple definition of func
Re: Multiple definition of func
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.
osdev project, goal is to run wasm as userspace: https://github.com/kwast-os/kwast
Re: Multiple definition of func
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
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
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 ?
One last question : is it better to static inline them or to move them in their own .c file ?
- Schol-R-LEA
- Member

- Posts: 1925
- Joined: Fri Oct 27, 2006 9:42 am
- Location: Athens, GA, USA
Re: Multiple definition of func
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.
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.)
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.R3DC0DE wrote:One last question : is it better to static inline them or to move them in their own .c file ?
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.)
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.

