[Mac] 用互動式的 Cling 快速測試 C++ 敘述與函式

[Mac] 用互動式的 Cling 快速測試 C++ 敘述與函式

最近同事轉貼了一篇

編譯器 LLVM 淺淺玩. 以動手實作來認識 The LLVM Compiler 寫得不錯,

裡面提到了 Cling 這個運用了 LLVM 架構寫的 Interactive C++ interpreter,

也就是 C++ 的直譯器,感覺很有趣,

就來玩玩看吧~

 

在 Mac 上可以用 Homebrew 安裝 Cling:

brew install cling

 

執行 cling 後,就會進到互動模式:

testuser@localhost ~ $ cling

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$

 

執行 .help 可以看到可用的指令:

[cling]$ .help

 Cling (C/C++ interpreter) meta commands usage
 All commands must be preceded by a '.', except
 for the evaluation statement { }
 ==============================================================================
 Syntax: .Command [arg0 arg1 ... argN]

   .L <filename>		- Load the given file or library

   .(x|X) <filename>[args]	- Same as .L and runs a function with
                  signature: ret_type filename(args)

   .> <filename>		- Redirect command to a given file
      '>' or '1>'		- Redirects the stdout stream only
      '2>'			- Redirects the stderr stream only
      '&>' (or '2>&1')		- Redirects both stdout and stderr
      '>>'			- Appends to the given file

   .undo [n]			- Unloads the last 'n' inputs lines

   .U <filename>		- Unloads the given file

   .I [path]			- Shows the include path. If a path is given -
                  adds the path to the include paths

   .O <level>			- Sets the optimization level (0-3)
                  (not yet implemented)

   .class <name>		- Prints out class <name> in a CINT-like style

   .files 			- Prints out some CINT-like file statistics

   .fileEx 			- Prints out some file statistics

   .g 				- Prints out information about global variable
                  'name' - if no name is given, print them all

   .@ 				- Cancels and ignores the multiline input

   .rawInput [0|1]		- Toggle wrapping and printing the
                  execution results of the input

   .dynamicExtensions [0|1]	- Toggles the use of the dynamic scopes and the
                  late binding

   .printDebug [0|1]		- Toggles the printing of input's corresponding
                  state changes

   .storeState <filename>	- Store the interpreter's state to a given file

   .compareState <filename>	- Compare the interpreter's state with the one
                  saved in a given file

   .stats [name]		- Show stats for internal data structures
                  'ast'  abstract syntax tree stats
                  'asttree [filter]'  abstract syntax tree layout
                  'decl' dump ast declarations
                  'undo' show undo stack

   .help			- Shows this information

   .q				- Exit the program

 

像是我跑 printf,會因為還沒有引入相關標頭檔而失敗:

[cling]$ printf
input_line_4:2:2: error: use of undeclared identifier 'printf'
 printf
 ^

 

先跑 #include <stdio.h> ,再跑 printf,

就會變成有定義了,Cling 會秀出函式的宣告:

[cling]$ #include <stdio.h>

[cling]$ printf
(int (*)(const char *, ...)) Function @0x7fff202cb0a8
  at /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h:170:
int	 printf(const char * __restrict, ...) __printflike(1, 2)

 

可以執行 printf() 函式:

[cling]$ printf("this is a test")
this is a test(int) 14

 

這種互動模式最方便的地方,

就是拿來測試一些不熟的函式 (或語法),可以直接觀察它的結果。

像是下面拿來測試一下 strchr() 函式的輸入與輸出:

[cling]$ strchr("abc", 'b')
(const char *) "bc"

[cling]$ strchr("abc", 'd')
(const char *) nullptr

 

當然也可以直接寫完整的 C++ 程式,來跑測試程式。

不過用這直譯器可以很方便的跑一些小敘述,算蠻實用的喔~

 

參考資料:GitHub – vgvassilev/cling: The interactive C++ interpreter Cling

(本頁面已被瀏覽過 33 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料