使用boost.python导入调用opencv的方法,但编译后找不到符号导致失败

1 投票
1 回答
850 浏览
提问于 2025-04-15 20:46

我现在没有代码,因为我不在家……不过我在C++中使用了boost库,让Python可以调用一个叫做loadImageIntoMainWindow(string filepath)的函数。

在C++的源代码里,这个方法会调用文件顶部导入的opencv方法。我在我的Jamroot文件中包含了opencv,并且还找到了一种在命令行手动编译和链接的方法……无论哪种情况,当我运行我的Python文件时,它会抱怨找不到第一个调用opencv方法的符号……

我一回到家就会更新,提供C++代码、命令行编译的命令、Jamroot文件和Python文件。

这是Jamroot文件:

# Copyright David Abrahams 2006. Distributed under the Boost
# Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

using python ;
lib libboost_python : : <name>boost_python-mt-py26 ;
# Specify the path to the Boost project.  If you move this project,
# adjust this path to refer to the Boost root directory.
use-project boost
  : ./ ;

# Set up the project-wide requirements that everything uses the
# boost_python library from the project whose global ID is
# /boost/python.

project
  : requirements
        <search>/usr
        <library>libboost_python
        <include>/usr/include/opencv ;
# Declare the three extension modules.  You can specify multiple
# source files after the colon separated by spaces.
python-extension uTrackSpheresForPyInterface : uTrackSpheresForPyInterface.cpp ;

# A little "rule" (function) to clean up the syntax of declaring tests
# of these extension modules.
local rule run-test ( test-name : sources + )
{
    import testing ;
    testing.make-test run-pyd : $(sources) : : $(test-name) ;
}

# Declare test targets

在我运行 bjam --preserve-test-targets 之后

或者

g++ -c -g -Wall -fPIC -pipe -DBOOST_PYTHON_MAX_ARITY=20 -I.  -I/usr/include/opencv/ - /usr/include/python2.6 `pkg-config --libs opencv` uTrackSpheresForPyInterface.cpp
g++ -shared -o uTrackSpheresForPyInterface.so uTrackSpheresForPyInterface.o -L/usr/lib - python2.6 -lboost_python-mt-py26

我得到了这个:

nathan@innovation:~/Research RIT/openCv$ python uTrackSpheres.py
Traceback (most recent call last):
  File "uTrackSpheres.py", line 18, in <module>
    import uTrackSpheresForPyInterface
ImportError: /home/nathan/Research RIT/openCv/uTrackSpheresForPyInterface.so: undefined symbol: cvCvtColor
nathan@innovation:~/Research RIT/openCv$ 

而在cpp文件中,我做的事情比这多一点:

#include <iostream>
using namespace std;
#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#endif

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>


int loadImageIntoMainWindow(string imgPath) {

        if( (imgLoaded = cvLoadImage(imgPath.c_str(),1)) == 0 )
                return 0;
        imgMain = cvCreateImage( cvSize(imgLoaded->width, imgLoaded->height), 8, 1 );
        cvCvtColor( imgLoaded, imgMain, CV_BGR2GRAY );

        cvNamedWindow( charCurrentFilename,CV_WINDOW_AUTOSIZE);
        cvSetMouseCallback(charCurrentFilename, on_mouse_imgMain, 0 );
        cvShowImage(charCurrentFilename, imgMain);

        return 1;
}

BOOST_PYTHON_MODULE(uTrackSpheresForPyInterface)
{
    using namespace boost::python;
    def("loadImageIntoMainWindow", loadImageIntoMainWindow);
}

1 个回答

0

在你的 Jamfile 文件中添加以下几行(根据需要修改 /your/lib/path 的路径...):

lib cvlib : : <name>cv <search>/your/lib/path/lib ;
lib cxcorelib : : <name>cxcore <search>/your/lib/path/lib ;

然后编辑

python-extension uTrackSpheresForPyInterface : uTrackSpheresForPyInterface.cpp ;

使其内容变成:

python-extension uTrackSpheresForPyInterface :
    uTrackSpheresForPyInterface.cpp
    cvlib
    cxcorelib ;

撰写回答