您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用SWIG包装python的C代码.无法使用cout命令

5b51 2022/1/14 8:21:35 python 字数 3597 阅读 524 来源 www.jb51.cc/python

我正在尝试使用SWIG为python包装这个简单的C代码:#include 'hello.h' int helloW() { std::cout << 'Hello, World!' ; return 0; } 这里是相对标题:#include <iostream> int helloW() ; // decl 正

概述

我正在尝试使用SWIG为python包装这个简单的C代码

#include "hello.h"

int helloW() 
{
    std::cout << "Hello,World!" ;
    return 0;
}

这里是相对标题

#include 
  

正如我正在使用的SWIG输入文件

/* file : pyhello.i */

/* name of module to use*/
%module pyhello
%{
    #include "hello.h"
%}    
%include "hello.h";

现在,我的makefile(运行正常)是:

all:
    swig -c++ -python -Wall pyhello.i 
    gcc -c -fpic pyhello_wrap.cxx hello.cpp -I/usr/include/python2.7
    gcc -shared hello.o pyhello_wrap.o -o _pyhello.so

因为我能够从不同的来源汇总相关的问题在线.
现在,一旦我尝试使用命令导入python我的库

>>> import pyhello

这是我得到的错误

    Traceback (most recent call last):
  File "
  
   __init__.py",line 37,in import_module
    __import__(name)
ImportError: ./_pyhello
   .so: undefined symbol: _ZSt4cout

  

这让我觉得这个问题与命令std :: cout相关,或者一般来说,与标准库< iostream>相关.

希望有人可以给我一些关于这个问题的提示.非常感谢提前!!

注意:同样的问题我尝试使用命令printf()而不是std :: cout和库< cstdio>而不是< iostream>

ImportError: ./_pyhello.so: undefined symbol: _ZSt4cout

用c filt _ZSt4cout你会发现它是std :: cout(name mangling).

你应该使用g,而不是gcc,尤其是你的链接器命令(带-shared).

或者您需要显式链接某些-lstdc您的共享库.

阅读Drepper的How to Write Shared Libraries(因为Python是dlopen(3),然后是dlsym(3)).

你最好声明为extern“C”int helloW(void);你的日常工作(阅读C++ dlopen minihowto).

总结

以上是编程之家为你收集整理的使用SWIG包装python的C代码.无法使用cout命令全部内容,希望文章能够帮你解决使用SWIG包装python的C代码.无法使用cout命令所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