ホーム » 2020 » 8月 » 06

日別アーカイブ: 2020/08/06

2020年8月
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

カテゴリー

アーカイブ

ブログ統計情報

  • 99,452 アクセス


Python から CPP の呼出し – 3

////////////////////////////////////////////////////////////////////
//	test_cpp.hpp
//
#pragma		once

#include	"_s_func.hxx"
#include	"_t_func.hxx"
#include	"_tdefine.hxx"

class	test_class	{
public:
			test_class	(LPCTSTR n)	{	name = n ;	std::tout<< name + _T("\t***") << std::endl ;	}
	virtual		~test_class	()      	{	          	std::tout<< name + _T("\t---") << std::endl ;	}
public:
	tstring		name ;
	} ;

inline	test_class*	get_test_class	(void)
{
	static	test_class	G_tc("G_test") ;
	return	&G_tc ;
	}

////////////////////////////////////////////////////////////////////
//	test_cpp.cpp
//
#include	"test_cpp.hpp"
#include	<clocale>
#include	<iostream>

test_class	tc(_T("global")) ;

int	_tmain	(int argc,TCHAR* argv[])
{
	_tsetlocale(LC_ALL,_T("")) ;
	test_class	tc(_T("local 1")) ;
	{
		test_class	tc(_T("local 2")) ;
		std::tout << _T("hello") << std::endl ;
		}
	{
		test_class*	gt = ::get_test_class() ;
		std::tout << gt->name << std::endl ;
		}
	return	0 ;
	}
////////////////////////////////////////////////////////////////////
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/test_cpp $ g++ test_cpp.cpp -Wall
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/test_cpp $ ./a.out
global  ***
local 1 ***
local 2 ***
hello
local 2 ---
G_test  ***
G_test
local 1 ---
G_test  ---
global  ---
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/test_cpp $
////////////////////////////////////////////////////////////////////
//	t_cpp.cpp
//
#include	<Python.h>

#include	"test_cpp.hpp"
#include	<clocale>
#include	<iostream>

test_class	tc(_T("global")) ;

static	PyObject*	local__	(PyObject* self, PyObject* args)
{
	test_class	tc(_T("local")) ;
	std::tout << tc.name << std::endl ;
	return	Py_None;
	}

static	PyObject*	global_	(PyObject* self, PyObject* args)
{
	test_class*	gt = ::get_test_class() ;
	std::tout << gt->name << std::endl ;
	return	Py_None;
	}

static	PyMethodDef			t_cpp_methods[] = {
	{	"local__",   	local__,	METH_NOARGS,	"local__"	},
	{	"global_",   	global_,	METH_NOARGS,	"global_"	},
	{	NULL								},
	} ;

static	struct	PyModuleDef	t_cpp = {
	PyModuleDef_HEAD_INIT,
	"t_cpp",
	"test cpp module",
	-1,
	t_cpp_methods
	} ;

PyMODINIT_FUNC	PyInit_t_cpp(void)
{
	return	PyModule_Create(&t_cpp) ;
	}
////////////////////////////////////////////////////////////////////
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/test_cpp $ g++ t_cpp.cpp -Wall -fPIC -shared -o t_cpp.so -I /volume1/.@plugins/AppCentral/python3/include/python3.7m/
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/test_cpp $ python3
Python 3.7.0 (default, Aug 23 2018, 17:48:39)
[GCC 4.6.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import t_cpp
global  ***
>>> dir(t_cpp)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'global_', 'local__']
>>> t_cpp.local__()
local   ***
local
local   ---
>>> t_cpp.local__()
local   ***
local
local   ---
>>> t_cpp.global_()
G_test  ***
G_test
>>> t_cpp.global_()
G_test
>>> import t_cpp
>>> t_cpp.local__()
local   ***
local
local   ---
>>> t_cpp.global_()
G_test
>>> exit()
G_test  ---
global  ---
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/test_cpp $ 

Python から C++ 呼び出し時のコンストラクタ,デストラクタ

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.