提问人:Tom Gautot 提问时间:6/21/2020 最后编辑:Tom Gautot 更新时间:6/21/2020 访问量:348
使用 pocketsphinx 和 opencv 编译 C++ 项目时未定义的引用
Undefined reference when compiling C++ project with pocketsphinx and opencv
问:
我最近安装了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的结果
有谁知道如何解决这个问题?
答: 暂无答案
评论
--cflags
--libs
pkg-config --cflags opencv pocketsphinx
pkg-config --libs opencv pocketsphinx
opencv
/usr/local
pocketsphinx
--cflags --libs opencv --cflags --libs pocketsphinx
--prefix /usr/local/pocketsphinx
pkg-config --cflags --libs opencv pocketsphinx
pkg-config --cflags --libs opencv --cflags --libs pocketsphinx
$(pkg-config --cflags --libs opencv) $(pkg-config --cflags --libs pocketsphinx)