0

I am facing a problem when using the inline keyword.

In header file:

void checkContinuation() __attribute__((always_inline));

In the source file:

void __attribute__((always_inline)) checkContinuation() { ..... }

In the main file I called it, but I got a compilation error in which said

undefined reference to checkContinuation()

I read all these link inline with external linkage & inline function linkage

And what I've understood that the inline function in C++ has external linkage which means that only one definition for the function must be available but others said that It must be definied in each translation unit in which it is used.

So what is the correct answer and how can I use define it once but user it a lot?

Also, What is the correct syntax for the inline function attribute in both header and source file? and what is the difference between it and __always_inline?

Community
  • 1
  • 1
Mahmoud Emam
  • 1,499
  • 4
  • 20
  • 37

2 Answers2

1

And what I've understood that the inline function in C++ has external linkage which means that only one definition for the function must be available but others said that It must be definied in each translation unit in which it is used.

The inline function can be defined in several translation units, if all the definitions are identical.

The usual way to do that is to have the definition in a header file, and include that everywhere the function is needed.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

The C compiler can't inline a function unless it sees it in the same compilation unit, which means you define it in the C file itself or in a header file that is #included (in which case you usually use inline static to make sure it doesn't cause name conflicts in multiple compilation units). This is because the C compiler can't look into any other compilation units besides the one it is compiling.

The linker can inline functions if whole-program optimization is enabled, but not all C linker packages support that.

Jason S
  • 184,598
  • 164
  • 608
  • 970