0

Simply including the OpenCV header results in linking error. Why is that?

// test.cpp
#include <opencv2/opencv.hpp>

int foo();
int bar();

int main() {
}

If I compile the file with g++ test.cpp, the following linking error occurs:

/tmp/ccugmQl4.o: In function `cv::String::~String()':
test.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccugmQl4.o: In function `cv::String::operator=(cv::String const&)':
test.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status

If I compile with g++ test.cpp -lopencv_core, it works all right.


My question is:

It seems to me that there's no need to resolve undefined symbols if I do not use it, like the functions foo and bar. There's no definition for them but the compile-link process works alright.

I don't use any OpenCV functions either. Why is there linking error only for OpenCV functions?

And what kinds of stuff defined in headers can cause such a linking error?

5
  • I suspect that there is a static object in OpenCV, and the other things it needs are defined in the headers. Commented Jul 22, 2020 at 8:27
  • How do you expect it to compile even it is not able to find the header? Commented Jul 22, 2020 at 8:34
  • The code compiles just fine. I can run g++ -c test.cpp without any error. Commented Jul 22, 2020 at 8:39
  • Of course I have OpenCV installed at my /usr/local/include/, that's why I can call g++ -c test.cpp without any compiling error. Commented Jul 22, 2020 at 10:06
  • For others who may wonder if linker is able to ignore unused references, the answer is yes, see stackoverflow.com/a/2007049/7413964 Commented Jul 23, 2020 at 2:38

1 Answer 1

1

If you tweak your example a little bit

// test.cpp

int foo();
int bar() {
    foo();
}

int main() {
}

You would notice that it'd stop working because linker won't be able to understand what is foo();

The same thing happens when you include opencv header - there are references to functions which are declared but since you never link opencv itself - linker can't figure what those functions are and where to get them.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow! Wow! May I ask why linker ignores foo in the first place (in my example), if it cannot ignore foo in your example? Why don't it behave in the same way?
In your example there's nothing which would reference foo(). int foo(); just declares that such function may exist somewhere in your code, maybe even in different file or different library - in that case you'd for example be able to use such function without having header included.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.