当前位置: 首页 > article >正文

cling: c++交互式执行

cling编译、使用

2. cling使用

经过 1. cling编译 后 ,即可使用如下(c++像脚本一样被使用):

上手 : On-the-fly-C++/ , cling-cpp-11-interpreter/

hello
/app5/cling-build/bin/cling #进入交互式界面

#也可以脚本样式执行
echo '''
#include <iostream>
std::cout << "hello" << std::endl;
''' | /app5/cling-build/bin/cling
#输出hello
cling无子进程 且 默认只有一个线程
#cling无子进程:
pgrep --parent  `pidof cling`

#cling默认只有一个线程: 
ls /proc/`pidof cling`/task/ | wc -l  # == 1
查函数签名
#include <pthread.h>

pthread //tab tab 后 有相关结构体补全

pthread_create //回车后有函数签名
/*
(int (*)(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict) noexcept(true)) Function @0x7d1d8a494c40
  at /usr/include/pthread.h:202:
extern int pthread_create (pthread_t *__restrict __newthread,
			   const pthread_attr_t *__restrict __attr,
			   void *(*__start_routine) (void *),
			   void *__restrict __arg) __THROWNL __nonnull ((1, 3))
*/
c++ std 文本文件按行读取
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
std::string ln;
std::vector<std::string> lnVec;
std::ifstream ifile("f1.txt");
if(!ifile.is_open()) exit(1);
ifile.fail(); //false
while(true){
  std::getline(ifile,ln);
  if(ifile.fail()){ break; }
  lnVec.push_back(ln);
}
ifile.fail(); //true
lnVec;// (std::vector<std::string> &) { "aaaa", "bbb", "ccc" }
ifile.close();
ifile.is_open(); //false
.help

/app5/cling-build/bin/cling 进入交互式界面



****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[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|include) [path]		- Shows all include paths. If a path is given,
				  adds the path to the include paths.

   .O <level>			- Sets the optimization level (0-3)
				  If no level is given, prints the current setting.

   .class <name>		- Prints out class <name> in a CINT-like style (one-level).
				  If no name is given, prints out list of all classes.

   .Class <name>		- Prints out class <name> in a CINT-like style (all-levels).
				  If no name is given, prints out list of all classes.

   .namespace			- Prints list of all known namespaces

   .typedef <name>		- Prints out typedef <name> in a CINT-like style
				  If no name is given, prints out list of all typedefs.

   .files			- Prints names of all included (parsed) files

   .fileEx			- Prints out included (parsed) file statistics
				  as well as a list of their names

   .g <var>			- Prints out information about global variable
				  'var' - 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

   .debug <level>		- Generates debug symbols (level is optional, 0 to disable)

   .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

   .T <filePath> <comment>	- Generate autoload map

   .trace <repr> <id>		- Dump trace of requested respresentation
				  (see .stats arguments for <repr>)

   .help			- Shows this information (also .?)

   .q				- Exit the program


1. cling编译

vgvassilev/cling.git/readme.md/编译步骤

binD=/app5/cling-build
mkdir $binD

D=/app5/llvm-home
mkdir $D 

#欧洲高能物理数据分析 ROOT
git clone https://github.com/root-project/llvm-project.git $D
#/app5/llvm-home/llvm-project/.git/config
llvmD=$D/llvm-project # == /app5/llvm-home/llvm-project


git clone https://github.com/root-project/cling.git $D
#/app5/llvm-home/cling/.git/config
clingD=$D/cling # == /app5/llvm-home/cling

#配置
cmake -DLLVM_EXTERNAL_PROJECTS=cling -DLLVM_EXTERNAL_CLING_SOURCE_DIR=$clingD -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_TARGETS_TO_BUILD="host;NVPTX" -DCMAKE_BUILD_TYPE=Release $llvmD/llvm
#命令展开: cmake -DLLVM_EXTERNAL_PROJECTS=cling -DLLVM_EXTERNAL_CLING_SOURCE_DIR=/app5/llvm-home/cling -DLLVM_ENABLE_PROJECTS="clang" -DLLVM_TARGETS_TO_BUILD="host;" -DCMAKE_BUILD_TYPE=Release /app5/llvm-home/llvm-project/llvm

#编译clang
cmake --build $binD  --target clang -j10

#编译cling
cmake --build  $binD --target cling -j10

issues/531#issuecomment-2337234891

所用版本

#/app5/llvm-home/llvm-project/.git/config
#llvmD=$D/llvm-project # == /app5/llvm-home/llvm-project
git --git-dir=$llvmD/.git rev-parse HEAD # == 66d752c5c714ac57b468e1b4d62a52f9207b5d44
git --git-dir=$llvmD/.git   branch  # == ROOT-llvm18
git --git-dir=$llvmD/.git --no-pager  log  --format=oneline | head -n 1
#66d752c5c714ac57b468e1b4d62a52f9207b5d44 Do not install {Clang,Cling}Config.cmake in the project lib dir.
 

