linux下C++调用python,识别算法。报错 undefined reference to `PyCObject_Type',求解决

我的C++源代码为:

//#include <Python.h>
#include<python2.7/Python.h>
#include<iostream>
//#include <windows.h>
#include <string.h>
#include <string>
#include "opencv2/opencv.hpp"
#include <numpy/arrayobject.h>
#include "thread"
#include <mutex>
# define _GLIBCXX_USE_CXX11_ABI 0

using namespace std;
using namespace cv;
/**
string Utf8ToGbk(const char* src_str)
{
    int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
    wchar_t* wszGBK = new wchar_t[len + 1];
    memset(wszGBK, 0, len * 2 + 2);
    MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
    len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
    char* szGBK = new char[len + 1];
    memset(szGBK, 0, len + 1);
    WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
    string strTemp(szGBK);
    if (wszGBK) delete[] wszGBK;
    if (szGBK) delete[] szGBK;
    return strTemp;
}
**/
void init_numpy()
{
import_array();

int transport(cv::Mat img)
{
    
    //Py_SetPythonHome(L"C:/Users/38487/Anaconda3");
    Py_SetPythonHome("/home/jk/anaconda2/envs/car_dect/lib/python3.6");        
    Py_Initialize();//初始化
   //import_array();   
    init_numpy();               
    PyRun_SimpleString("import sys"); // 
    PyRun_SimpleString("sys.path.append('./')");//
    PyObject* pModule = PyImport_ImportModule("car_plate_frozen");//   加载车牌识别模块
    PyObject* pDict = PyModule_GetDict(pModule);

    PyObject* pFuncFive = PyDict_GetItemString(pDict, "load_image");  //加载载入图片模块。

    //PyObject* pFuncFive2 = PyDict_GetItemString(pDict, "init");//
    //PyObject_CallObject(pFuncFive2, NULL);

    npy_intp dims[] = { img.rows, img.cols, 3 };
    PyObject* pValue = PyArray_SimpleNewFromData(3, dims, NPY_UINT8, img.data);

    PyObject* ArgArray = PyTuple_New(1);

    //PyTuple_SetItem(ArgArray, 0, pValue);
    
    PyObject* pRet = PyObject_CallObject(pFuncFive, ArgArray);

    char* res;
    PyArg_Parse(pRet, "s", &res);//

    //string str_res = Utf8ToGbk(res);  

    cout << "res:" << res << endl;//
    //Py_Finalize();

    return 0;

}

int main()

        
        
    cv::Mat img = cv::imread("/home/jk/Downloads/Project3/1.jpg", 1);

    cv::Mat img1 = cv::imread("/home/jk/Downloads/Project3/2.jpg", 1);

    PyEval_InitThreads();

    //PyEval_ReleaseThread(PyThreadState_Get());

    thread t1(transport, img);

    //thread t2(transport, img1);

    t1.join();

    //t2.join();

    //transport(pFuncFive, img);

    Py_Finalize();
        return 0;
}

我在终端使用的编译命令为:g++ test_multithread.cpp -I/usr/include/python3.5 -L/usr/lib/python3.5 -lpython3.5m  -std=c++11 -lpthread `pkg-config opencv --cflags --libs`

然后报的错为:

In file included from /usr/include/numpy/ndarraytypes.h:1777:0,
 from /usr/include/numpy/ndarrayobject.h:18,
 from /usr/include/numpy/arrayobject.h:4,
from test_multithread.cpp:8:
/usr/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it by " \
  ^
test_multithread.cpp:11:0: warning: "_GLIBCXX_USE_CXX11_ABI" redefined
 # define _GLIBCXX_USE_CXX11_ABI 0
 ^
In file included from /usr/include/c++/5/iostream:38:0,
                 from test_multithread.cpp:3:
/usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h:212:0: note: this is the location of the previous definition
 # define _GLIBCXX_USE_CXX11_ABI 1
 ^
test_multithread.cpp: In function ‘int transport(cv::Mat)’:
test_multithread.cpp:40:74: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
         Py_SetPythonHome("/home/jk/anaconda2/envs/car_dect/lib/python3.6");        
                                                                          ^
/tmp/ccAIqf6I.o: In function `_import_array':
test_multithread.cpp:(.text+0x181): undefined reference to `PyCObject_Type'
test_multithread.cpp:(.text+0x1e5): undefined reference to `PyCObject_AsVoidPtr'

 

请问有知道的大神吗,可以告诉我怎么解决吗。。。。快崩溃了。。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++编译过程中,当出现"undefined reference to"的错误时,一般是由于链接时找不到对应的库引起的。解决这个问题的方法有以下几种: 1. 确保库文件存在:首先要确保所需的库文件存在,并且路径正确。可以使用`ls`命令检查库文件是否存在。 2. 添加库文件路径:如果库文件存在于非标准路径下,可以通过设置环境变量`LIBRARY_PATH`来告诉编译器库文件的位置。例如,可以使用以下命令将`/usr/local/lib`路径添加到`LIBRARY_PATH`环境变量中: ```shell export LIBRARY_PATH=/usr/local/lib:$LIBRARY_PATH ``` 3. 指定库文件:如果库文件的路径无法通过环境变量解决,可以直接在编译命令中指定库文件的路径。例如,使用`-L`选项指定库文件的路径: ```shell g++ -o output_file source_file.cpp -L/path/to/library -llibrary_name ``` 4. 检查库文件名:确保在编译命令中正确指定了库文件的名称。库文件名通常以`lib`开头,并以`.a`(静态库)或`.so`(共享库)结尾。 5. 检查函数名:如果报错信息中指定了具体的函数名,可以检查函数名是否正确拼写,并确保在编译命令中正确链接了对应的库文件。 6. 检查编译顺序:如果项目中有多个源文件和库文件,确保在编译命令中按照正确的顺序指定它们。一般来说,先编译源文件,再链接库文件。 7. 检查编译选项:有时候需要添加一些特定的编译选项来解决链接错误。例如,使用`-l`选项指定链接的库文件。 8. 检查编译器版本:某些链接错误可能是由于编译器版本不兼容导致的。可以尝试升级或更换编译器版本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值