1

I have a question about header guard and inline.

I know that if you want to definition of function in header file you must use inline pre-descriptor.Because when you use inline function, this function will be copy by preprocessor for compiler whenever it there.

So, we can say inline remove multiple definition with this way.

Also, header guards can remove multiple definition.

So my main question, if I use header guards on header file, Do I need use inline for remove multiple definition? What is real difference between?

Lundin
  • 195,001
  • 40
  • 254
  • 396
KaruF
  • 47
  • 5
  • 2
    Header guards do not prevent multiple definitions because the same header with a function definition can be included in several translation units. – Vlad from Moscow Dec 03 '20 at 14:22
  • If it is `static inline` then it will get inlined per translation unit that includes the header, with no name collisions. – Lundin Dec 03 '20 at 14:24
  • So what is purpose of header guards? – KaruF Dec 03 '20 at 14:29
  • purpose of header guards: to not define things twice. – chux - Reinstate Monica Dec 03 '20 at 14:30
  • The same header file may get included more than once by the same translation unit, but some things such as the members of struct and union types, or the body of an inline function can only be defined once per translation unit. The header guards protect against multiple definitions. – Ian Abbott Dec 03 '20 at 14:47
  • @IanAbbott So, can we say both inline and header guards can remove multiple definition? and if we use header guards on every file do we need inline command? – KaruF Dec 03 '20 at 15:42
  • @KaruF `inline` really has nothing to do with preventing multiple definition. `static` turns an externally linked definition into an internally linked definition. – Ian Abbott Dec 03 '20 at 15:51
  • @IanAbbott pls refer to this question answers.https://stackoverflow.com/questions/10242357/why-cant-i-define-plain-c-functions-in-header-file#:~:text=If%20you%20define%20the%20function,c%20or%20.&text=That%20means%20that%20it's%20valid,legitimately)%20contain%20the%20same%20symbol. You will see when ı want to definition of function on header file and include this header file into some source file i will get linker error for multiple definition.Than if ı use inline with this function ı do not get multiple definition error.. – KaruF Dec 03 '20 at 16:24
  • 1
    @KaruF Try including the header file twice in the same .c file. You will get multiple definition errors for the inline functions in the header file unless you use header guards. (You would not normally include a header file twice in the same .c file, but it can happen in real life due to header files including other header files.) – Ian Abbott Dec 03 '20 at 16:34
  • @IanAbbott ı have tried some example about this and ı get it this. Header guards can remove redefinition same translation unit.But it can't be remove redefinition different translation unit. If you have definition on your header file, you should header guards and inline command for any multiple definition error. – KaruF Dec 04 '20 at 06:51

0 Answers0