#/app5/llvm-home/cling/.git/config
#clingD=$D/cling # == /app5/llvm-home/cling
git --git-dir=$clingD/.git rev-parse HEAD # == 1d4925536b9f89015ad2afdc24e260207dd69ebb
git --git-dir=$clingD/.git   branch  # == master
git --git-dir=$clingD/.git --no-pager  log  --format=oneline | head -n 1
#1d4925536b9f89015ad2afdc24e260207dd69ebb Move static init function renaming to `BackendPasses.cpp`

1B. cling编译备忘

若不编译clang会报错 resource directory /app5/llvm-home/cling-build/lib/clang/18 not found!

cd /app5/llvm-home/cling-build/
./bin/cling 
ERROR in cling::CIFactory::createCI():
  resource directory /app5/llvm-home/cling-build/lib/clang/18 not found!

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

cling-官方编译好的

vgvassilev/cling.git

夜间编译 / 2020-Dec-17-cling-Ubuntu-16.04-x86_64-0.8.dev-c054ac2.tar.bz2

上手 : On-the-fly-C++/ , cling-cpp-11-interpreter/

 cat /etc/issue # == Ubuntu 22.04.5 LTS \n \l

aria2c --all-proxy=http://westgw:7890   https://github.com/vgvassilev/cling/releases/download/cling-nightlies/2020-Dec-17-cling-Ubuntu-16.04-x86_64-0.8.dev-c054ac2.tar.bz2
tar -jxf 2020-Dec-17-cling-Ubuntu-16.04-x86_64-0.8.dev-c054ac2.tar.bz2

cling_D=/app5/cling-Ubuntu-16.04-x86_64-0.8~dev-c054ac2

export PATH_BASE=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH=$PATH_BASE:$jdk8_d/bin:$cling_D/bin

which cling # == /app5/cling-Ubuntu-16.04-x86_64-0.8~dev-c054ac2/bin/cling

cling运行报错, 估计 原因是 这cling 是基于ubuntu16编译的, 而我这是ubuntu22

ERROR in cling::CIFactory::createCI(): cannot extract standard library include paths!
Invoking:
  LC_ALL=C g++-5  -O3 -DNDEBUG -xc++ -E -v /dev/null 2>&1 | sed -n -e '/^.include/,${' -e '/^ \/.*++/p' -e '}'
Results was:
With exit code 0
input_line_1:1:10: fatal error: 'new' file not found
#include <new>
         ^~~~~
Warning in cling::IncrementalParser::CheckABICompatibility():
  Failed to extract C++ standard library version.

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

欧洲高能物理数据分析 ROOT内置的cling

改为用 高能物理数据分析 ROOT / root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4.tar.gz 其内置了cling

结论: rootcling bare-cling 没有交互式界面, 直接退出了

aria2c https://root.cern/download/root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4.tar.gz

tar -xf root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4.tar.gz

#清理PATH
export PATH_BASE=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH=$PATH_BASE

rootD=/app5/root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4

#激活 高能物理ROOT 环境
source $rootD/bin/thisroot.sh
#命令展开: source /app5/root_v6.34.02.Linux-ubuntu22.04-x86_64-gcc11.4/bin/thisroot.sh

echo $PATH #== $rootD/bin:$PATH_BASE
echo $LD_LIBRARY_PATH # == $rootD/lib
echo  $ROOTSYS # == $rootD
which rootcling # == $rootD/bin/rootcling

rootcling  --help-list | grep bare
#bare-cling - Call directly cling and exit.


rootcling  bare-cling #没有交互式界面, 直接退出了


http://www.kler.cn/a/514638.html

相关文章:

  • Effective C++读书笔记——item22(明确变量的作用域和访问权限)
  • 精选100+套HTML可视化大屏模板源码素材
  • RK3568笔记七十七:RTMP实时推流
  • Java Web开发高级——单元测试与集成测试
  • 【重庆市乡镇界】面图层shp格式arcgis数据乡镇名称和编码wgs84坐标无偏移内容测评
  • cuda从零开始手搓PB神经网络
  • 数据分析 基础定义
  • 深入探讨Web应用开发:从前端到后端的全栈实践
  • 无人机反制设备:察打诱一体设备技术详解
  • Linux:修改用户名
  • 5.9 洞察 OpenAI - Translator:日志(Logger)模块的 “时光记录仪”
  • 「全网最细 + 实战源码案例」设计模式——单例设计模式
  • 深度学习 Pytorch 动态计算图与梯度下降入门
  • HTTPS协议简述
  • Flask基础和URL映射
  • 【spring专题】编译spring5.3源码
  • 如何给自己的域名配置免费的HTTPS How to configure free HTTPS for your domain name
  • ERP系统的财务会计基础知识:财务管理
  • Kmeans与KMedoids聚类对比以及python实现
  • C语言中危险函数
  • JMeter 测试Dubbo 接口
  • Win10系统部署RabbitMQ Server
  • linux系统安装vmware workstation
  • Laravel 请求接口 调用2次
  • TS报错解决:不能将类型“string | null”分配给类型“string | undefined”
  • 2025年最新电子制造行业CRM售后管理解决方案