OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 6:09 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Multiple definition of func
PostPosted: Fri Jan 10, 2020 5:46 am 
Offline

Joined: Sat May 04, 2019 4:31 pm
Posts: 12
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.


Top
 Profile  
 
 Post subject: Re: Multiple definition of func
PostPosted: Fri Jan 10, 2020 5:53 am 
Offline
Member
Member
User avatar

Joined: Sun Apr 05, 2015 3:15 pm
Posts: 31
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


Top
 Profile  
 
 Post subject: Re: Multiple definition of func
PostPosted: Fri Jan 10, 2020 6:13 am 
Offline

Joined: Sat May 04, 2019 4:31 pm
Posts: 12
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.


Top
 Profile  
 
 Post subject: Re: Multiple definition of func
PostPosted: Fri Jan 10, 2020 6:18 am 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
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.

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: Multiple definition of func
PostPosted: Fri Jan 10, 2020 6:24 am 
Offline

Joined: Sat May 04, 2019 4:31 pm
Posts: 12
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 ?


Top
 Profile  
 
 Post subject: Re: Multiple definition of func
PostPosted: Fri Jan 10, 2020 11:32 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
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.)

_________________
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 71 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group