inline function in c
Asked Answered
P

4

10

Can inline function be declared in .h and defined once in .c?

Polyzoic answered 19/4, 2010 at 20:59 Comment(0)
H
14

inline function definitions need to be visible wherever the function is invoked. You can define the function in a .c file if you only want to use it within that .c file, otherwise you need to define it in a .h file so that the definition can be #included wherever it is needed.

Hy answered 19/4, 2010 at 21:3 Comment(0)
G
3

Yes. It can get messy though here is a good read: http://www.greenend.org.uk/rjk/2003/03/inline.html

Galatians answered 19/4, 2010 at 21:4 Comment(0)
S
1

You can make a function static inline and include it on a header file:

square.h:

#ifndef SQUARE_H
#define SQUARE_H

static inline int square(int num) {
    return num * num;
}

#endif // SQUARE_H
Skilken answered 14/4, 2020 at 15:9 Comment(0)
G
0

Yes you can do that.

Galagalactagogue answered 19/4, 2010 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.