Python이 C++ 프로그램을 호출하는 방법에 대한 자세한 설명
서문
파이썬의 장점은 높은 개발 효율성과 사용 용이성인 반면, C++의 장점은 운영 효율성이 높다는 것은 누구나 아는 사실입니다. Python에서 프로젝트에 C++ 코드를 포함하거나 Python을 사용하여 C++ 프로젝트에서 주변 기능을 구현하는 경우 Python에서 C++ 모듈을 호출해야 할 수 있습니다. 다음은 C++ 코드를 Python 인터페이스로 내보내는 몇 가지 기본 방법입니다. 함께. .
원래 내보내기
Python 인터프리터는 C로 구현되므로 C++ 데이터 구조를 Python에서 이해할 수 있습니다. , 이론적으로는 직접 호출할 수 있습니다. test1.cpp를 다음과 같이 구현합니다
#include <Python.h> int Add(int x, int y) { return x + y; } int Del(int x, int y) { return x - y; } PyObject* WrappAdd(PyObject* self, PyObject* args) { int x, y; if (!PyArg_ParseTuple(args, "ii", &x, &y)) { return NULL; } return Py_BuildValue("i", Add(x, y)); } PyObject* WrappDel(PyObject* self, PyObject* args) { int x, y; if (!PyArg_ParseTuple(args, "ii", &x, &y)) { return NULL; } return Py_BuildValue("i", Del(x, y)); } static PyMethodDef test_methods[] = { {"Add", WrappAdd, METH_VARARGS, "something"}, {"Del", WrappDel, METH_VARARGS, "something"}, {NULL, NULL} }; extern "C" void inittest1() { Py_InitModule("test1", test_methods); }
컴파일 명령은 다음과 같습니다
g++ -fPIC -shared test1.cpp -I/usr/include/python2.6 -o test1.so
파이썬 해석 실행 테스트는 다음과 같습니다
>>> import test1 >>> test1.Add(1,2) 3
여기서 몇 가지 주의할 점이 있습니다
-
생성된 동적 라이브러리의 이름이 test1이면 소스 파일에 inittest1 함수가 있어야 하며 Py_InitModule의 첫 번째 매개변수는 "test1"이어야 합니다. 그렇지 않으면 Python에서 모듈을 가져오는 데 실패합니다.
-
cpp 소스 파일인 경우 inittest1 함수를 extern "C"로 수정해야 합니다. c 소스 파일인 경우에는 필요하지 않습니다. 그 이유는 Python 인터프리터가 라이브러리를 가져올 때 initxxx와 같은 함수를 찾고, C와 C++는 함수 기호를 다르게 인코딩하기 때문입니다. 특히 C++에서는 함수 기호를 인코딩할 때 함수 길이와 매개변수 유형을 고려합니다. 함수 기호는 부스트
를 통해 구현된 기호nm test1.so
를 통해 함수 프로토타입을 디코딩할 수 있습니다.
#include <boost/python/module.hpp> #include <boost/python/def.hpp> using namespace boost::python; int Add(const int x, const int y) { return x + y; } int Del(const int x, const int y) { return x - y; } BOOST_PYTHON_MODULE(test2) { def("Add", Add); def("Del", Del); }
g++ test2.cpp -fPIC -shared -o test2.so -I/usr/include/python2.6 -I/usr/local/include -L/usr/local/lib -lboost_python
참고: Path를 컴파일할 때 부스트 헤더 파일과 라이브러리를 지정하십시오. 여기에는 /usr/local/include 및 /usr/local/lib
가 있거나 setup.py#!/usr/bin/env python from distutils.core import setup from distutils.extension import Extension setup(name="PackageName", ext_modules=[ Extension("test2", ["test2.cpp"], libraries = ["boost_python"]) ])
python setup.py build
>>> import test2 >>> test2.Add(1,2) 3 >>> test2.Del(1,2) -1
#include <boost/python.hpp> using namespace boost::python; class Test { public: int Add(const int x, const int y) { return x + y; } int Del(const int x, const int y) { return x - y; } }; BOOST_PYTHON_MODULE(test3) { class_<Test>("Test") .def("Add", &Test::Add) .def("Del", &Test::Del); }
참고: BOOST_PYTHON_MODULE에서 .def를 사용하는 것은 Python 구문과 다소 유사합니다. 이는
class_<Test>("Test").def("Add", &Test::Add); class_<Test>("Test").def("Del", &Test::Del);
g++ test3.cpp -fPIC -shared -o test3.so -I/usr/include/python2.6 -I/usr/local/include/boost -L/usr/local/lib -lboost_python
>>> import test3 >>> test = test3.Test() >>> test.Add(1,2) 3 >>> test.Del(1,2) -1
가변 매개변수 내보내기 함수
#include <boost/python.hpp> using namespace boost::python; class Test { public: int Add(const int x, const int y, const int z = 100) { return x + y + z; } }; int Del(const int x, const int y, const int z = 100) { return x - y - z; } BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Add_member_overloads, Add, 2, 3) BOOST_PYTHON_FUNCTION_OVERLOADS(Del_overloads, Del, 2, 3) BOOST_PYTHON_MODULE(test4) { class_<Test>("Test") .def("Add", &Test::Add, Add_member_overloads(args("x", "y", "z"), "something")); def("Del", Del, Del_overloads(args("x", "y", "z"), "something")); }
g++ test4.cpp -fPIC -shared -o test4.so -I/usr/include/python2.6 -I/usr/local/include/boost -L/usr/local/lib -lboost_python
>>> import test4 >>> test = test4.Test() >>> print test.Add(1,2) 103 >>> print test.Add(1,2,z=3) 6 >>> print test4.Del(1,2) -1 >>> print test4.Del(1,2,z=3) -1
Python 객체로 인터페이스 내보내기
def Square(list_a) { return [x * x for x in list_a] }
#include <boost/python.hpp> boost::python::list Square(boost::python::list& data) { boost::python::list ret; for (int i = 0; i < len(data); ++i) { ret.append(data[i] * data[i]); } return ret; } BOOST_PYTHON_MODULE(test5) { def("Square", Square); }
g++ test5.cpp -fPIC -shared -o test5.so -I/usr/include/python2.6 -I/usr/local/include/boost -L/usr/local/lib -lboost_python
>>> import test5 >>> test5.Square([1,2,3]) [1, 4, 9]
, boost::python::tuple
, boost::python::list
이러한 데이터 유형의 사용 방법은 기본적으로 Python과 일치합니다. 특정 방법의 경우 Boost 헤더 파일 boost::python::dict
이고, 사용방법은 다음과 같습니다. boost::python::make_tuple()
boost::python::tuple(int a, int b, int c) { return boost::python::make_tuple(a, b, c); }

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











