std::vector<long> の文字列への変換.
何年も前に書いたコードで,使い方を忘れているのでそのまとめ.
次の様な関数を用意している.
template <class VX> tstring Vx_To_tstring(const VX& vx,LPCTSTR sp=_T(" "))
次の様な使い方で long の配列を文字列へ変換できる.
tstring vl_str = ::Vx_To_tstring<v_long>(long_ary) ;
この画面は、簡易表示です
std::vector<long> の文字列への変換.
何年も前に書いたコードで,使い方を忘れているのでそのまとめ.
次の様な関数を用意している.
template <class VX> tstring Vx_To_tstring(const VX& vx,LPCTSTR sp=_T(" "))
次の様な使い方で long の配列を文字列へ変換できる.
tstring vl_str = ::Vx_To_tstring<v_long>(long_ary) ;
localtime が正しく求められない ままの環境.
https:/
ある程度 DS220+ に移行したので,SSH 接続環境の再設定.
CLI コマンドの導入.
DS220+ と同様 .
名前 :「SynoCommunity」
場所 :「https://packages.synocommunity.com/」
「SynoCli File Tools」をインストールして tree , mc , nano などを追加.
これで追加した tree は,localtime が正しく求められている.
Entware のインストール.
Install on Synology NAS
Iwao@DS116:~$ sudo -i
Password:
root@DS116:~# mkdir -p /volume1/@Entware/opt
root@DS116:~# rm -rf /opt
root@DS116:~# mkdir /opt
root@DS116:~# mount -o bind "/volume1/@Entware/opt" /opt
root@DS116:~# uname -m
armv7l
root@DS116:~# wget -O - https://bin.entware.net/armv7sf-k3.2/installer/generic.sh | /bin/sh
--2022-06-15 14:51:04-- https://bin.entware.net/armv7sf-k3.2/installer/generic.sh
タスクスケジューラの登録.
「作成」-「トリガーされたタスク」-「ユーザー指定のスクリプト」.
「Entware」「root」「ブートアップ」を指定.
「タスク設定」タブのスクリプトを記述するの所に,貼り付け.
NAS を再起動.
gcc のインストール.
Iwao@DS116:~$ sudo -i
root@DS116:~# opkg update
Downloading http://bin.entware.net/armv7sf-k3.2/Packages.gz
Updated list of available packages in /opt/var/opkg-lists/entware
root@DS116:~# opkg install gcc
Installing gcc (7.4.0-7) to root...
前回の設定で PHP などが動作する様になったので,今度は opkg のインストール.
その前に,インストールしていない状態で,何か動作するかをチェック.
以前書いた,次のコード.
#include "pref_ini.hxx"
#include <iostream>
#include "get_path.hxx"
#include "i_Trace.hxx"
int _tmain (int argc,TCHAR* argv[])
{
_tsetlocale(LC_ALL,_T("")) ;
{
std::tout <<::TGetEnv(_T("TEMP")) << std::endl ;
std::tout <<::TGetEnv(_T("USERNAME")) << std::endl ;
std::tout <<::TGetEnv(_T("USER")) << std::endl ;
std::tout <<::TGetEnv(_T("HOMEPATH")) << std::endl ;
}
{
tstring iniPath = ::Path_AddLastSP(::GetCWD()) + _T("Test.ini") ;
tstring result = ::P_ini_Get(iniPath.c_str(),_T("Preference"),_T("server"),_T("")) ;
std::tout << result << std::endl ;
}
return 0 ;
}
以前コンパイルした a.out は動作した.
この頃はまだ他社 NAS の購入前なので,DS116 と,仮想マシンの Linux 環境で作成したものと思う.
他のものを幾つか試したが,ほとんど動作しない.
Iwao@DS220:~/gcc_test/Test/t_linux/t_mtime/t_m_time$ ./f.out
./f.out: /lib64/libc.so.6: version `GLIBC_2.33' not found (required by ./f.out)
./f.out: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./f.out)
Iwao@DS220:~/gcc_test/Test/t_linux/t_mtime/t_m_time$
以前 opkg のインストールは面倒だった と記憶している.
それで,新しい情報がないかと検索すると QNAP NAS と似た様なものがあった.
Packages for Synology NAS
が,この中に Entware はなさそう?
更に検索すると,
Synology NAS DS213JへAria2をインストール
Community Package Hub
こちらの方かと思いやってみたが,エラーとなる.
結局以前と同じ方法で.
Install on Synology NAS
Iwao@DS220:~$ Iwao@DS220:~$ sudo -i
root@DS220:~# ll /opt
ls: cannot access '/opt': No such file or directory
root@DS220:~# mkdir -p /volume1/@Entware/opt
root@DS220:~# mkdir /opt
root@DS220:~# mount -o bind /volume1/@Entware/opt/ /opt
root@DS220:~# wget -O - https://bin.entware.net/x64-k3.2/installer/generic.sh | /bin/sh
Will not apply HSTS. The HSTS database must be a regular and non-world-writable file.
NAS を再起動.
gcc のインストール.
Iwao@DS220:~$ Iwao@DS220:~$ sudo -i
root@DS220:~# opkg update
Downloading http://bin.entware.net/x64-k3.2/Packages.gz
Updated list of available packages in /opt/var/opkg-lists/entware
root@DS220:~# opkg install gcc
Installing gcc (7.4.0-7) to root...
root@DS220:~#
仮想マシンの 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 $
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 $
幾つかの Windows API を呼び出す所を書き換えていて,
RECT や POINT の構造体の初期化で色々と気になり少し調べたことのまとめ.
class として書く場合は,コンストラクタで初期化しているので意識しない.
struct Vector2 { // Vector2 () {} Vector2 (T v=0) : x(v), y(v) {} Vector2 (T x, T y) : x(x), y(y) {} // ... T x ; T y ; } ;
MFC を使用している時も同様.
CRect rect(0,0,0,0) ;
他の Win32 の構造体だと
MEMORYSTATUSEX memStat ; memset(&memStat,0,sizeof(MEMORYSTATUSEX)) ; memStat.dwLength= sizeof(MEMORYSTATUSEX) ; ::GlobalMemoryStatusEx(&memStat) ;
Windows では ::ZeroMemory を使っていることもあり.
#define ZeroMemory(pb,cb) memset((pb),0,(cb))
これまで RECT を 0 にする場合,次の様にも書いていた.
RECT rect = { 0,0,0,0 } ;
次の様に省略する方法もあるのは知っていたがあまり使っていない.
RECT rect = { 0 } ;
K & R 2nd の A8.7 に書かれている.
Web で検索すると ” RECT rect = { } ; ” の様な記述もあったが,これはどうかと…
構造体のコピーや戻り値は悩む所.
RECT の様な単純な構造では特に問題ないと思う.
XML などのツリー構造の場合はデータにより時間がかかるので注意が必要.
2020/10/13
次の様なコードで動作 を確認.
struct strct_c { i__8 i1 ; i_32 i4 ; i_16 i2 ; i_64 i8 ; } ; bool test_struct (void) { strct_a sa_rn ; strct_b sb_rn ; strct_c sc_rn ; strct_a sa_f0 = {1,2,3,4} ; strct_b sb_f0 = {1,2,3,4} ; strct_c sc_f0 = {1,2,3,4} ; strct_a sa__0 = {0} ; strct_b sb__0 = {0} ; strct_c sc__0 = {0} ; strct_a sa_m0 ; memset(&sa_m0,0,sizeof(sa_m0)) ; strct_b sb_m0 ; memset(&sb_m0,0,sizeof(sb_m0)) ; strct_c sc_m0 ; memset(&sc_m0,0,sizeof(sc_m0)) ; ::dump_mem(&sc_rn,sizeof(sc_rn)) ; ::dump_mem(&sc_f0,sizeof(sc_f0)) ; ::dump_mem(&sc__0,sizeof(sc__0)) ; ::dump_mem(&sc_m0,sizeof(sc_m0)) ; return true ; }
Entware のインストール.前回 と同様だが少し画像と説明を追加.
「App Center」を開き,右上の「設定(歯車マーク)」をクリック.「アプリリポジトリ」タブを選択.
「追加」を押して「OPKG Store」の URL「 https:/
ログイン情報は不要.
https:/
左のアイコンに「OPKG Store」が増える.選択して「すべてのアプリ」.検索ボックスに「Entware」と入力.
「Entware-std」をインストール.
これで opkg コマンドが使える様になる.
ASUSTOR NAS で作成した .out は,この段階で動作する様になるみたい.
gcc と python3 などのインストール
admin で入ったコンソール で,
opkg install gcc
opkg install python3
他に tree , mc なども.
http:/
http:/
以前調べた時は,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:/
//////////////////////////////////////////////////////////////////// // 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 はうまくないのではないか?
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 ; }
結局はこの方法ではなく,予めリスト化して キー入力 により切り替える方法に.
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 を再起動したり,プロジェクトを読み直したりしていたら通る様になった.
今度は PHP からの呼び出し.
<?php echo ("call c++\n") ; system ("./a.out") ; echo ("call python\n") ; system ("python r_cs.py") ; ?>
実際の動作を試すには,gcc と python が必要です.
cpp のコンパイルは
g++ -Wall r_cs.cpp
これで ./a.out が作成されます.
python の呼び出しは
python r_cs.py
次の所で,同じ様な結果を表示します.
https:/
次の様な計算を何かに使えないかと…
/* https://ja.wikipedia.org/wiki/矢_(幾何学) https:// 弦 c と 矢 s から 半径 r を求める r = (c*c) / (8*s) + s/2 半径 r と 弦 c から 矢 s を求める s = r - sqrt( r*r - (c/2)*(c/2) ) 半径 r と 矢 s から 弦 c を求める c = sqrt( r*r - (r-s)*(r-s) ) * 2 i_func.hxx https://drive.google.com/file/d/1kHadFbhUi9QfGRovMXKYr2xHgcFJAqdZ/view */ double r_cs (const double c , const double s) { return ( ( c*c ) / ( 8*s ) + s/2 ) ; } double s_rc (const double r , const double c) { return ( r-sqrt(r*r - (c/2)*(c/2) ) ) ; } double c_rs (const double r , const double s) { return ( sqrt(r*r - (r-s)*(r-s) )*2 ) ; }en.wikipedia.org/ wiki/ Circle#Sagitta
動作を確認するために 3,4,5 や 5,12,13 は知っていたが次のキーワードで検索.
三平方の定理 整数 組み合わせ
https://ja.wikipedia.org/wiki/ピタゴラスの定理
先ず C++ で書いたもの.
#include <cmath> double r_cs (const double c , const double s) { return ( ( c*c ) / ( 8*s ) + s/2 ) ; } double s_rc (const double r , const double c) { return ( r-sqrt(r*r - (c/2)*(c/2) ) ) ; } double c_rs (const double r , const double s) { return ( sqrt(r*r - (r-s)*(r-s) )*2 ) ; } #include <iostream> int main(void){ std::cout << "6 1 5" << std::endl ; std::cout << r_cs(6,1) << std::endl ; std::cout << s_rc(5,6) << std::endl ; std::cout << c_rs(5,1) << std::endl ; std::cout << std::endl ; std::cout << "8 2 5" << std::endl ; std::cout << r_cs(8,2) << std::endl ; std::cout << s_rc(5,8) << std::endl ; std::cout << c_rs(5,2) << std::endl ; std::cout << std::endl ; return 0 ; }
次の所に貼り付けて動作確認できます.
https://paiza.io/ja/
https://wandbox.org/
https://ideone.com/
次は JavaScript .
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 ) ; } console.log ("6 1 5") ; console.log (r_cs(6,1)) ; console.log (s_rc(5,6)) ; console.log (c_rs(5,1)) ; console.log ("") ; console.log ("8 2 5") ; console.log (r_cs(8,2)) ; console.log (s_rc(5,8)) ; console.log (c_rs(5,2)) ; console.log ("") ;
Python .
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 ) print ("6 1 5") print (r_cs(6,1)) print (s_rc(5,6)) print (c_rs(5,1)) print ("") print ("8 2 5") print (r_cs(8,2)) print (s_rc(5,8)) print (c_rs(5,2)) print ("")
2020/05/30
r_cs.py の r_cs で「浮動小数点数」として扱われていなかったので修正.
def r_cs (c , s) : return ( ( c*c ) / ( 8.*s ) + s/2. )