ホーム » Linux (ページ 2)
「Linux」カテゴリーアーカイブ
TS-253D セットアップ – 6
以前調べた時は,Synology NAS と同様に面倒だと思ったが…
検索すると
QNAPにEntware-stdをインストールする
Microsoft Windows [Version 10.0.18362.1016] (c) 2019 Microsoft Corporation. All rights reserved. C:\Users\Iwao>\\WDCloud\Public\Document\bat\ssh_ts253d.bat C:\Users\Iwao>cd C:\Users\Iwao\AppData\Local\Temp C:\Users\Iwao\AppData\Local\Temp>ssh -l Iwao -p 22 ts253d Iwao@ts253d's password: [Iwao@TS253D ~]$ gcc -sh: gcc: command not found [Iwao@TS253D ~]$ opkg -sh: opkg: command not found [Iwao@TS253D ~]$ /opt/bin/opkg opkg must have one sub-command argument usage: opkg [options...] sub-command [arguments...] where sub-command is one of: Package Manipulation: update Update list of available packages upgrade <pkgs> Upgrade packages install <pkgs> Install package(s) configure <pkgs> Configure unpacked package(s) remove <pkgs|regexp> Remove package(s) flag <flag> <pkgs> Flag package(s) <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation) ... regexp could be something like 'pkgname*' '*file*' or similar e.g. opkg info 'libstd*' or opkg search '*libop*' or opkg remove 'libncur*' [Iwao@TS253D ~]$ [Iwao@TS253D ~]$ /opt/bin/opkg install gcc Installing gcc (7.4.0-5) to root... Downloading http://bin.entware.net/x64-k3.2/gcc_7.4.0-5_x64-3.2.ipk Installing zlib (1.2.11-3) to root... Downloading http://bin.entware.net/x64-k3.2/zlib_1.2.11-3_x64-3.2.ipk Collected errors: * wfopen: //opt/lib/opkg/info/zlib.control: Permission denied. * extract_archive: Cannot create symlink from ./opt/lib/libz.so to 'libz.so.1': Permission denied. * extract_archive: Cannot create symlink from ./opt/lib/libz.so.1 to 'libz.so.1.2.11': Permission denied. * wfopen: /opt/lib/libz.so.1.2.11: Permission denied. * pkg_write_filelist: Failed to open //opt/lib/opkg/info/zlib.list: Permission denied. * opkg_install_pkg: Failed to extract data files for zlib. Package debris may remain! * opkg_install_cmd: Cannot install package gcc. * opkg_conf_write_status_files: Can't open status file //opt/lib/opkg/status: Permission denied. * opkg_conf_write_status_files: Can't open status file /opt/tmp//opt/lib/opkg/status: Permission denied. [Iwao@TS253D ~]$
sudo -i としても入れない.方法がわからなかったので admin で入ることに.
C:\Users\Iwao>ssh -l Iwao ts253d Iwao@ts253d's password: [Iwao@TS253D ~]$ sudo -i Password: Iwao is not in the sudoers file. This incident will be reported. [Iwao@TS253D ~]$ [Iwao@TS253D ~]$ [Iwao@TS253D ~]$ exit logout Connection to ts253d closed. C:\Users\Iwao>ssh -l admin ts253d admin@ts253d's password: [~] # [~] # [~] #
[/opt/bin] # opkg install gcc
Installing gcc (7.4.0-5) to root...
Downloading http://bin.entware.net/x64-k3.2/gcc_7.4.0-5_x64-3.2.ipk
Installing zlib (1.2.11-3) to root...
Downloading http://bin.entware.net/x64-k3.2/zlib_1.2.11-3_x64-3.2.ipk
Installing libiconv-full (1.11.1-4) to root...
Downloading http://bin.entware.net/x64-k3.2/libiconv-full_1.11.1-4_x64-3.2.ipk
Installing libintl-full (0.19.8.1-2) to root...
Downloading http://bin.entware.net/x64-k3.2/libintl-full_0.19.8.1-2_x64-3.2.ipk
Installing libbfd (2.27-1) to root...
Downloading http://bin.entware.net/x64-k3.2/libbfd_2.27-1_x64-3.2.ipk
Installing libopcodes (2.27-1) to root...
Downloading http://bin.entware.net/x64-k3.2/libopcodes_2.27-1_x64-3.2.ipk
Installing objdump (2.27-1) to root...
Downloading http://bin.entware.net/x64-k3.2/objdump_2.27-1_x64-3.2.ipk
Installing ar (2.27-1) to root...
Downloading http://bin.entware.net/x64-k3.2/ar_2.27-1_x64-3.2.ipk
Installing binutils (2.27-1) to root...
Downloading http://bin.entware.net/x64-k3.2/binutils_2.27-1_x64-3.2.ipk
Configuring zlib.
Configuring libiconv-full.
Configuring libintl-full.
Configuring libbfd.
Configuring libopcodes.
Configuring objdump.
Configuring ar.
Configuring binutils.
Configuring gcc.
There are no *-dev packages in Entware(with few exceptions)!
Please install headers as described in the wiki:
https://github.com/Entware/Entware/wiki
[/opt/bin] #
NAS 環境に Flask のインストール
先ず Debian 環境に Flask をインストール.
pip3 install Flask
サンプルの hello.py を実行,ブラウザで表示.
AS5202T でも同様に Flask をインストール.
pip3 install Flask
Synology NAS では pip のインストールから.
suto opkg install python-pip
suto opkg install python3-pip
Flask をインストールしてみたが …
Iwao@DS116:~/pyt_test/t_flask$ pip3 install Flask Collecting Flask Using cached https://files.pythonhosted.org/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl Collecting click>=5.1 (from Flask) Using cached https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl Collecting itsdangerous>=0.24 (from Flask) Using cached https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl Collecting Werkzeug>=0.15 (from Flask) Using cached https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl Collecting Jinja2>=2.10.1 (from Flask) Using cached https://files.pythonhosted.org/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->Flask) Using cached https://files.pythonhosted.org/packages/b9/2e/64db92e53b86efccfaea71321f597fa2e1b2bd3853d8ce658568f7a13094/MarkupSafe-1.1.1.tar.gz Complete output from command python setup.py egg_info: /opt/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'project_urls' /opt/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'include_package_data' /opt/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'python_requires' usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: -c --help [cmd1 cmd2 ...] or: -c --help-commands or: -c cmd --help error: invalid command 'egg_info' ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-dsqn65uh/MarkupSafe/ Iwao@DS116:~/pyt_test/t_flask$
「 Command python setup.py egg_info faild with error code 1 」で検索 をかけると pip などが古いとある.
次の様にして pip などを更新.
sudo pip install –upgrade pip setuptools
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 の呼出し – 2
先日 Python から C を呼び出す関係を調べていて Synology NAS に Python.h がなかった.
検索 をかけると DSM「パッケージ センター」-「Python3」では python-dev が入ってないらしい.
次の様な手順で python-dev をインストール.
# sudo -i
# cd /var/services/homes/Iwao/
# source ./set_ds_inc.sh
# opkg install python-dev
# opkg install python3-dev
コンパイルで必要なファイルは次の所に入った.
/volume1/@entware-ng/opt/include/python3.6/Python.h
/volume1/@entware-ng/opt/include/python2.7/Python.h
Iwao@DS116:~/pyt_test/call_c/call_cpp/g3d_to$ g++ g3d_to.cpp -Wall -fPIC -o g3d_to.so -shared g3d_to.cpp:9:20: fatal error: Python.h: No such file or directory ^ compilation terminated. Iwao@DS116:~/pyt_test/call_c/call_cpp/g3d_to$ g++ g3d_to.cpp -Wall -fPIC -o g3d_to.so -shared -I /volume1/@entware-ng/opt/include/python3.6/ Iwao@DS116:~/pyt_test/call_c/call_cpp/g3d_to$ ll total 9568 drwxrwxrwx+ 3 Iwao users 4096 Aug 5 10:29 . drwxrwxrwx+ 3 Iwao users 4096 Aug 5 09:50 .. -rwxrwxrwx+ 1 Iwao users 3941375 May 7 18:03 3887.imo -rwxrwxrwx+ 1 Iwao users 1241865 Jul 7 15:13 7801.imo drwxrwxrwx+ 2 Iwao users 4096 Aug 5 10:29 bak -rwxrwxrwx+ 1 Iwao users 1688 Aug 4 15:04 g3d_to.cpp -rwxrwxrwx 1 Iwao users 2654136 Aug 5 10:29 g3d_to.so -rwxrwxrwx+ 1 Iwao users 1644 Aug 7 2019 gons_to.cpp -rwxrwxrwx 1 Iwao users 1931244 Aug 5 10:17 gons_to.out Iwao@DS116:~/pyt_test/call_c/call_cpp/g3d_to$ python3 Python 3.6.2 (default, Jan 11 2018, 10:32:53) [GCC 6.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import g3d_to >>> dir(g3d_to) ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'gons_to', 'load', 'save'] >>> g3d_to.load("./7801.imo") >>> g3d_to.save("./7801.stl") >>> g3d_to.save("./7801.ac") >>> Iwao@DS116:~/pyt_test/call_c/call_cpp/g3d_to$ ll total 10252 drwxrwxrwx+ 3 Iwao users 4096 Aug 5 10:31 . drwxrwxrwx+ 3 Iwao users 4096 Aug 5 09:50 .. -rwxrwxrwx+ 1 Iwao users 3941375 May 7 18:03 3887.imo -rwxrwxrwx+ 1 Iwao users 438369 Aug 5 10:31 7801.ac -rwxrwxrwx+ 1 Iwao users 1241865 Jul 7 15:13 7801.imo -rwxrwxrwx+ 1 Iwao users 254784 Aug 5 10:31 7801.stl drwxrwxrwx+ 2 Iwao users 4096 Aug 5 10:29 bak -rwxrwxrwx+ 1 Iwao users 1688 Aug 4 15:04 g3d_to.cpp -rwxrwxrwx 1 Iwao users 2654136 Aug 5 10:29 g3d_to.so -rwxrwxrwx+ 1 Iwao users 1644 Aug 7 2019 gons_to.cpp -rwxrwxrwx 1 Iwao users 1931244 Aug 5 10:17 gons_to.out Iwao@DS116:~/pyt_test/call_c/call_cpp/g3d_to$
Python から CPP の呼出し – 2
予めデータ(vv_PLF)を作成して,必要に応じてファイルに出力するコード.
vv_PLF* get_vv_PLF (void) { static vv_PLF G_PLF ; return &G_PLF ; } static PyObject* check_rd (PyObject* self, PyObject* args) { vv_PLF* gvv_plf = get_vv_PLF() ; vv_PLF vv_plf_ = ::Check_Revise_deg() ; *gvv_plf = vv_plf_ ; return Py_None ; } static PyObject* dump_svg (PyObject* self, PyObject* args) { vv_PLF* gvv_plf = get_vv_PLF() ; ::Dump_SVG(*gvv_plf) ; return Py_None ; } static PyObject* dump_ipl (PyObject* self, PyObject* args) { vv_PLF* gvv_plf = get_vv_PLF() ; ::Dump_ipl(*gvv_plf) ; return Py_None ; }
Python 側では
* check_rd でデータを作成.
* dump_svg などでデータを出力.
3D データを読み込んで,指定されたファイル名(拡張子により形式を判断)で出力.
static PyObject* load (PyObject* self, PyObject* args) { const char* str_file = NULL ; if (!PyArg_ParseTuple(args,"s",&str_file)) { return NULL ; } tstring g3_file = str_file ; GonsA gnsa = ::To_GonsA(g3_file.c_str()) ; set_GonsA(gnsa) ; return Py_None; } static PyObject* save (PyObject* self, PyObject* args) { const char* str_file = NULL ; if (!PyArg_ParseTuple(args,"s",&str_file)) { return NULL ; } tstring g3_file = str_file ; GonsA* gnsa = ::get_GonsA() ; ::GonsA_To(*gnsa,g3_file.c_str()) ; return Py_None; } static PyObject* gons_to (PyObject* self, PyObject* args) { const char* str_file = NULL ; if (!PyArg_ParseTuple(args,"s",&str_file)) { return NULL ; } tstring g3_file = str_file ; gons_to(g3_file.c_str()) ; return Py_None; }
Python から CPP の呼出し
雰囲気はつかめてきたので,以前作成した cpp を呼んでみることに…
実際の処理部分は C++ のコードだが,呼び出しは C の関数.
また引数もない状態なので,C 関数の system(“a.out”) と呼んでいるのと同様.
#include "i_rd_dbg.hxx" int _tmain(int argc, TCHAR* argv[]) { ::Test_Revise_deg() ; return 0 ; }
#include <Python.h> #include "i_rd_dbg.hxx" #include "messbar.cxx" static PyObject* call_cpp(PyObject* self, PyObject* args) { ::Test_Revise_deg() ; return Py_None ; } static PyMethodDef myMethods[] = { { "_call_cpp_", call_cpp, METH_NOARGS, "call cpp" }, { NULL }, } ; static struct PyModuleDef call_mod = { PyModuleDef_HEAD_INIT, "call_mod", "call_cpp module", -1, myMethods } ; PyMODINIT_FUNC PyInit_call_mod(void) { return PyModule_Create(&call_mod) ; }
コンパイルして import まではできたが,メソッドをうまく呼び出せない.
>>> call_mod.call_cpp()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module ‘call_mod’ has no attribute ‘call_cpp’
>>>
それで,メソッドを表示できないかと思い検索すると,
python でメソッドの一覧を取得する方法
他に dir(call_mod) もあった.
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc $ g++ rvs_sc_w.cpp -o call_mod.so -fPIC -Wall -shared -I /volume1/.@plugins/AppCentral/python3/include/python3.7m/ Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc $ Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc $ 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 call_mod >>> call_mod.call_cpp Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'call_mod' has no attribute 'call_cpp' >>> call_mod.call_cpp() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'call_mod' has no attribute 'call_cpp' >>> obj = call_mod >>> import inspect >>> for m in inspect.getmembers(obj): ... print(m) ... ('__doc__', 'call_cpp module') ('__file__', '/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc/call_mod.so') ('__loader__', <_frozen_importlib_external.ExtensionFileLoader object at 0x7f5a1a35ec50>) ('__name__', 'call_mod') ('__package__', '') ('__spec__', ModuleSpec(name='call_mod', loader=<_frozen_importlib_external.ExtensionFileLoader object at 0x7f5a1a35ec50>, origin='/volume1/home/Iwao/test/test_py/call_c/call_cpp/T_Rvs_sc/call_mod.so')) ('_call_cpp_', <built-in function _call_cpp_>) >>> call_mod._call_cpp_() >>>
Python から C の呼出し
C のコードを Python から呼出せないかと…
Python のドキュメントとしては次の所にある
C や C++ による Python の拡張
Win10 C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\include\Python.h debian10 /usr/include/python3.7/Python.h AS5202T /volume1/.@plugins/AppCentral/python3/include/python3.7m/Python.h Iwao@AS5202T:/volume1/.@plugins/AppCentral/python3/include/python3.7m $ find / -name Python.h /volume1/.@plugins/AppCentral/linux-center/containers/debian10/rootfs/usr/include/python2.7/Python.h /volume1/.@plugins/AppCentral/linux-center/containers/debian10/rootfs/usr/include/python3.7m/Python.h /volume1/.@plugins/AppCentral/python/include/python2.7/Python.h /volume1/.@plugins/AppCentral/python3/include/python3.7m/Python.h
検索 して見つけたもの.
https://www.fsi-embedded.jp/kumico/columns/?cat=python
https://qiita.com/donkonishi/items/b7825b34d0711e336c61
https://www.quark.kj.yamagata-u.ac.jp/~hiroki/python/?id=19
http://owa.as.wakwak.ne.jp/zope/docs/Python/BindingC/
https://cpp-learning.com/?s=”Python+C+API”
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello/bak $ cat hellWrap.c #include <Python.h> extern int add(int, int); extern void out(const char*, const char*); 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", hello_add, METH_VARARGS}, {"out", hello_out, METH_VARARGS | METH_KEYWORDS}, {NULL} }; void initchello(){ Py_InitModule("hello", hellomethods); } Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello/bak $ gcc -fPIC -Wall -c -o hellWrap.o hellWrap.c -I /volume1/.@plugins/AppCentral/python/include/python2.7/ hellWrap.c:30:13: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] {"out", hello_out, METH_VARARGS | METH_KEYWORDS}, ^~~~~~~~~ hellWrap.c:30:13: note: (near initialization for 'hellomethods[1].ml_meth') Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello/bak $ gcc -fPIC -Wall -c -o hellWrap.o hellWrap.c -I /volume1/.@plugins/AppCentral/python/include/python2.7/ Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello/bak $
PyMethodDef の所でエラーとなっていていろいろと探すと,型を指定しているものがあり,それを指定.
https://bty.sakura.ne.jp/wp/archives/83
static PyMethodDef hellomethods[] = { {"add", (PyCFunction)hello_add, METH_VARARGS}, {"out", (PyCFunction)hello_out, METH_VARARGS | METH_KEYWORDS}, {NULL} };
コンパイルは通る様になった.
Python から試そうとすると …
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ gcc -fPIC -Wall -c -o helloWrap.o helloWrap.c -I /volume1/.@plugins/AppCentral/python/include/python2.7/ Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ gcc -fPIC -Wall -shared -o hellomodule.so hello.o helloWrap.o Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ ll total 44 drwxrwxrwx 3 Iwao users 4.0K Jul 29 18:16 ./ drwxrwxrwx 5 Iwao users 4.0K Jul 29 15:31 ../ drwxrwxrwx 2 Iwao users 4.0K Jul 29 18:05 bak/ -rwxrwxrwx 1 Iwao users 188 Jul 29 16:29 hello.c* -rw-r--r-- 1 Iwao users 1.6K Jul 29 16:33 hello.o -rwxrwxrwx 1 Iwao users 910 Jul 29 16:32 helloWrap.BAK* -rwxrwxrwx 1 Iwao users 936 Jul 29 16:37 helloWrap.c* -rw-r--r-- 1 Iwao users 3.1K Jul 29 18:16 helloWrap.o -rwxr-xr-x 1 Iwao users 8.3K Jul 29 18:16 hellomodule.so* Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ 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 hello Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'hello' >>> exit() Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $ python Python 2.7.10 (default, Aug 19 2015, 09:18:54) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import hello Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: dynamic module does not define init function (inithello) >>> exit() Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/hello $
まだ何か違うみたい.
メッセージは inithello がないとなっているので helloWrap.c を見直すと…
void initchello() { … } となっている.
関数名を inithello に変更してビルドすると通った.
2020/07/30
今日は次の所を参考にさせてもらって…
https://cpp-learning.com/python_c_api_step1/
Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ gcc ph_hello.c -o mymodule.so -fPIC -Wall -shared -I /volume1/.@plugins/AppCentral/python3/include/python3.7m/ Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ 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 mymodule Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: dynamic module does not define module export function (PyInit_mymodule) >>> import myModule Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'myModule' >>> Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ cp mymodule.so myModule.so Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ ll total 44 drwxrwxrwx 3 Iwao users 4.0K Jul 30 16:49 ./ drwxrwxrwx 6 Iwao users 4.0K Jul 30 14:59 ../ drwxrwxrwx 2 Iwao users 4.0K Jul 30 16:48 bak/ -rwxr-xr-x 1 Iwao users 8.1K Jul 30 16:49 myModule.so* -rwxr-xr-x 1 Iwao users 8.1K Jul 30 16:47 mymodule.so* -rwxrwxrwx 1 Iwao users 188 Jul 29 16:29 ph_hello.BAK* -rwxrwxrwx 1 Iwao users 1.3K Jul 30 16:27 ph_hello.c* Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $ 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 myModule >>> myModule.helloworld <built-in function helloworld> >>> myModule.helloworld() Hello World >>> >>> >>> >>> Iwao@AS5202T:/volume1/home/Iwao/test/test_py/call_c/py_hello $
コンパイル時の出力ファイル名 mymodule.so が間違っていた.正しくは myModule.so .
そこ には詳しく書かれているが,自分用にメモ.
PyMethodDef の “メソッド名” は Python 側での (モジュール名).(メソッド名) .
同様に PyModuleDef の “モジュール名” は import 時の名称..so の出力ファイル名も対応している必要がある?
文字列なので,異なっていてもコンパイル時のエラーにはならない.
実行時に見つからないなどのエラーとなる.
2020/07/31
続きの内容をやっていて…
https://cpp-learning.com/python_c_api_step2/
Fatal Python error: GC object already tracked
c_list は Python 側で確保しているため Py_DECREF はうまくないのではないか?
Python 負のインデックス
次の所を読ませてもらっていて感じたことを…
けいしゅけのブログ薬局 情報館
https://keisyuke-blogyakkyoku.xyz/python-list-index
今私が習得するのにタイミングや更新サイクルが丁度良いので助かっている.
ASUSTOR NAS AS5202T に SSH 接続して Python を操作.
C:\Program Files\Microsoft Office\Office14>cd C:\Users\Iwao\AppData\Local\Temp C:\Users\Iwao\AppData\Local\Temp>ssh -l Iwao -p 22 192.168.1.75 Password: Iwao@AS5202T:/volume1/home/Iwao $ python Python 2.7.10 (default, Aug 19 2015, 09:18:54) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a=[0,1,2,3,4] >>> print(a[0]) 0 >>> print(a[4]) 4 >>> print(a[5]) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> print(a[-1]) 4 >>> print(a[-4]) 1 >>> print(a[-5]) 0 >>> print(a[-6]) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>>
似たようなことを C++ でやろうとすると次の様な感じ.
std::cout << a[a.size()-1] ;
この様な記述ができるからか,今まで負のインデックスの必要性は感じたことがなかった.
また配列の検索で該当するものがないと -1 を返す様なコードもよく書く.
インデックスを変数で持つ時は?
いろいろ考えると個人的にはあまり使わないのではないかと思う.
Python などをやればもっと有効な使い方が出てくるのか?
GLUT でのメニュー
GLUT を使用してのテストコードで,起動後データを切替える方法がないかと…
メニューの利用で何とかできるか?
glutCreateMenu などで検索したがわかりやすい情報が少なかった.
https://seesaawiki.jp/w/mikk_ni3_92/d/%b4%f0%cb%dc%ca%d418
https://www.jstage.jst.go.jp/article/itej/67/5/67_417/_pdf
http://opengl.jp/glut/section06.html
以前作成した雛型に対して追加.メニュー部分はほぼリンク先のコードのまま.
https://jml.mish.work/index.php/cpp/cb-glut.html
#include "glut_cb.hxx" #include <iostream> void cb_menu (int val) { std::cout << "menu val=" << val << std::endl ; } int main(int argc, char* argv[]) { ::glutInitWindowPosition(200,100) ; ::glutInitWindowSize (600,400) ; ::glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) ; ::glutInit (&argc,argv) ; ::glutCreateWindow (argv[0]) ; ::glutReshapeFunc (cb_resize) ; ::glutDisplayFunc (cb_display) ; ::glutKeyboardFunc (cb_keyboard) ; ::glutMouseFunc (cs_mouse) ; { ::glutCreateMenu(cb_menu) ; ::glutAddMenuEntry("name 1",1) ; ::glutAddMenuEntry("name 2",2) ; ::glutAddMenuEntry("name 3",3) ; ::glutAttachMenu(GLUT_RIGHT_BUTTON) ; } ::cb_init () ; ::glutMainLoop () ; return 0 ; }
結局はこの方法ではなく,予めリスト化して キー入力 により切り替える方法に.
Debian 環境に pip のインストール
PyOpenGL を使おうとして pip コマンドを打つと,コマンドがない.
どうも pip が入っていないみたいで,次の様に入力してインストール.
sudo apt install python-pip
更に PyOpenGL のインストール.
pip install PyOpenGL
pip3 のインストールは
sudo apt install python3-pip
C:\WINDOWS\System32>cd C:\Users\Iwao\AppData\Local\Temp C:\Users\Iwao\AppData\Local\Temp>ssh -l admin -p 22 lxcdebian10 admin@lxcdebian10's password: Linux lxcdebian10 4.14.x #1 SMP Wed May 13 00:37:48 CST 2020 x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Thu Jul 9 10:51:02 2020 from fe80::ed6f:4991:21c9:1882%eth0 admin@lxcdebian10:~$ cat /usr/bin/pip #!/usr/bin/python # GENERATED BY DEBIAN import sys # Run the main entry point, similarly to how setuptools does it, but because # we didn't install the actual entry point from setup.py, don't use the # pkg_resources API. from pip._internal import main if __name__ == '__main__': sys.exit(main()) admin@lxcdebian10:~$ cat /usr/bin/pip3 #!/usr/bin/python3 # GENERATED BY DEBIAN import sys # Run the main entry point, similarly to how setuptools does it, but because # we didn't install the actual entry point from setup.py, don't use the # pkg_resources API. from pip._internal import main if __name__ == '__main__': sys.exit(main()) admin@lxcdebian10:~$
AS5202T Debian 10 に Image Magick
Raspberry Pi などと同様 に
sudo apt install imagemagick
特に難しいことはない.
AS5202T Debian 10 と VC
VC で ASUSTOR NAS Linux Center の Debian 10 Desktop を使用するための設定.
SSH 接続を可能にして gcc などをインストールしておく必要がある.
Linux development with C++ in Visual Studio
VC の「ツール」-「オプション」,「クロスプラットフォーム」-「接続マネージャー」-「追加」.
SSH 接続する時の情報を設定する.
追加後「接続マネージャー」-「リモートヘッダー…」で「更新」するとエラー.
エラーが発生しました。Could not start the ‘rsync’ command on the remote host, please install it using your system package manager. Please see https://aka.ms/AA23jat for troubleshooting。詳細については、C:\Users\Iwao\AppData\Local\Temp\vslinux_header_update_log.txt を参照してください。トラブルシューティングを行うには、https://aka.ms/AA23jat をご覧ください。
この部分の対応方法はよくわからない.
これでビルドすれば大丈夫なはずだが,何故かエラー.
VC を再起動したり,プロジェクトを読み直したりしていたら通る様になった.
AS5202T Debian 10
以前 インストールしてそのままになっていた環境.
その時わからなかったのが「タイムゾーン」の設定.
次の所を参考にさせてもらって設定.
Debian 10 busterをインストールして最初に行う設定と確認 10項目
共通のソースなどを参照するために…
Linux から Windows 環境への接続
接続先は,最終的には変更する予定.
弦と矢と半径 – 11
JavaScript 版で Canvas を使用する様にしてみた.
https://mish.myds.me/Test/blog/r_cs/b/
弦と矢と半径 – 10
JavaScript 数値入力で更新.
<body> <pre> c : <input type='number' id='in__c' value='8' style='width:100px;' step='any' /> <span id='out_c'> </span> <br /> s : <input type='number' id='in__s' value='2' style='width:100px;' step='any' /> <span id='out_s'> </span> <br /> r : <input type='number' id='in__r' value='5' style='width:100px;' step='any' /> <span id='out_r'> </span> <br /> </pre> <img src='r_cs.png' width='300px'> <script> function r_cs (c , s) { return ( ( c*c ) / ( 8*s ) + s/2 ) ; } function s_rc (r , c) { return ( r-Math.sqrt(r*r - (c/2)*(c/2) ) ) ; } function c_rs (r , s) { return ( Math.sqrt(r*r - (r-s)*(r-s) )*2 ) ; } function f_c_rs () { var oc = document.querySelector("#out_c") ; var os = document.querySelector("#out_s") ; var or = document.querySelector("#out_r") ; var ic = document.querySelector("#in__c") ; var is = document.querySelector("#in__s") ; var ir = document.querySelector("#in__r") ; var fc = parseFloat(ic.value) ; var fs = parseFloat(is.value) ; var fr = parseFloat(ir.value) ; oc.textContent = "" ; os.textContent = "" ; or.textContent = "" ; os.textContent = s_rc(fr,fc) ; or.textContent = r_cs(fc,fs) ; } // ... { var ec = document.getElementById("in__c") ; var es = document.getElementById("in__s") ; var er = document.getElementById("in__r") ; ec.addEventListener("keyup",f_c_rs, false) ; es.addEventListener("keyup",f_s_rc, false) ; er.addEventListener("keyup",f_r_cs, false) ; } </script> </body>
弦と矢と半径 – 9
JavaScript で.
<body> c : <input type='number' id='in__c' value='80.0' size='10' step='any' /><input type='submit' value='c_rs' id='c_rs' /> <span id='out_c'> </span> <br /> s : <input type='number' id='in__s' value='20.0' size='10' step='any' /><input type='submit' value='s_rc' id='s_rc' /> <span id='out_s'> </span> <br /> r : <input type='number' id='in__r' value='50.0' size='10' step='any' /><input type='submit' value='r_cs' id='r_cs' /> <span id='out_r'> </span> <br /> <img src='r_cs.png' width='300px'> <script> function r_cs (c , s) { return ( ( c*c ) / ( 8*s ) + s/2 ) ; } function s_rc (r , c) { return ( r-Math.sqrt(r*r - (c/2)*(c/2) ) ) ; } function c_rs (r , s) { return ( Math.sqrt(r*r - (r-s)*(r-s) )*2 ) ; } function f_c_rs () { var oc = document.querySelector("#out_c") ; var os = document.querySelector("#out_s") ; var or = document.querySelector("#out_r") ; var ic = document.querySelector("#in__c") ; var is = document.querySelector("#in__s") ; var ir = document.querySelector("#in__r") ; var fc = parseFloat(ic.value) ; var fs = parseFloat(is.value) ; var fr = parseFloat(ir.value) ; oc.textContent = "" ; os.textContent = "" ; or.textContent = "" ; oc.textContent = c_rs(fr,fs) ; } // ... { var ec = document.getElementById("c_rs") ; var es = document.getElementById("s_rc") ; var er = document.getElementById("r_cs") ; ec.addEventListener("click",f_c_rs, false) ; es.addEventListener("click",f_s_rc, false) ; er.addEventListener("click",f_r_cs, false) ; } </script> </body>
弦と矢と半径 – 8
Python を使用しないで PHP のみで.
<?php function r_cs ($c , $s) { return ( ( $c*$c ) / ( 8 *$s ) + $s/2 ) ; } function s_rc ($r , $c) { return ( $r-sqrt($r*$r - ($c/2)*($c/2) ) ) ; } function c_rs ($r , $s) { return ( sqrt($r*$r - ($r-$s)*($r-$s) )*2 ) ; } ?>
弦と矢と半径 – 7
input タグで number を使用している部分.
step=’0.001′ などとすれば小数値を入力できるが,欲しい動作ではない.
例えば 1.23456789 など任意の値を入力可能にしたかった.
step=’any’ と指定することで小数部を入力できるようになる.
<input type=’number’ name=’c’ value=’1.234567890′ size=’15’ step=’any’ />
https://mish.myds.me/…/r_cs/7/
弦と矢と半径 – 6
Python のコードを修正して「結果のみ」を出力する様に変更.
import sys import math def r_cs (c , s) : return ( ( c*c ) / ( 8 *s ) + s/2 ) def s_rc (r , c) : return ( r-math.sqrt(r*r - (c/2)*(c/2) ) ) def c_rs (r , s) : return ( math.sqrt(r*r - (r-s)*(r-s) )*2 ) cmd = sys.argv[1] p1 = float(sys.argv[2]) p2 = float(sys.argv[3]) #print (cmd + " " + str(p1) + " " + str(p2)) if cmd == "r_cs" : print (r_cs(p1,p2)) elif cmd == "s_rc" : print (s_rc(p1,p2)) elif cmd == "c_rs" : print (c_rs(p1,p2)) #else : print ("error") #print ("")
PHP .
あまり綺麗なコードでないので,ここには張り付けていません.リンク先を見てください.
https://mish.myds.me/…/r_cs/6/
弦と矢と半径 – 5
PHP から Python の呼び出しで,Python のコマンドライン引数を使用する.
<?php echo ("call python\n") ; echo ("\n") ; system ("python r_cs.py r_cs 6 1") ; system ("python r_cs.py s_rc 5 6") ; system ("python r_cs.py c_rs 5 1") ; echo ("\n") ; system ("python r_cs.py r_cs 8 2") ; system ("python r_cs.py s_rc 5 8") ; system ("python r_cs.py c_rs 5 2") ; ?>
Python のコードは,
import sys import math def r_cs (c , s) : return ( ( c*c ) / ( 8 *s ) + s/2 ) def s_rc (r , c) : return ( r-math.sqrt(r*r - (c/2)*(c/2) ) ) def c_rs (r , s) : return ( math.sqrt(r*r - (r-s)*(r-s) )*2 ) cmd = sys.argv[1] p1 = float(sys.argv[2]) p2 = float(sys.argv[3]) print (cmd + " " + str(p1) + " " + str(p2)) if cmd == "r_cs" : print (r_cs(p1,p2)) elif cmd == "s_rc" : print (s_rc(p1,p2)) elif cmd == "c_rs" : print (c_rs(p1,p2)) else : print ("error") print ("")
Python がよくわかっていないので,いろいろなエラーが…
https://mish.myds.me/…/r_cs/5/