简单地包含 <opencv2/opencv.hpp> 会导致链接错误
Simply including <opencv2/opencv.hpp> results in linking error
简单地包含 OpenCV header 会导致 链接错误 。这是为什么?
// test.cpp
#include <opencv2/opencv.hpp>
int foo();
int bar();
int main() {
}
如果我用 g++ test.cpp
编译文件,会出现以下链接错误:
/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
如果我用 g++ test.cpp -lopencv_core
编译,它工作正常。
我的问题是:
在我看来,如果我不使用它,就不需要解析未定义的符号,比如函数foo
和bar
。它们没有定义,但 compile-link 过程正常。
我也不使用任何 OpenCV 函数。为什么只有 OpenCV 函数会出现链接错误?
在 header 中定义的什么样的东西会导致这样的链接错误?
如果你稍微调整一下你的例子
// test.cpp
int foo();
int bar() {
foo();
}
int main() {
}
您会注意到它会停止工作,因为 linker 将无法理解什么是 foo();
当你包含 opencv header 时会发生同样的事情 - 有对声明的函数的引用但是因为你从来没有 link opencv 本身 - linker 无法弄清楚是什么这些功能是什么以及从哪里获得它们。
简单地包含 OpenCV header 会导致 链接错误 。这是为什么?
// test.cpp
#include <opencv2/opencv.hpp>
int foo();
int bar();
int main() {
}
如果我用 g++ test.cpp
编译文件,会出现以下链接错误:
/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
如果我用 g++ test.cpp -lopencv_core
编译,它工作正常。
我的问题是:
在我看来,如果我不使用它,就不需要解析未定义的符号,比如函数foo
和bar
。它们没有定义,但 compile-link 过程正常。
我也不使用任何 OpenCV 函数。为什么只有 OpenCV 函数会出现链接错误?
在 header 中定义的什么样的东西会导致这样的链接错误?
如果你稍微调整一下你的例子
// test.cpp
int foo();
int bar() {
foo();
}
int main() {
}
您会注意到它会停止工作,因为 linker 将无法理解什么是 foo();
当你包含 opencv header 时会发生同样的事情 - 有对声明的函数的引用但是因为你从来没有 link opencv 本身 - linker 无法弄清楚是什么这些功能是什么以及从哪里获得它们。