Error when using inline methods in C ++ classes: undefined reference to

0

I'm optimizing part of my code where some methods that are looped are too often passing them to inline , but doing so I'm getting the following error messages:

And I'm getting the following error:

In file included from CycloTracker.hpp:15:0,
                 from CycloTracker.cpp:3:
ObjectTracker.hpp:45:14: warning: inline function ‘void ObjectTracker::PrintLeftPartial(cv::Mat&)’ used but never defined
  inline void PrintLeftPartial(cv::Mat &frame);
              ^
ObjectTracker.hpp:46:14: warning: inline function ‘void ObjectTracker::PrintRightPartial(cv::Mat&)’ used but never defined
  inline void PrintRightPartial(cv::Mat &frame);
              ^
ObjectTracker.hpp:43:14: warning: inline function ‘void ObjectTracker::PrintLeftPartial(cv::Mat&, cv::Point)’ used but never defined
  inline void PrintLeftPartial(cv::Mat &frame, cv::Point pt);
              ^
ObjectTracker.hpp:44:14: warning: inline function ‘void ObjectTracker::PrintRightPartial(cv::Mat&, cv::Point)’ used but never defined
  inline void PrintRightPartial(cv::Mat &frame, cv::Point pt);
              ^
....
CycloTracker.o: In function 'CycloTracker::processFrames()':
/home/fa/Workspace/contadordeciclistas/CycloTracker.cpp:64: undefined reference to 'ObjectTracker::PrintLeftPartial(cv::Mat&)'
/home/fa/Workspace/contadordeciclistas/CycloTracker.cpp:65: undefined reference to 'ObjectTracker::PrintRightPartial(cv::Mat&)'
/home/fa/Workspace/contadordeciclistas/CycloTracker.cpp:104: undefined reference to 'ObjectTracker::PrintLeftPartial(cv::Mat&, cv::Point_<int>)'
/home/fa/Workspace/contadordeciclistas/CycloTracker.cpp:105: undefined reference to 'ObjectTracker::PrintRightPartial(cv::Mat&, cv::Point_<int>)'
collect2: error: ld returned 1 exit status
Makefile:56: recipe for target 'all' failed
make: *** [all] Error 1

I have already compiled all the files thinking that it could be dirt in the code, already compiled, but it does not solve.

How do I proceed to use inline methods in classes?

Here are the codes that have been transformed into inline:

inline void ObjectTracker::PrintRightPartial(cv::Mat &frame, cv::Point pt) {
    char id[10];
    sprintf(id, ">%02d", object_counter->GetLTRPoints());
    cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
            cv::Scalar(0, 0, 0), 5, CV_AA);
    cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
            cv::Scalar(0, 255, 255), 3, CV_AA);
}

inline void ObjectTracker::PrintLeftPartial(cv::Mat &frame, cv::Point pt) {
    char id[10];
    sprintf(id, "<%02d", object_counter->GetRTLPoints());
    cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
            cv::Scalar(0, 0, 0), 5, CV_AA);
    cv::putText(frame, std::string(id), pt, CV_FONT_HERSHEY_PLAIN, 3,
            cv::Scalar(0, 255, 255), 3, CV_AA);
}

inline void ObjectTracker::PrintLeftPartial(cv::Mat &frame) {
    PrintLeftPartial(frame, config->GetCounterPos(0));
}

inline void ObjectTracker::PrintRightPartial(cv::Mat &frame) {
    PrintRightPartial(frame, config->GetCounterPos(1));
}

Calling the above methods:

...

ObjectTracker ot(config, 30, 50, config->getInterestArea());

...

do {

        *cap >> frame;      // captura um novo frame e processa imediatamente
        full = frame.clone();
...
        ot.PrintLeftPartial(full);
        ot.PrintRightPartial(full);
...
    } while (char(key) != char(27));

So I tried to mute the code as suggested here: Inline functions in iteration

But it did not work, causing the following errors:

In file included from CycloTracker.hpp:15:0,
                 from CycloTracker.cpp:3:
ObjectTracker.hpp:46:8: warning: always_inline function might not be inlinable [-Wattributes]
   void PrintRightPartial(cv::Mat &frame)__attribute__((always_inline));
        ^
ObjectTracker.hpp:45:8: warning: always_inline function might not be inlinable [-Wattributes]
   void PrintLeftPartial(cv::Mat &frame)__attribute__((always_inline));
        ^
ObjectTracker.hpp: In member function ‘void CycloTracker::processFrames()’:
ObjectTracker.hpp:45:8: error: inlining failed in call to always_inline ‘void ObjectTracker::PrintLeftPartial(cv::Mat&)’: function body not available
CycloTracker.cpp:64:28: error: called from here
   ot.PrintLeftPartial(full);
                            ^
In file included from CycloTracker.hpp:15:0,
                 from CycloTracker.cpp:3:
ObjectTracker.hpp:46:8: error: inlining failed in call to always_inline ‘void ObjectTracker::PrintRightPartial(cv::Mat&)’: function body not available
   void PrintRightPartial(cv::Mat &frame)__attribute__((always_inline));
        ^
CycloTracker.cpp:65:29: error: called from here
   ot.PrintRightPartial(full);
                             ^
Makefile:65: recipe for target 'CycloTracker.o' failed
make: *** [CycloTracker.o] Error 1

The code looks like this in what I've changed:

 void PrintLeftPartial(cv::Mat &frame, cv::Point pt)__attribute__((always_inline));
 void PrintRightPartial(cv::Mat &frame, cv::Point pt)__attribute__((always_inline));
 void PrintLeftPartial(cv::Mat &frame)__attribute__((always_inline));
 void PrintRightPartial(cv::Mat &frame)__attribute__((always_inline));
    
asked by anonymous 11.10.2016 / 11:42

1 answer

2

Functions declared with inline need to be defined (that is, they must have a body) in the translation unit where they are used. A translation unit is a source file ( .cpp ) and all headers ( .hpp ) that it includes.

In practice, when declaring an inline role in a header , its implementation must be in place. It can be either along with the statement or below.

This applies to free functions or functions that are members of classes (methods).

Example:

inline void function1();  // declarada aqui, definida abaixo
inline void function2() {  // declarada e definida aqui
    // ...
}

class A {
public:
    inline void method1();  // declarado aqui, definido abaixo
    inline void method2() {  // declarado e definido aqui
        // ...
    }
};

void function1() {
    // ...
}

void A::method1() {
    // ...
}
    
11.10.2016 / 22:16