python loadlibrary_使用ctypes.cdll.LoadLibrary从Python加载库时ELF头无效

本文介绍了在Linux上使用Python的ctypes模块加载C++编译的静态库时遇到的'invalid ELF header'错误。通过将静态库转换为动态库,并在C++代码中使用extern "C"来确保函数调用兼容,成功解决了问题。最终,作者展示了如何编译C++代码为动态库,并从Python中正确调用这些函数。
摘要由CSDN通过智能技术生成

我刚刚开始在Linux上使用gcc。我正在学习教程here,只是我在使用g++编译器。

你好,cpp#include

#include "hello.h"

void

hello (const char * name)

{

printf ("Hello, %s!\n", name);

}

再见,cpp#include

#include "hello.h"

void

bye (void)

{

printf ("Goodbye!\n");

}

你好,hvoid hello (const char * name);

void bye (void);

然后在shell中运行以下命令:$ g++ -Wall -c hello_fn.cpp

$ g++ -Wall -c bye_fn.cpp

$ ar cr libhello.a hello_fn.o bye_fn.o

然后我从python中尝试以下操作:Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)

[GCC 4.5.2] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import ctypes

>>> test = ct

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值