仮想マシンの Fedora 32 を 34 に更新.
動作を確認していると,GLUT を使用した .out がうまく動作しない.
起動はしているが,ウィンドウが表示されない?
コンパイルなどはできている.
実行するとコンソールにはウィンドウを表示する前の部分は表示される.
いろいろやったがダメで,再起動.
うまく表示する様になった.
この画面は、簡易表示です
仮想マシンの Fedora 32 を 34 に更新.
動作を確認していると,GLUT を使用した .out がうまく動作しない.
起動はしているが,ウィンドウが表示されない?
コンパイルなどはできている.
実行するとコンソールにはウィンドウを表示する前の部分は表示される.
いろいろやったがダメで,再起動.
うまく表示する様になった.
Win9x が多く存在していた頃のコードの MFC を使用しない方法での書き直し.
前のものは次の様になっている.
// J048221 SDK32:GetDiskFreeSpace と GetDiskFreeSpaceEx について
typedef BOOL (WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER,PULARGE_INTEGER, PULARGE_INTEGER);
// ..
fResult = ::GetDiskFreeSpace(pszDrive,&SectorsPerCluster,&BytesPerSector,&FreeClusters,&TotalClusters) ;
if (fResult) {
TotalBytes = (__int64)TotalClusters * SectorsPerCluster * BytesPerSector ;
TotalFreeBytes = (__int64)FreeClusters * SectorsPerCluster * BytesPerSector ;
FreeBytesAvailable = TotalFreeBytes ;
}
// ..
{
P_GDFSE pGetDiskFreeSpaceEx = NULL;
#ifdef UNICODE
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")),"GetDiskFreeSpaceExW") ;
#else
pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")),"GetDiskFreeSpaceExA") ;
#endif
if (pGetDiskFreeSpaceEx) {
fResult = pGetDiskFreeSpaceEx (pszDrive,
(PULARGE_INTEGER)&FreeBytesAvailable,
(PULARGE_INTEGER)&TotalBytes,
(PULARGE_INTEGER)&TotalFreeBytes);
}
}
https:/
Windows 環境では次の様な感じ?
class DiskFree {
public:
DiskFree () { Free = Total = 0 ; }
public:
u_64 Free ;
u_64 Total ;
} ;
DiskFree GetDiskFree (LPCTSTR path)
{
DiskFree df ;
u_64 freeC = 0 ;
u_64 free = 0 ;
u_64 total = 0 ;
if (::GetDiskFreeSpaceEx(path,(PULARGE_INTEGER)&freeC,(PULARGE_INTEGER)&total,(PULARGE_INTEGER)&free)) {
df.Free = free ;
df.Total= total;
}
return df ;
}
Linux 環境では ::statvfs が使えるみたいで,次の様なコードで取得してデバッガで確認.
#include <iostream>
#include <sys/statvfs.h>
int main()
{
struct statvfs vfs = { 0 } ;
::statvfs(".", &vfs) ;
return 0 ;
}
vfs {...} statvfs
f_bsize 4096 unsigned long
f_frsize 4096 unsigned long
f_blocks 1452408524 __fsblkcnt_t
f_bfree 200259802 __fsblkcnt_t
f_bavail 199469255 __fsblkcnt_t
f_files 183001088 __fsfilcnt_t
f_ffree 181248356 __fsfilcnt_t
f_favail 181248356 __fsfilcnt_t
f_fsid 3941329918106335254 unsigned long
f_flag 4096 unsigned long
f_namemax 255 unsigned long
__f_spare int [6]
[0] 0 int
[1] 0 int
[2] 0 int
[3] 0 int
[4] 0 int
[5] 0 int
Iwao@AS5202T:/volume1/home/Iwao $ cd gcc_test/Test/t_linux/T_vfs/T_s_vfs/
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $ ll
total 12
drwxrwxrwx 2 Iwao users 4.0K Jul 1 22:01 ./
drwxrwxrwx 4 Iwao users 4.0K Jul 1 22:00 ../
-rwxrwxrwx 1 Iwao users 581 Jul 1 21:55 T_s_vfs.cpp*
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $ cat T_s_vfs.cpp
#include <iostream>
#include <clocale>
#include <sys/statvfs.h>
#include "_tdefine.hxx"
#include "t_tstrng.hxx"
int _tmain (int argc,TCHAR* argv[])
{
_tsetlocale(LC_ALL,_T("")) ;
{
struct statvfs vfs = { 0 } ;
if (::statvfs(".", &vfs) == 0) {
u_64 free = vfs.f_bavail * vfs.f_frsize ;
u_64 total= vfs.f_blocks * vfs.f_frsize ;
std::tout << ::To_tstring_Ki(free) << _T(" / ") << ::To_tstring_Ki(total) << std::endl ;
std::tout << ::To_tstring_cs(free) << _T(" / ") << ::To_tstring_cs(total) << std::endl ;
}
}
return 0 ;
}
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $ g++ T_s_vfs.cpp -Wall
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $ ./a.out
760.90 G / 5.41 T
817,005,420,544 / 5,949,065,314,304
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/T_vfs/T_s_vfs $
ASUSTOR NAS 上の Debian と VC 2019 を使用してのビルドとデバッグ.
VC で次の様なエラーになっている.
lxcdebian10 にツールがありません: gdb rsync zip
次の所にある様に必要なものをインストール.
Linux development with C++ in Visual Studio
丁度 1 年前にやっていた みたいで,その時より 安定したか?
スタックを使用した次の様なコード.
stack_s so ;
エラー情報を指定する様に変更してビルド.
i_error ie ; stack_s so(&ie) ;
--------------------Configuration: t_stack - Win32 Debug-------------------- Compiling... t_stack.cpp d:\document\vs\vs\1998\t_stack\t_stack\t_stack.cpp(14) : error C2664: '__thiscall stack_s::stack_s(const class stack_s &)' : cannot convert parameter 1 from 'class i_error *' to 'const class stack_s &' Reason: cannot convert from 'class i_error *' to 'const class stack_s' No constructor could take the source type, or constructor overload resolution was ambiguous Error executing cl.exe. T_stack.exe - 1 error(s), 0 warning(s)
C2664 になってしまう.
class stack_b { public: stack_b (i_error* i_err=NULL) { ie = &tmp_ie ; if(i_err != NULL) { ie = i_err ; } } protected: i_error tmp_ie ; i_error* ie ; } ; class stack_s : public stack_b { public: // stack_s (i_error* i_err=NULL) : stack_b (i_err) { ; } public: virtual bool push (c_tstring& s) ; virtual tstring pop (void) ; virtual tstring last (void) ; protected: s_tstring stck_s ; } ;
コンソール AP では,標準入力を使用していた.
#include "quotm.hxx"
#include "existff.hxx"
inline bool Test (void)
{
while(true) {
tstring str ;
{
tstring buf ;
buf.resize(1000) ;
std::terr << _T("file ...=") ;
std::tin.getline(&buf[0],std::streamsize(buf.size())) ;
str = buf.c_str() ;
if (str == _T("q")) { break ; }
else if (str == _T("Q")) { break ; }
str = ::QuotM_Del_All(str) ;
if (str.empty()) { continue ; }
}
{
if (::File_IsNothing(str)) { continue ; }
std::tout << str << std::endl ;
}
}
return true ;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
{
Test() ;
}
return 0 ;
}
以前作成した CFileDialog を使用したもの.
#include <afxwin.h>
#include "F_Dialog.hxx"
#include "quotm.hxx"
inline bool Test (void)
{
while(true) {
tstring str ;
{
str = ::FD_GetOpenFile(_T("./")) ;
if (str.empty()) { break ; }
}
{
if (::File_IsNothing(str)) { continue ; }
std::tout << str << std::endl ;
}
}
return true ;
}
CWinApp theApp;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
std::terr << _T("error : AfxWinInit") << std::endl ;
nRetCode = 1;
}
else {
Test() ;
}
return nRetCode;
}
////////////////////////////////////////////////////////////////////////////////////////
#include <AfxExt.h>
#include "BrowseFF.cxx"
「開く」ダイアログを表示するだけのコード.
{
OPENFILENAME ofn = { 0 } ;
ofn.lStructSize = sizeof(OPENFILENAME) ;
if (!::GetOpenFileName(&ofn)) { return tstring() ; }
// ...
}
OPENFILENAME の指定が複雑なので,使いそうな内容でまとめてみた.
OPENFILENAME | |
lStructSize | sizeof(OPENFILENAME) |
hwndOwner | NULL の時,モードレスになる. CFileDialog では DoModal で求めている. |
lpstrFile | 選択されたファイル名が入る. OFN_ALLOWMULTISELECT の場合は,下のメモリイメージ. |
nMaxFile | lpstrFile で確保している領域の文字数 |
OFN_ALLOWMULTISELECT の場合の結果の lpstrFile .
0x02C00078 44 3a 5c 44 6f 63 75 6d 65 6e 74 5c 56 53 5c 56 D:\Document\VS\V 0x02C00088 53 5c 32 30 30 35 5c 54 5f 4f 70 65 6e 46 5c 54 S\2005\T_OpenF\T 0x02C00098 5f 4f 46 5f 4d 46 43 54 5f 4f 46 5f 4d 46 43 _OF_MFC.T_OF_MFC 0x02C000A8 2e 42 41 4b 54 5f 4f 46 5f 4d 46 43 2e 63 70 .BAK.T_OF_MFC.cp 0x02C000B8 70 54 5f 4f 46 5f 4d 46 43 2e 76 63 70 72 6f p.T_OF_MFC.vcpro 0x02C000C8 6a 54 5f 4f 46 5f 4d 46 43 2e 76 63 70 72 6f j.T_OF_MFC.vcpro 0x02C000D8 6a 2e 5a 31 37 30 53 30 2e 49 77 61 6f 2e 75 73 j.Z170S0.Iwao.us 0x02C000E8 65 72 00 00 00 00 00 00 00 00 00 00 00 00 er.............. 0x027E6B50 44 00 3a 00 5c 00 44 00 6f 00 63 00 75 00 6d 00 65 00 6e 00 74 00 5c 00 56 00 53 00 5c 00 56 00 D:\Document\VS\V 0x027E6B70 53 00 5c 00 32 00 30 00 30 00 35 00 5c 00 54 00 5f 00 4f 00 70 00 65 00 6e 00 46 00 5c 00 54 00 S\2005\T_OpenF\T 0x027E6B90 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 00 00 54 00 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 _OF_MFC.T_OF_MFC 0x027E6BB0 2e 00 42 00 41 00 4b 00 00 00 54 00 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 2e 00 63 00 70 00 .BAK.T_OF_MFC.cp 0x027E6BD0 70 00 00 00 54 00 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 2e 00 76 00 63 00 70 00 72 00 6f 00 p.T_OF_MFC.vcpro 0x027E6BF0 6a 00 00 00 54 00 5f 00 4f 00 46 00 5f 00 4d 00 46 00 43 00 2e 00 76 00 63 00 70 00 72 00 6f 00 j.T_OF_MFC.vcpro 0x027E6C10 6a 00 2e 00 5a 00 31 00 37 00 30 00 53 00 30 00 2e 00 49 00 77 00 61 00 6f 00 2e 00 75 00 73 00 j.Z170S0.Iwao.us 0x027E6C30 65 00 72 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 er.............. lpstrFile 0x02c10078 "D:\Document\VS\VS\2005\T_OpenF\T_OF_MFC\ReadMe.txt" char *
デバッガで OPENFILENAME_SIZE_VERSION_400 となる様にしたもの.
MFC 9 で bVistaStyle を FALSE とすると
VC 8 exe と同様の,左側がフォルダツリーではない表示のもの.
デフォルトの TRUE だと
COM を利用したもの?
Common Item Dialog
今度は MFC を使用しない方法.
::GetOpenFileName を使用する方法で OFN_ALLOWMULTISELECT を指定すると実行されなかった.
見た目のエラーにはならない?が,API を呼んで FALSE で抜けてくる.
::CommDlgExtendedError で調べると FNERR_INVALIDFILENAME だった.
存在しないファイル名などを与えるとうまくないみたい.
まだ何か足りないみたいで Win3.x 頃の表示になってしまった.
選択したファイル名などもスペースで区切られている.
OFN_ALLOWMULTISELECT を指定する時は OFN_EXPLORER も指定する必要があるみたい.
次の所に書かれていた.
OPENFILENAMEW structure (commdlg.h)
OFN_ALLOWMULTISELECT | OFN_EXPLORER の場合 lpstrFile に存在しないファイル名でも問題なさそう.
複数選択可能な「開く」ダイアログのコードは次の様な感じ.
tstring GetOpenFile (LPCTSTR default_name=_T("./"))
{
HWND hwnd = ::GetConsoleHwnd() ;
tstring str_file = default_name ;
str_file.resize(260*100) ;
OPENFILENAME ofn = { 0 } ;
{
ofn.lStructSize = sizeof(OPENFILENAME) ;
ofn.hwndOwner = hwnd ;
ofn.nMaxFile = DWORD(str_file.size()) ;
ofn.lpstrFile = &str_file[0] ;
ofn.Flags = OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_HIDEREADONLY ;
}
if (!::GetOpenFileName(&ofn)) {
DWORD err = ::CommDlgExtendedError() ;
std::terr << ::To_tstring(u_32(err),16) << std::endl ;
return tstring() ;
}
tstring sel_file ;
{
sel_file = ofn.lpstrFile ;
// ...
}
return sel_file ;
}
拡張子のフィルタなどは指定していない.また,選択されたファイルを取得するコードもこれから.
選択されたファイルを取得するコード.
tstring String_Change (c_tstring& str_,const TCHAR src,const TCHAR dst)
{
tstring str = str_ ;
for (size_t index=0 ; index<str.size() ; index++) {
TCHAR ch = str[index] ;
if (ch == src) {
str[index] = dst ;
}
}
return str ;
}
tstring sel_file ;
{
sel_file = ofn.lpstrFile ;
sel_file = str_file ;
sel_file = ::String_Change(ofn.lpstrFile,_T('\x0'),_T('\n')) ;
sel_file = ::String_Change(str_file, _T('\x0'),_T('\n')) ;
sel_file = ::String_TrimBoth(sel_file) ;
}
ofn.lpstrFile は ASCIIZ となってしまうので,str_file をそのまま使用する必要がある.
Win3.x 形式の lpstrFile のダンプ.
0x02311F00 0043 003a 005c 0055 0053 0045 0052 0053 005c 0050 0055 0042 004c 0049 0043 005c C.:.\.U.S.E.R.S.\.P.U.B.L.I.C.\.
0x02311F20 0044 004f 0043 0055 004d 0045 004e 0054 0053 005c 0020 0042 0061 0063 006b 0043 D.O.C.U.M.E.N.T.S.\. .B.a.c.k.C.
0x02311F40 0050 002e 0062 0061 0074 002e 006c 006e 006b 0020 0057 0049 004e 0031 0030 002d P...b.a.t...l.n.k. .W.I.N.1.0.-.
0x02311F60 007e 0031 002e 004c 004e 004b 0020 005a 0031 0037 0030 0044 004f 007e 0031 002e ~.1...L.N.K. .Z.1.7.0.D.O.~.1...
0x02311F80 004c 004e 004b 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 L.N.K...........................
区切りがスペースになるため,ファイル名などにスペースが含まれている場合は 8.3 形式 になる.
lpstrFilter を指定.
tstring filter ;
filter += _T("Executable Files (*.exe)|*.exe|") ;
filter += _T("All Files (*.*)|*.*|") ;
filter += _T("|") ;
filter = ::String_Change(filter,_T('|'),_T('\x0')) ;
OPENFILENAME ofn = { 0 } ;
{
ofn.lStructSize = sizeof(OPENFILENAME) ;
// ...
ofn.lpstrFilter = &filter[0] ;
}
先日 Linux Station がうまく動作しなくなったように思ったが…
ファームアップデート後,試しに起動するとうまくいった.
先日は,メモリが足りなかったとかだったのか?
GLUT を使える様に インストール .
sudo apt install freeglut3 freeglut3-dev
ASUSTOR NAS Linux Center Debian 環境の a.out を実行.
2020/12/02 0 時過ぎ,個人的に書いているコードがうまく動作していないことに気づいた.
全てではないが,日時の表示が GMT になってしまっている?
うまく動作しているものもあり,次のものは正しく表示される.
この様になった心当たりはある.
昨日 opkg の update と upgrade ,opkg install gcc などを行ったことが影響している?
tree … -D や cal なども GMT になっている.
どこが影響しているかを調べるために少しコードを書いてみた.
#include <clocale> #include <iostream> #include "_tdefine.hxx" #include "timefmt.hxx" int _tmain (int argc,TCHAR* argv[]) { _tsetlocale(LC_ALL,_T("")) ; { tstring now_time = ::Now_Format() ; std::tout << _T("::Now_Format() \t") << now_time << std::endl ; } { tstring gmt_time = ::Now_FormatGMT() ; std::tout << _T("::Now_FormatGMT()\t") << gmt_time << std::endl ; } return 0 ; }
Win10 環境では意図した動作.
Microsoft Windows [Version 10.0.18363.1198] (c) 2019 Microsoft Corporation. All rights reserved. C:\Users\Iwao>\\TestXP\C_Temp\Test_cpp\t_mtime\t_localt\Release.060\t_localt.exe ::Now_Format() 2020/12/02 10:22:34 ::Now_FormatGMT() 2020/12/02 01:22:34 C:\Users\Iwao>
DS116 では localtime が正しく求まっていない.
Iwao@DS116:~/gcc_test/Test/t_linux/t_mtime/t_localt$ ll total 44 drwxrwxrwx+ 2 Iwao users 4096 Dec 2 10:19 . drwxrwxrwx+ 5 Iwao users 4096 Dec 2 10:03 .. -rwxrwxrwx 1 Iwao users 14188 Dec 2 10:19 a.out -rwxrwxrwx+ 1 Iwao users 892 Dec 2 10:02 t_localt.BAK -rwxrwxrwx+ 1 Iwao users 917 Dec 2 10:17 t_localt.cpp -rwxrwxrwx+ 1 Iwao users 4476 Dec 2 10:02 t_localt.dsp Iwao@DS116:~/gcc_test/Test/t_linux/t_mtime/t_localt$ ./a.out ::Now_Format() 2020/12/02 01:24:30 ::Now_FormatGMT() 2020/12/02 01:24:30 Iwao@DS116:~/gcc_test/Test/t_linux/t_mtime/t_localt$
上の WebGL で現在日時を表示ている .out は,2017/07/19 にコンパイルしたもの.
他の .out で,古いものは正しく動作し,2020/02 のものはうまくない.
どこかに設定などがあるのか?
2020/12/09
GMT と同じ扱いになり困っていたのは,個人的なコードの i_drawlg.hxx .
今日のファイルを求める所で 24*60*60 を加えることで対応.
今まで Windows 上で動かしていた OpenMP 対応のコードを,NAS 上で…
QNAP NAS 上にソースをコピーしてコンパイルすると
[Iwao@TS253D T_cmb_f]$ g++ T_cmb_f.cpp -Wall -fopenmp In file included from T_cmb_f.cpp:12:0: /share/Public/CloudD/GoogleD/Develop/_.SRC/Test/t_g3d_et.hpp:12:10: fatal error: omp.h: No such file or directory #include <omp.h> ^~~~~~~ compilation terminated. [Iwao@TS253D T_cmb_f]$
もう少し単純なコードで…
#ifdef _OPENMP #include <omp.h> #endif #include <clocale> #include <iostream> #include "i_define.hxx" bool Test (void) { #ifdef _OPENMP #pragma omp parallel for #endif for (long index=0 ; index<20 ; index++) { #ifdef _OPENMP #pragma omp critical (wait) #endif std::cout << index << std::endl ; } return true ; } int _tmain (int argc,TCHAR* argv[]) { _tsetlocale(LC_ALL,_T("")) ; { ::Test() ; } return 0 ; }
[Iwao@TS253D T_cmb_f]$ cd ../T_omp/ [Iwao@TS253D T_omp]$ g++ T_omp.cpp -Wall -fopenmp T_omp.cpp:10:10: fatal error: omp.h: No such file or directory #include <omp.h> ^~~~~~~ compilation terminated. [Iwao@TS253D T_omp]$
これとは直接関係ないが,
普通にコンパイルした T_cmb_f を QNAP NAS で実行すると,CPU などの温度が正しく表示されない状態に陥った.
2021/05/24
Ubuntu Linux Station ではうまく動作する.
Linux Center でも動作する.
今までファイルの更新日時の変更は使っていた.
https:/
が,このツールでフォルダの更新日時を変更しようとするとできない.
コードは CFile::SetStatus を使用している.
CFileStatus fs ; CFile::GetStatus(file,fs) ; fs.m_mtime = newTime ; CFile::SetStatus(file,fs) ;
CFile::SetStatus の中(…\atlmfc\src\mfc\filest.cpp)を見ると ::SetFileTime を使用している.
また Linux などでも使える utime(…\crt\src\utime.c)も ::SetFileTime を使っている.
VC 6 で作成したものをデバッガで追いかけると ::CreateFile で 0xffffffff が返ってきている.
ASUSTOR NAS で試すと,特に問題なく変更できる.
Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/t_mtime/t_utime $ cat t_utime.cpp #include <clocale> #include "i_trace.hxx" #include "filetime.hxx" #include "itls_tmp.hxx" #include "c_which.hxx" int _tmain (int argc,TCHAR* argv[]) { _tsetlocale(LC_ALL,_T("")) ; { tstring temp_path = ::Get_i_Tools_tmp_date() ; tstring fold_path = ::Path_AddLastSP(temp_path) + ::Now_Format(_T("%H%M%S")) ; tstring file_path = ::Path_AddLastSP(temp_path) + ::Now_Format(_T("%H%M%S")) + _T(".tmp") ; { ::Folder_Create (fold_path) ; ::File_CreateEmpty(file_path) ; } { time_t f_time = ::File_GetMTime(file_path.c_str()) ; f_time-= 3600*24*7 ; ::File_SetMTime(file_path.c_str(),f_time) ; ::File_SetMTime(fold_path.c_str(),f_time) ; } { tstring cmd = tstring(cmd_ls_la) + _T(" ") + ::QuotM_Add_Auto(temp_path) ; _tsystem(cmd.c_str()) ; } } return 0 ; } Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/t_mtime/t_utime $ g++ t_utime.cpp -Wall Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/t_mtime/t_utime $ ll total 136 drwxrwxrwx 2 Iwao users 4.0K Nov 7 16:24 ./ drwxrwxrwx 3 Iwao users 4.0K Nov 7 15:32 ../ -rwxr-xr-x 1 Iwao users 122.7K Nov 7 16:24 a.out* -rwxrwxrwx 1 Iwao users 838 Nov 7 16:22 t_utime.cpp* Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/t_mtime/t_utime $ ./a.out total 0 drwxr-xr-x 3 Iwao users 80 Nov 7 16:24 . drwxr-xr-x 3 Iwao users 60 Nov 7 16:24 .. drwxr-xr-x 2 Iwao users 40 Oct 31 16:24 162451 -rw------- 1 Iwao users 0 Oct 31 16:24 162451.tmp Iwao@AS5202T:/volume1/home/Iwao/gcc_test/Test/t_linux/t_mtime/t_utime $
次の所を参考にして…
VS CodeとFlaskによるWebアプリ開発「最初の一歩」
https:/
cd %TMP% で移動し temp\test\ 以下に作成.
py -m venv temp\test
Linux 環境では,~/Documents/tmp/test/
cd ~/Documents/tmp
python3 -m venv test
Win10 .\test\Scripts\activate Ubuntu source ./test/bin/activate
Flask の起動は,環境変数 FLASK_APP を設定して flask run らしい.
サンプルにある内容 を python3 sample.py との違いは?
2020/08/24
VS 2017 での仮想環境の作成は「ソリューション エクスプローラー」-「Python 環境」の「右クリック」にある.
Windows から Raspberry Pi 環境に接続できる様に samba を追加しようと…
検索して次の所を参考に設定.
https:/
https:/
この中で書かれている nano というエディタ.
CUI で使えるみたいで NAS などの幾つかの環境にインストール.
sudo opkg install nano
他に CUI のファイルマネージャ.
sudo opkg install mc
操作性など異なるが,エコロジーⅡを思い出す.
https:/
以前調べた時は,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] #
https:/
先ず 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
//////////////////////////////////////////////////////////////////// // 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 を呼び出す関係を調べていて 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$
予めデータ(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; }
雰囲気はつかめてきたので,以前作成した 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_() >>>
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:/
https:/
https://www.quark.kj.yamagata-u.ac.jp/~hiroki/python/?id=19
http:/
https:/
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:/
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:/
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:/
Fatal Python error: GC object already tracked
c_list は Python 側で確保しているため Py_DECREF はうまくないのではないか?
次の所を読ませてもらっていて感じたことを…
けいしゅけのブログ薬局 情報館
https:/
今私が習得するのにタイミングや更新サイクルが丁度良いので助かっている.
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 を使用してのテストコードで,起動後データを切替える方法がないかと…
メニューの利用で何とかできるか?
glutCreateMenu などで検索したがわかりやすい情報が少なかった.
https:/
https:/
http:/
以前作成した雛型に対して追加.メニュー部分はほぼリンク先のコードのまま.
https:/
#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 ; }
結局はこの方法ではなく,予めリスト化して キー入力 により切り替える方法に.