拡張モジュール作成 (1)
を参考にWindows版のビルドを試した。
// hello.cpp int add(int x, int y) { return x + y; } void out(const char* adrs, const char* name) { printf("こんにちは、私は %s の %s です。\n", adrs, name); }
// helloWrapper.cpp #include "Python.h" extern int add(int, int); extern void out(const char*, const char*); void _declspec(dllexport) inithello(void); PyObject* hello_add(PyObject* self, PyObject* args) { int x, y, g; if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL; g = add(x, y); return Py_BuildValue("i", g); } PyObject* hello_out(PyObject* self, PyObject* args, PyObject* kw) { const char* adrs = NULL; const char* name = NULL; static char* argnames[] = {"adrs", "name", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kw, "|ss", argnames, &adrs, &name)) return NULL; out(adrs, name); return Py_BuildValue(""); } static PyMethodDef hellomethods[] = { {"add", (PyCFunction )hello_add, METH_VARARGS}, {"out", (PyCFunction )hello_out, METH_VARARGS | METH_KEYWORDS}, {NULL}, }; void inithello() { Py_InitModule("hello", hellomethods); }
// hello.def
EXPORTS
inithello
VisualStudioのコマンドプロンプトから以下を実行してビルド。
> cl /LD /IC:/Python27/include hello.c helloWrapper.c C:/Python27/libs/python27.lib hello.def > move hello.dll hello.pyd
あとは、普通にimport helloして使える。
64bitアプリ
- 64bitプログラミングで気をつけること
ユーザ領域のメモリは、8TBか。今後、8EB(エクサバイト)まで使えるようになるのだろうか。
結局、OSによって制限されるみたい。
メモ
LPWSTR型変数にCString文字列を渡す方法。
CString cs; LPWSTR lpwstr = const_cast<LPTSTR>(static_cast<LPCTSTR>(cs));
PythonのReportLabのソースをpy2exeでexe化する際のメモ
setup.pyのsetup()のoptionsでpackagesを指定する必要がある。
# coding: cp932 # setup.py from distutils.core import setup import py2exe packages=[ 'reportlab', 'reportlab.graphics.charts', 'reportlab.graphics.samples', 'reportlab.graphics.widgets', 'reportlab.graphics.barcode', 'reportlab.graphics', 'reportlab.lib', 'reportlab.pdfbase', 'reportlab.pdfgen', 'reportlab.platypus',] py2exe_options = { "compressed": 1, "optimize": 2, "bundle_files": 1, "packages": packages} setup( options = {"py2exe": py2exe_options}, console = [ {"script" : "hoge.py"}], zipfile = None)