A
A
Alexander Afanasiev2017-10-05 12:01:25
Arduino
Alexander Afanasiev, 2017-10-05 12:01:25

Why does not see the function from the library in Arduino?

Created a simple library for arduino:
Test_lib.h

#ifndef Test_lib_h
#define Test_lib_h
extern void Idle(void);
#endif

Test_lib.cpp
#include "Test_lib.h"
inline void  Idle(void){}

In the project I include it:
#include <Test_lib.h>
void setup() 
{
  Idle();
}

void loop() {}

The compiler throws an error:
undefined reference to `Idle()'
But if you remove "inline" from the library, everything works. Why is this happening and what to do? the function will be used in an interrupt, so inline is highly desirable.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2017-10-05
@Mercury13

Remember once and for all! Inline does not create code, it creates the fact that it is being used somewhere.
On the one hand, the inline function must be defined each time it is used. On the other hand, it will not get into the object file by itself.
Therefore, inline is kept in headers (except for tricky cases like private inline, when it is syntactically impossible to call this function from anywhere, and it is better not to clog the header).
When you get to C++ templates, they have the same properties. The template does not create code, it creates an untemplate. And also in headers, except for tricky cases like a private template or a template that has exactly N predefined specializations and (N + 1) is not needed.
But the full specialization of a template function of type template<> (in angle brackets is empty, not inline) also creates a place for it in CPP.

K
kacejot, 2017-10-06
@kacejot

Your program is trying to look up the definition for
in the compiled library file. Since the function with the inline modifier is defined in the source file, then, during compilation, for each place where this function was called, its body will be substituted, but (!) Within the boundaries of only this object file, which is assembled from this source file. There is no function in the object file itself (it only serves as a way to instantiate code, without pushing up the call stack), so we can't take its address so that other object files can import it. There are only processor instructions that were substituted for each call to this function.
If we move the implementation into a header file (which comes with our library), then any calling code that includes this header file will have access to the implementation. Therefore, everything worked for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question