参考:
http://blog.chinaunix.net/uid-20564848-id-73829.html
http://www.cnblogs.com/dyllove98/archive/2013/06/25/3155599.html
http://blog.youkuaiyun.com/joeblackzqq/article/details/10431733
1、
编写c动态库(含c++语法)的代码
/home/linaro/projects/include/hello.h
#ifdef _HELLO_H_
#define _HELLO_H_
void hello();
#endif
.编写/home/linaro/projects/lib/hello.c
#include "hello.h"
#include <stdio.h>
#include <iostream>
using namespace std;
class TestLib{
public:
void display();
void display(int a);
};
void TestLib::display() {
cout<<"First display"<<endl;
}
void TestLib::display(int a) {
cout<<"Second display"<<endl;
}
extern "C" {
TestLib obj;
void display() {
obj.display();
}
void display_int() {
obj.display(2);
}
void hello()
{
printf("hello world!\n");
}
}
2.用g++编译生成/home/linaro/projects/lib/libhello.so 动态库
g++ hello.c -I../include -fPIC -shared -o libhello.so
3、编写 python代码 /home/linaro/projects/src/call.py
import ctypes
so = ctypes.CDLL("../lib/libhello.so")
print("------------------------------------\n")
so.hello()#测试python代码中如何调用c动态库libhello.so中的代码
print("------------------------------------\n")
so.display()
print("------------------------------------\n")
so.display(2)
print("------------------------------------\n")
def pythonMethod(param1):
print(param1)
so.display()
4、在 lib目录下运行python脚本代码,测试python代码调用c动态库 python call.py 输出结果
------------------------------------
hello world!
------------------------------------
First display
------------------------------------
First display
------------------------------------
5、编写包含main方法的c/c++代码 /home/linaro/projects/src/main.c
#include"hello.h"
#include <Python.h>
//#include<iostream>
//using namespace std;
int main() {
// hello(); //测试libhello.so中的hello方法,下面测试c/c++代码中如何调用python代码
Py_Initialize();
if (!Py_IsInitialized()) return 0;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
//import Module
PyObject* pModule = PyImport_ImportModule("call");
if (!pModule) {
// cout<<"Can't import Module!/n"<<endl;
printf("Can't import Module!/n");
return -1;
}
PyObject* pDict = PyModule_GetDict(pModule);
if (!pDict) {
return -1;
}
//fetch Function
PyObject* pFunHi = PyDict_GetItemString(pDict, "pythonMethod");
PyObject_CallFunction(pFunHi, "s", "stat to call display!");
Py_DECREF(pFunHi);
//Release
Py_DECREF(pModule);
Py_Finalize();
return 0;
}
6、编译/home/linaro/projects/src/main.c 生成 可执行文件/home/linaro/projects/src/main
gcc main.c -I/usr/include/python2.7 -I../include -L../lib -lhello -ldl -lutil -lpthread -lpython2.7 -o main
7、运行二进制文件main (./main)提示错误:
error while loading shared libraries: libhello.so: cannotopen shared object file: No such file or directory
即找不到动态库文件libhello.so,
解决方法:
设置环境变量LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=”../lib”
重新 运行二进制文件main (./main)输出:
------------------------------------
hello world!
------------------------------------
First display
------------------------------------
First display
------------------------------------
stat to call display!
First display
8、 祝各位好运!
另外
qq群 机器视觉解决方案群 142669208
简介:
机器视觉,机器人,视觉自动化,图像处理,视频分析,人工智能领域技术开发交流
iMV视觉核心库提供最专业的视觉解决方案, 欢迎项目合作,定制开发。
机器视觉论坛
http://www.m-vision.club
本人qq:645625872
http://blog.chinaunix.net/uid-20564848-id-73829.html
http://www.cnblogs.com/dyllove98/archive/2013/06/25/3155599.html
http://blog.youkuaiyun.com/joeblackzqq/article/details/10431733
1、
编写c动态库(含c++语法)的代码
/home/linaro/projects/include/hello.h
#ifdef _HELLO_H_
#define _HELLO_H_
void hello();
#endif
.编写/home/linaro/projects/lib/hello.c
#include "hello.h"
#include <stdio.h>
#include <iostream>
using namespace std;
class TestLib{
public:
void display();
void display(int a);
};
void TestLib::display() {
cout<<"First display"<<endl;
}
void TestLib::display(int a) {
cout<<"Second display"<<endl;
}
extern "C" {
TestLib obj;
void display() {
obj.display();
}
void display_int() {
obj.display(2);
}
void hello()
{
printf("hello world!\n");
}
}
2.用g++编译生成/home/linaro/projects/lib/libhello.so 动态库
g++ hello.c -I../include -fPIC -shared -o libhello.so
3、编写 python代码 /home/linaro/projects/src/call.py
import ctypes
so = ctypes.CDLL("../lib/libhello.so")
print("------------------------------------\n")
so.hello()#测试python代码中如何调用c动态库libhello.so中的代码
print("------------------------------------\n")
so.display()
print("------------------------------------\n")
so.display(2)
print("------------------------------------\n")
def pythonMethod(param1):
print(param1)
so.display()
4、在 lib目录下运行python脚本代码,测试python代码调用c动态库 python call.py 输出结果
------------------------------------
hello world!
------------------------------------
First display
------------------------------------
First display
------------------------------------
5、编写包含main方法的c/c++代码 /home/linaro/projects/src/main.c
#include"hello.h"
#include <Python.h>
//#include<iostream>
//using namespace std;
int main() {
// hello(); //测试libhello.so中的hello方法,下面测试c/c++代码中如何调用python代码
Py_Initialize();
if (!Py_IsInitialized()) return 0;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
//import Module
PyObject* pModule = PyImport_ImportModule("call");
if (!pModule) {
// cout<<"Can't import Module!/n"<<endl;
printf("Can't import Module!/n");
return -1;
}
PyObject* pDict = PyModule_GetDict(pModule);
if (!pDict) {
return -1;
}
//fetch Function
PyObject* pFunHi = PyDict_GetItemString(pDict, "pythonMethod");
PyObject_CallFunction(pFunHi, "s", "stat to call display!");
Py_DECREF(pFunHi);
//Release
Py_DECREF(pModule);
Py_Finalize();
return 0;
}
6、编译/home/linaro/projects/src/main.c 生成 可执行文件/home/linaro/projects/src/main
gcc main.c -I/usr/include/python2.7 -I../include -L../lib -lhello -ldl -lutil -lpthread -lpython2.7 -o main
7、运行二进制文件main (./main)提示错误:
error while loading shared libraries: libhello.so: cannotopen shared object file: No such file or directory
即找不到动态库文件libhello.so,
解决方法:
设置环境变量LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=”../lib”
重新 运行二进制文件main (./main)输出:
------------------------------------
hello world!
------------------------------------
First display
------------------------------------
First display
------------------------------------
stat to call display!
First display
8、 祝各位好运!
另外
qq群 机器视觉解决方案群 142669208
简介:
机器视觉,机器人,视觉自动化,图像处理,视频分析,人工智能领域技术开发交流
iMV视觉核心库提供最专业的视觉解决方案, 欢迎项目合作,定制开发。
机器视觉论坛
http://www.m-vision.club
本人qq:645625872