Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ImportError:导入带开关的c++-python中的类_Python_C++_G++_Swig_Distutils - Fatal编程技术网

ImportError:导入带开关的c++-python中的类

ImportError:导入带开关的c++-python中的类,python,c++,g++,swig,distutils,Python,C++,G++,Swig,Distutils,我尝试在Python中使用c++类进行套接字通信。因此,我创建了一个使用。将swiged文件导入python时,我得到ImportError:undefined符号:nng_msg_insert。该类的定义为: /* commclass.h */ #include <nngpp/nngpp.h> #include <nngpp/protocol/req0.h> #include <nngpp/protocol/rep0.h> #include <nngp

我尝试在Python中使用c++类进行套接字通信。因此,我创建了一个使用。将swiged文件导入python时,我得到ImportError:undefined符号:nng_msg_insert。该类的定义为:

/* commclass.h */
#include <nngpp/nngpp.h>
#include <nngpp/protocol/req0.h>
#include <nngpp/protocol/rep0.h>
#include <nngpp/msg_body.h>
#include <nngpp/msg_header.h>
#include <nngpp/msg.h>
#include <nngpp/socket.h>
#include <nngpp/view.h>
#include <string>
#include <nlohmann/json.hpp>
//#include <thread>
#include <iostream>
#include <cstdio>
#include "/usr/local/include/nng/nng.h"
//#include <memory>
#include <chrono>
using json = nlohmann::json;

class CommIF
{
private:
    nng::socket socket;
    nng::msg message;
    int msg_size;
public:
    CommIF(const std::string option, std::string ipToListen, std::string ipToDial)
    {
        message = nng::make_msg(0);
        if (option.compare("rep") == 0)
        {
            socket = std::move(nng::rep::v0::open());
        }
        else if (option.compare("req") == 0)
        {
            socket = std::move(nng::req::v0::open());
        }
        else
        {
            printf("EXCEPTION");
        }
        socket.listen(ipToListen.c_str());
        bool connected = false;
        while (connected == false)
        {
            try
            {
                socket.dial(ipToDial.c_str());
                connected = true;
                std::cout << "successfully connected\n";
            }
            catch (const nng::exception &e)
            {
                std::cerr << e.what() << "; retry in 1 s" << '\n';
                //std::this_thread::sleep_for(std::chrono::seconds(1));
            }
        }
        msg_size = 0;
    }
};
然后,我从命令
python3 build\u commclass.py build\u ext--inplace
开始构建过程。文件build_commclass.py如下所示

from distutils.core import setup, Extension
import os

name = "commclass"
version = "0.0.1"               
os.environ["CC"] = "g++"

setup(name = name, version = version, ext_modules = [Extension(
name = '_commclass', 
sources = ["commclass.i"],#"src/commclass.h"],
include_dirs = ['src'],#'/home/user1/Documents/extLibs','/usr/local/include'],
swig_opts = ["-c++", "-modern"]
)])
/* commclass.i */
/* module*/
%module commclass
%{
#include "/usr/local/include/nng/nng.h"
#include "src/nngpp/nngpp.h"
#include "src/nngpp/protocol/req0.h"
#include "src/nngpp/protocol/rep0.h"
#include "src/nngpp/socket.h"
#include "src/nngpp/msg.h"
#include "src/nngpp/aio.h"
#include "src/nngpp/aio_view.h"
#include "src/nngpp/msg_body.h"
#include "src/nngpp/msg_header.h"
#include "src/commclass.h"
%}
%include "/usr/local/include/nng/nng.h"
%include "src/commclass.h"
当我现在将类导入python时,我得到了上面提到的错误。我在google和stackoverflow上搜索了很多,我很确定这是一个链接器问题。我还在distutils.core的编译器和链接器选项上做了很多尝试,但没有找到解决方案

编辑1: 我现在更改了接口文件,如下所示

from distutils.core import setup, Extension
import os

name = "commclass"
version = "0.0.1"               
os.environ["CC"] = "g++"

setup(name = name, version = version, ext_modules = [Extension(
name = '_commclass', 
sources = ["commclass.i"],#"src/commclass.h"],
include_dirs = ['src'],#'/home/user1/Documents/extLibs','/usr/local/include'],
swig_opts = ["-c++", "-modern"]
)])
/* commclass.i */
/* module*/
%module commclass
%{
#include "/usr/local/include/nng/nng.h"
#include "src/nngpp/nngpp.h"
#include "src/nngpp/protocol/req0.h"
#include "src/nngpp/protocol/rep0.h"
#include "src/nngpp/socket.h"
#include "src/nngpp/msg.h"
#include "src/nngpp/aio.h"
#include "src/nngpp/aio_view.h"
#include "src/nngpp/msg_body.h"
#include "src/nngpp/msg_header.h"
#include "src/commclass.h"
%}
%include "/usr/local/include/nng/nng.h"
%include "src/commclass.h"

我现在在python中导入时遇到相同的错误,但是未定义的符号发生了更改。现在是
nng\u aio\u set\u iov
nng_msg_insert
nng_aio_set_iov
都在我现在包含的文件nng.h中定义。我现在很困惑。

SWIG只为默认情况下由
%include
直接指定的定义生成接口<代码>nng\u msg\u insert未在
src/commclass.h
中定义。您需要额外的
%include
语句才能引入定义

注意:您可以将默认值更改为使用
-includeall
SWIG标志递归到所有
#include
语句中,但通常不希望对整个
等接口进行包装,如果不进行大量额外工作,它可能无法工作