Linux 터미널에서 Python 버전을 보려고 할 때 Linux 터미널에서 Python 버전을 볼 때 권한 문제에 대한 솔루션 ... Python을 입력하십시오 ...

Python의 Pandas 라이브러리를 사용할 때는 구조가 다른 두 데이터 프레임 사이에서 전체 열을 복사하는 방법이 일반적인 문제입니다. 두 개의 dats가 있다고 가정 해

파이썬에서 문자열을 통해 객체를 동적으로 생성하고 메소드를 호출하는 방법은 무엇입니까? 특히 구성 또는 실행 해야하는 경우 일반적인 프로그래밍 요구 사항입니다.

10 시간 이내에 컴퓨터 초보자 프로그래밍 기본 사항을 가르치는 방법은 무엇입니까? 컴퓨터 초보자에게 프로그래밍 지식을 가르치는 데 10 시간 밖에 걸리지 않는다면 무엇을 가르치기로 선택 하시겠습니까?

Uvicorn은 HTTP 요청을 어떻게 지속적으로 듣습니까? Uvicorn은 ASGI를 기반으로 한 가벼운 웹 서버입니다. 핵심 기능 중 하나는 HTTP 요청을 듣고 진행하는 것입니다 ...

이 기사는 Numpy, Pandas, Matplotlib, Scikit-Learn, Tensorflow, Django, Flask 및 요청과 같은 인기있는 Python 라이브러리에 대해 설명하고 과학 컴퓨팅, 데이터 분석, 시각화, 기계 학습, 웹 개발 및 H에서의 사용에 대해 자세히 설명합니다.

Fiddlerevery Where를 사용할 때 Man-in-the-Middle Reading에 Fiddlereverywhere를 사용할 때 감지되는 방법 ...
