æè¿ãèªä½ã©ã¤ãã©ãªã«æ©è½ã追å ããã®ã ããå®è£
ä¸ã«å¦ãªãªã³ã¯ã¨ã©ã¼ã«ééãã¦å³å¾å·¦å¾ããã®ã§ãã¡ã¢ãæ®ãã¦ãããå½åãã¸ãªã¡ããC++使ãã®ç§ã«ã¯åå ãã¤ããããã¨ããããåé¿çã§ãè¶ãæ¿ãã¦ããã
追å ããã®ã¯std::this_thread::sleep_for()
ã®ã©ããã¼é¢æ°ã ãè¦ã¦ã®éããé常ã«ç°¡åãªå
容ã§ããã
ãããã®é¢æ°ã¯ãå½åã¯å¼æ°ã®åãconståç
§ã«ãã¦ããã®ã ããè¬ã®ãªã³ã¯ã¨ã©ã¼ãçºçãããããã²ã¨ã¾ãåç
§ã§ã¯ãªãå®ä½ã渡ãããã«ãããã¼ã¸ã§ã³ãã³ããããããã¨ããçµç·¯ããããä¾ãã°é¢æ°cun::sleep::millis()
ã®å¼æ°ã®åã¯ãåæ²ã®ã½ã¼ã¹ã³ã¼ãã§ã¯const std::chrono::milliseconds::rep
ã«ãªã£ã¦ããããåæå®è£
ã§ã¯const std::chrono::milliseconds::rep&
ã ã£ãã
extern void millis(const std::chrono::milliseconds::rep& r) noexcept;
extern void millis(const std::chrono::milliseconds::rep r) noexcept;
ãã®é¢æ°ãå«ãã©ã¤ãã©ãªcun
ã¯ãã¦ã¼ãã£ãªãã£é¡ãã¾ã¨ããéçã©ã¤ãã©ãªlibcun
ããã«ãããä¸ã§ãåã
ã®ãã¹ãã¢ããªã«ã¦ãã«ãæ¸ã¿ã©ã¤ãã©ãªããªã³ã¯ãã¦ä½¿ç¨ããæ§æã«ãªã£ã¦ããã
åé¡ã¯ããã¹ãã³ã¼ããã³ã³ãã¤ã«ããå¾ãéçã©ã¤ãã©ãªããªã³ã¯ããæã«çºçãããUbnutu 22.04ã®GCC 11.4.0ããããªãªã³ã¯ã¨ã©ã¼ãåºåããã®ã ã
/usr/bin/ld: test_sleep.o: in function `main':
test_sleep.cpp:(.text.startup+0x182): undefined reference to `cun::sleep::millis(long const&)'
èå³æ·±ããã¨ã«ãVisual Studio 2022ã§ããªã³ã¯ã¨ã©ã¼ãçºçããã
test_sleep.obj : error LNK2001: å¤é¨ã·ã³ãã« "void __cdecl cun::sleep::millis(__int64 const &)" (?millis@sleep@cun@@YAXAEB_J@Z) ã¯æªè§£æ±ºã§ã
test_sleep.exe : fatal error LNK1120: 1 件ã®æªè§£æ±ºã®å¤é¨åç
§
æåã¯é¢æ°åãªã©ã®ã¹ãã«ãã¹ãçã£ãã®ã ãããã¹ã¯ãªãã£ããã¨ããããããããVisual Studio Codeä¸ã§é¢æ°ã®å®ç¾©å
ã«æ£ããã¸ã£ã³ãã§ããã®ã§ãååã¯æ£ããã¯ãã ã
ä¸ä½ãä½ãèµ·ãã¦ããã®ã ãããï¼ãçåã¯ãªãã¸ã§ã¯ããã¡ã¤ã«å
ã®ã·ã³ãã«åãè¦ããååã ã氷解ããã
cun::sleep
ã«ã¯secs()
ãmillis()
ãmicros()
ãnanos()
ã®4åã®å
¬éé¢æ°ãå®ç¾©ããã¦ããã®ã ãããã®ãã¡millis()
ã®ã¿ãã©ã¤ãã©ãªå´ã¨ãã¹ãã³ã¼ãå´ã¨ã§ã·ã³ãã«åãé£ãéã£ã¦ããã
$ nm ../../../build/linux/libcun.a | grep -F sleep
sleep.o:
0000000000000000 T _ZN3cun5sleep4secsERKl
0000000000000000 t _ZN3cun5sleep4secsERKl.cold
00000000000001e0 T _ZN3cun5sleep5nanosERKl
000000000000002d t _ZN3cun5sleep5nanosERKl.cold
0000000000000130 T _ZN3cun5sleep6microsERKl
000000000000001e t _ZN3cun5sleep6microsERKl.cold
0000000000000080 T _ZN3cun5sleep6millisERl
000000000000000f t _ZN3cun5sleep6millisERl.cold
U nanosleep
$ nm test_sleep.o | grep -F sleep
U _ZN3cun5sleep4secsERKl
U _ZN3cun5sleep5nanosERKl
U _ZN3cun5sleep6microsERKl
U _ZN3cun5sleep6millisERKl
$ _
ã·ã³ãã«åã®æ«å°¾ãè¦ã¦ã¿ãã¨ããã¹ãã³ã¼ãå´ã¯å
¨ã¦ERKl
ã§ãããã¨ãæå¾
ãã¦ããã®ã ããã©ã¤ãã©ãªå´ã«ã¦ãªããmillis()
ã®ã¿ERl
ã¨ãªã£ã¦ãããããã§ã¯ç¢ºãã«ãªã³ã¯ã¨ã©ã¼ã¨ãªãã¯ãã ã
Visual Studio 2022ã§ãåæ§ã«ãã·ã³ãã«åãé£ãéã£ã¦ãããã¨ã©ã¼ã¡ãã»ã¼ã¸ããæ¨æ¸¬ããã«ããã¹ãã³ã¼ãå´ã¯?millis@sleep@cun@@YAXAEB_J@Z
ã¨ããã·ã³ãã«åãæå¾
ãã¦ããããã ãï¼
> dumpbin /NOLOGO /LINKERMEMBER ..\..\..\build\msvc\libcun.lib | findstr /l sleep
C71F4 ??$sleep_for@_JU?$ratio@$00$00@std@@@this_thread@std@@YAXAEBV?$duration@_JU?$ratio@$00$00@std@@@chrono@1@@Z
C71F4 ??$sleep_for@_JU?$ratio@$00$0DLJKMKAA@@std@@@this_thread@std@@YAXAEBV?$duration@_JU?$ratio@$00$0DLJKMKAA@@std@@@chrono@1@@Z
C71F4 ??$sleep_for@_JU?$ratio@$00$0DOI@@std@@@this_thread@std@@YAXAEBV?$duration@_JU?$ratio@$00$0DOI@@std@@@chrono@1@@Z
C71F4 ??$sleep_for@_JU?$ratio@$00$0PECEA@@std@@@this_thread@std@@YAXAEBV?$duration@_JU?$ratio@$00$0PECEA@@std@@@chrono@1@@Z
C71F4 ??$sleep_until@Usteady_clock@chrono@std@@V?$duration@_JU?$ratio@$00$0DLJKMKAA@@std@@@23@@this_thread@std@@YAXAEBV?$time_point@Usteady_clock@chrono@std@@V?$duration@_JU?$ratio@$00$0DLJKMKAA@@std@@@23@@chrono@1@@Z
C71F4 ?micros@sleep@cun@@YAXAEB_J@Z
C71F4 ?millis@sleep@cun@@YAXAEA_J@Z
C71F4 ?nanos@sleep@cun@@YAXAEB_J@Z
C71F4 ?secs@sleep@cun@@YAXAEB_J@Z
3 ??$sleep_for@_JU?$ratio@$00$00@std@@@this_thread@std@@YAXAEBV?$duration@_JU?$ratio@$00$00@std@@@chrono@1@@Z
3 ??$sleep_for@_JU?$ratio@$00$0DLJKMKAA@@std@@@this_thread@std@@YAXAEBV?$duration@_JU?$ratio@$00$0DLJKMKAA@@std@@@chrono@1@@Z
3 ??$sleep_for@_JU?$ratio@$00$0DOI@@std@@@this_thread@std@@YAXAEBV?$duration@_JU?$ratio@$00$0DOI@@std@@@chrono@1@@Z
3 ??$sleep_for@_JU?$ratio@$00$0PECEA@@std@@@this_thread@std@@YAXAEBV?$duration@_JU?$ratio@$00$0PECEA@@std@@@chrono@1@@Z
3 ??$sleep_until@Usteady_clock@chrono@std@@V?$duration@_JU?$ratio@$00$0DLJKMKAA@@std@@@23@@this_thread@std@@YAXAEBV?$time_point@Usteady_clock@chrono@std@@V?$duration@_JU?$ratio@$00$0DLJKMKAA@@std@@@23@@chrono@1@@Z
3 ?micros@sleep@cun@@YAXAEB_J@Z
3 ?millis@sleep@cun@@YAXAEA_J@Z
3 ?nanos@sleep@cun@@YAXAEB_J@Z
3 ?secs@sleep@cun@@YAXAEB_J@Z
> _
ã©ã¤ãã©ãªå´ã®ã·ã³ãã«åã¯?millis@sleep@cun@@YAXAEA_J@Z
ã ã£ãã
ãã¦ãã·ã³ãã«åãé£ãéã£ã¦ãããã¨ã¾ã§ã¯åãã£ãã®ã ãããªãã·ã³ãã«åãé£ãéãç¶æ
ã«é¥ã£ãã®ããã¸ãªã¡ããC++使ãã®ç§ã«ã¯åãããªãã£ãï¼C++ã®ããã©ã³ãªããããããERKl
ã¨ERl
ã®éãã®æå³ã¨ãã調ã¹ãæç«ã¦ãç¥ã£ã¦ããã¨æãããã®ã ãï¼ã
ããã§ããã®åé¡ã®åé¿çã¨ãã¦ãã³ãããæ¸ã¿ã³ã¼ãããåããããã«ãå¼æ°ã®åã¨ãã¦åç
§ã使ãã®ãæ¢ãããããã§ã·ã³ãã«åã®é£ãéãã¯çºçããªããªã£ãã
$ nm ../../../build/linux/libcun.a | grep -F sleep
sleep.o:
0000000000000000 T _ZN3cun5sleep4secsEl
0000000000000000 t _ZN3cun5sleep4secsEl.cold
00000000000001e0 T _ZN3cun5sleep5nanosEl
000000000000002d t _ZN3cun5sleep5nanosEl.cold
0000000000000130 T _ZN3cun5sleep6microsEl
000000000000001e t _ZN3cun5sleep6microsEl.cold
0000000000000080 T _ZN3cun5sleep6millisEl
000000000000000f t _ZN3cun5sleep6millisEl.cold
U nanosleep
$ nm test_sleep.o | grep -F sleep
U _ZN3cun5sleep4secsEl
U _ZN3cun5sleep5nanosEl
U _ZN3cun5sleep6microsEl
U _ZN3cun5sleep6millisEl
$ _
ãã®ç¾è±¡ã¯ç°ãªãã³ã³ãã¤ã©ï¼ããã¦ç°ãªãæ¨æºã©ã¤ãã©ãªå®è£
ï¼ã§çºçãããå¦çç³»ã®ä¸å
·åã¨ãã§ã¯ãªãããã£ã¨å¥ã®è¦å ãããããªè¨ãã¨èªåèªèº«ã®ããã«èµ·å ãã¦ãããããªæ°ãããããæ確ãªæ ¹æ ã¯ãªãã£ãã
ãã¨ãã©ã®é¢æ°ãä¼¼ããããªã³ã¼ããªã®ã«ããªããmillis()
ã ãã·ã³ãã«åã®é£ãéããçºçããââã¨ããç¹ããã³ãã«ãªãããã ã£ããä»ã¨ã®éãã¯åï¼std::chrono::milliseconds::rep
ï¼ã ãã ããããã©ã¤ãã©ãªå´ããã¹ãã³ã¼ãå´ãåãããããã¡ã¤ã«ï¼âåããããã¿ã¤ã宣è¨ï¼ãåç
§ãã¦ãã¦ããã¤ã³ã³ãã¤ã«æã«è¦åããåºã¦ããªãããããªã®ã«çæãããã·ã³ãã«åãé£ãéãã¨ã¯â¦â¦ã
åå ã¯2æ¥å¾ã«åãã£ããé¢æ°millis()
ã ããã©ã¤ãã©ãªå´ã®ãããã¿ã¤ã宣è¨ã¨é¢æ°å®ç¾©ã¨ã§å¼æ°ã®åãé£ãéã£ã¦ããã®ã ã
extern void millis(const std::chrono::milliseconds::rep& r) noexcept;
void millis(milliseconds::rep& r) noexcept
{
delay<milliseconds>(r);
}
ãããã¿ã¤ã宣è¨ã§ã¯ãå¼æ°ã®åã¯conståç
§ã ã£ãããªã®ã§ãããããã¡ã¤ã«ä¸ã®ãããã¿ã¤ã宣è¨ãåç
§ãããã¹ãã³ã¼ãå´ã¯ãconståç
§åãåæã¨ããã·ã³ãã«åãçæããã
ä¸æ¹ã§ã©ã¤ãã©ãªå´ã¯ãã½ã¼ã¹ãã¡ã¤ã«ã«æ¸ããã¦ããéãã«æ®éã®åç
§åãåæã¨ããã·ã³ãã«åãçæããã
ããã«ããã両è
ã®éã§ã·ã³ãã«åã®é£ãéããçºçããã
ãªãã»ã©ã確ãã«çå±ã¯éããå®éã«ãé¢æ°å®ç¾©å´ã®åãconståç
§ã«ãã¦ã¿ããããªã³ã¯ã¨ã©ã¼ã¯çºçããªããªã£ãã
ãå¦ãªãªã³ã¯ã¨ã©ã¼ãã ã¨æã£ã¦ãããã®ã¯ãæ¡ã®å®ãèªåãåãè¾¼ãã ãã°ã ã£ã訳ã ã
åå ã¯åãã£ããã®ã®ããªãå人çã«ç´å¾ã§ããªãã£ãã®ã¯ãã©ã¤ãã©ãªããã«ãããæã«ä»®å¼æ°ã®åã®é£ãéããæ¤åºãããªãã£ããã¨ã ãã½ã¼ã¹ãã¡ã¤ã«sleep.cppã§ã¯ããããã¡ã¤ã«sleep.hppãã¤ã³ã¯ã«ã¼ããã¦ããã宣è¨ã¨å®ç¾©ã¨ã§ä»®å¼æ°ã®åãç°ãªããã¨ãæ¤åºã§ããªãã£ãï¼ãããç°ãªã2ã¤ã®ã³ã³ãã¤ã©ã§ï¼ï¼ã ãªãã¦â¦â¦ã
ââã¨ãã¾ããããªãã¨ãèãããããã«ãC++ã«æ
£ãã¦ããªãï¼ããã¦äºæ³ä»¥ä¸ã«Cè¨èªã®å½±é¿ãåãã¦ããï¼ããã°ã©ãã®å¾ã姿ãéãã¦ã¿ããã ããã
å¤åãCè¨èªãªã*1ã宣è¨ã¨å®ç¾©ã¨ã§ä»®å¼æ°ã®åãç°ãªããã¨ãæ¤åºã§ããå¯è½æ§ãããããªããªãã°ãCè¨èªã§ã¯é¢æ°ãå¤éå®ç¾©ï¼ãªã¼ãã¼ãã¼ãï¼ã§ããªãããã ãé¢æ°åãåããªãã°ãå¼æ°ãæ»ãå¤ã®åãä¸è´ãã¦ããªãã¦ã¯ãªããªãã
ã§ãC++ã§ã¯é¢æ°ã®å¤éå®ç¾©ãå¯è½ã ã宣è¨ã¨å®ç¾©ã¨ã§ä»®å¼æ°ã®åãé£ãéã£ã¦ããã®ããããã¨ããconståç
§ãå¼æ°ã«ã¨ããã¼ã¸ã§ã³ãã®é¢æ°å®£è¨ã¨ãæ®éã®åç
§ãå¼æ°ã«ã¨ããã¼ã¸ã§ã³ãã®é¢æ°å®ç¾©ãåå¨ããã ããªã®ããã³ã³ãã¤ã©ã«ã¯è¦åããã¤ããªããã ããè¦åããªãã¨ããããè¦åã§ããªãã¨ãããã
ãã¼ããC++ã§ã¯ã宣è¨ã¨å®ç¾©ã®é£ãéããã«ã©ã対å¦ããã°ããã®ã ãããï¼ããã®åé¡ã絶対ã«ãã§ã«ã¤ã¾ã¥ãã人ãããã¯ããªãã ããªããå
人ãã¡ã®è§£æ±ºçãç¥ãããã
ãã£ã±ãããªãã¹ãããããã¡ã¤ã«ã®ã¿ã§å®çµãããï¼ã½ã¼ã¹ãã¡ã¤ã«ã¨ããããã¡ã¤ã«ã«åããªãï¼ããã«ããã¹ããªã®ã ãããï¼ãã§ããããã¯ããã§ãã«ãã«ãããæéãé·ããªãããã ã