OpenCV 未定义引用我自己的库中的方法



我在OpenCV C++写了一个软件,它的结构非常合理:

  • 主.cpp
  • 测试FPS.cpp
  • testfps.hpp

问题是我得到这两个错误

对"myTestfps1(int, int)"
的未定义引用 对"myTestfps2(int, int)"的未定义引用

这两个方法是用testfps编写的.cpp并在testfps.hpp中声明。

在主要.cpp被宣布所有必要的#include <name>,在它们之后有#include "testfps.hpp"


主.cpp

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include "testfps.hpp"
int main(int argc, char** argv[]){
    ....
    switch(c){
    case 1: myTestfps1(a,b);break;
    case 2: myTestfps2(a,b);break;
    }
...
}

测试FPS.cpp

#include <opencv2/opencv.hpp>
#include <time.h>
#include <stdio.h>
#include "testfps.hpp"
int myTestfps1(int a, int b){
...
}
int myTestfps2(int a, int b){
...
}

testfps.hpp

#ifndef TESTFPS_HPP
#define TESTFPS_HPP
int myTestfps1(int a, int b);
int myTestfps2(int a, int b);
#endif

这是怎么回事?

正如Samuel指出的那样,您可能遇到的主要问题是您在编译中没有包含testfps.cpp。

如果你在带有 g++ 的 Linux 上运行,请尝试以下操作:

g++ -o testfps main.cpp testfps.cpp

无论如何,由于您正在处理C++我建议您不要使用诸如stdio.h和stdlib.h之类的C标头。改用 cstdio 和 cstdlib:

#include <cstdlib>
#include <cstdio>

最后,你对 main 的定义是错误的,argv 不是指针3

int main(int argc, char* argv[])

或:

int main(int argc, char** argv)

最新更新