首页 > 解决方案 > 使用 pocketsphinx 和 opencv 编译 C++ 项目时未定义的引用

问题描述

我最近安装了 pocketsphinx 和 opencv,我用这两个库创建和编译了示例项目,并且都运行良好。然后我尝试创建一个我会同时使用两者的项目,但是编译变得很痛苦。

我认为这与我当前的问题没有任何关系,但首先存在一些冲突的类型声明。我通过修改狮身人面像的prim_type.h文件解决了这个问题,如下所示:

#if !defined(int64) && !defined(int64)
// If other dependencies use those, prevents conflicting declaration
#elif defined(_MSC_VER)
typedef __int64          int64;
typedef unsigned __int64 uint64;
#elif defined(HAVE_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
typedef long long int      int64;
typedef unsigned long long uint64;
#else /* !HAVE_LONG_LONG && SIZEOF_LONG_LONG == 8 */
typedef double          int64;
typedef double          uint64;
#endif /* !HAVE_LONG_LONG && SIZEOF_LONG_LONG == 8 */

第一个如果一开始不存在,我添加了它,这样如果我首先在需要两个依赖项的文件中导入 opencv 和 THEN 狮身人面像就不会有问题。

然后我尝试编译以下代码(这是我用来测试opencv的纯opencv代码)

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"


#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>

#include <iostream>
#include <stdio.h>

#include "body_parts_detection/detectBodyParts.h"


using namespace std;

int main( int argc, const char** argv )
{
    VideoCapture capture(0); // capture video from webcam
    Mat frame, prevFrame;
    
    while( true )
    {
        // Capture the current frame from the input video stream
        capture >> frame;
        
        // Downsample the input frame for faster processing
        float downsampleFactor = 0.5;
        resize(frame, frame, Size(), downsampleFactor, downsampleFactor, INTER_NEAREST);
        
        // Apply the classifier to the frame depending on the enabled macros
        if( !frame.empty() )
        {
            detectEyes(frame);
        }
        
        else
        {
            cout << "No captured frame. Stopping!" << endl;
            break;
        }
        
        int c = waitKey(10);
        if( (char)c == 27 ) { break; }
    }
    
    capture.release();
    
    return 0;
}

顺便说一句body_parts_detection/detectBodyParts.h,我导入的以下项目的头文件:https ://github.com/prateekvjoshi/Body-Parts-Detection ,注意主要功能也来自这个项目。

现在,当我尝试编译它时,只为编译器提供 opencv 库,即使用以下命令:

g++ main.cpp ./body_parts_detection/*.cpp -o main `pkg-config --cflags --libs opencv`

哪里pkg-config --cflags --libs opencv给了我:

-I/usr/include/opencv -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_datasets - lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf - lopencv_line_descriptor -lopencv_optflow -lopencv_video -lopencv_plot -lopencv_reg - lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping - lopencv_rgbd -lopencv_viz -lopencv_surface_matching -lopencv_text -lopencv_ximgproc - lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect - lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs - lopencv_photo -lopencv_imgproc -lopencv_core`

正如我之前所说,它工作得很好!但是,当我尝试使用以下命令添加 pocketsphinx 库时:

g++ main.cpp ./body_parts_detection/*.cpp -o main `pkg-config --cflags --libs opencv pocketsphinx`

在哪里pkg-config --cflags --libs opencv pocketsphinx给我

-I/usr/local/include -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -I/usr/local/include -I/usr/local/include/sphinxbase -I/usr /include/opencv -L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_datasets -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_video -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_viz -lopencv_surface_matching -lopencv_text -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo-lopencv_imgproc -lopencv_core -lpocketsphinx -lsphinxbase -lsphinxad -lpulse -lpulse-simple -lpthread -lm

然后,突然间,它不再工作了,我得到了undefined reference错误(请记住,这是一个纯粹的 opencv 项目,没有 pocketsphinx 依赖项):

/tmp/ccVDpw8s.o: In function `main':
main.cpp:(.text+0x38): undefined reference to `cv::VideoCapture::VideoCapture(int)'
/tmp/ccbZxpo9.o: In function `detectFace(cv::Mat)':
detectBodyParts.cpp:(.text+0x74): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x4d3): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `detectEyes(cv::Mat)':
detectBodyParts.cpp:(.text+0x681): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x6d0): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0xc82): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `detectEars(cv::Mat)':
detectBodyParts.cpp:(.text+0xea9): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0xef8): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x1637): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `detectMouth(cv::Mat)':
detectBodyParts.cpp:(.text+0x185e): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x1cbd): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `detectNose(cv::Mat)':
detectBodyParts.cpp:(.text+0x1e5a): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x22b9): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `detectSmile(cv::Mat)':
detectBodyParts.cpp:(.text+0x2456): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x28b5): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `detectUpperBody(cv::Mat)':
detectBodyParts.cpp:(.text+0x2a52): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x2d0c): undefined reference to `cv::rectangle(cv::Mat&, cv::Rect_<int>, cv::Scalar_<double> const&, int, int, int)'
detectBodyParts.cpp:(.text+0x2d61): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `detectLowerBody(cv::Mat)':
detectBodyParts.cpp:(.text+0x2ee1): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x3340): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `detectFullBody(cv::Mat)':
detectBodyParts.cpp:(.text+0x34dd): undefined reference to `cv::CascadeClassifier::load(cv::String const&)'
detectBodyParts.cpp:(.text+0x393c): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/tmp/ccbZxpo9.o: In function `cv::String::String(char const*)':
detectBodyParts.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccbZxpo9.o: In function `cv::String::~String()':
detectBodyParts.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

编辑 这是似乎引起麻烦的库部分。我可以使用 --cflags ofpocketsphinx 保留 pkg-config 的结果,只要 --libs 标志仅随 opencv 一起提供

有谁知道如何解决这个问题?

标签: c++opencvcompiler-errorsundefined-referencepocketsphinx

解决方案


推荐阅读