使用 pocketsphinx 和 opencv 编译 C++ 项目时未定义的引用

Undefined reference when compiling C++ project with pocketsphinx and opencv

提问人:Tom Gautot 提问时间:6/21/2020 最后编辑:Tom Gautot 更新时间:6/21/2020 访问量:348

问:

我最近安装了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 和 sphinx,就不会有问题。

然后,我尝试编译以下代码(这是我用来测试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;
}

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

现在,当我尝试编译它时,仅向编译器提供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

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

/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

编辑似乎是 libs 部分造成了麻烦。只要--libs标志只带有opencv,我就可以使用--cflags ofpocketsphinx保留pkg-config的结果

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

C++ OpenCV 编译器-错误 未定义-引用 pocketsphinx

评论

0赞 Waqar 6/21/2020
尝试分别指定和。g++ main.cpp -o main--cflags--libspkg-config --cflags opencv pocketsphinxpkg-config --libs opencv pocketsphinx
0赞 Tom Gautot 6/21/2020
@Waqar不幸的是,它似乎没有改变任何事情
0赞 n. m. could be an AI 6/21/2020
您可能有一个不完整和/或过时的 installed in 版本,该版本会转移到。尝试单独指定(按此顺序)。我个人总是将每个本地软件包安装在自己的前缀下,例如 ,以免在数据包之间产生干扰。opencv/usr/localpocketsphinx--cflags --libs opencv --cflags --libs pocketsphinx--prefix /usr/local/pocketsphinx
0赞 Tom Gautot 6/21/2020
@n.'代词'm.那也行不通。我比较了 和 的结果。它们完全相等。pkg-config --cflags --libs opencv pocketsphinxpkg-config --cflags --libs opencv --cflags --libs pocketsphinx
1赞 n. m. could be an AI 6/21/2020
对不起,我的意思是,两个单独的 pkg-config 命令。无论如何,如果您在 /usr/local/lib 中有一个损坏的 opencv,最好将其删除。$(pkg-config --cflags --libs opencv) $(pkg-config --cflags --libs pocketsphinx)

答: 暂无答案