欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 明星 > cling: c++交互式执行

cling: c++交互式执行

2025/2/24 20:25:56 来源:https://blog.csdn.net/hfcaoguilin/article/details/145299305  浏览:    关键词: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 @0x7d1d8a494c40at /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]$ .helpCling (C/C++ interpreter) meta commands usageAll commands must be preceded by a '.', exceptfor 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 withsignature: 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 styleIf no name is given, prints out list of all typedefs..files			- Prints names of all included (parsed) files.fileEx			- Prints out included (parsed) file statisticsas 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 theexecution results of the input.dynamicExtensions [0|1]	- Toggles the use of the dynamic scopesand the late binding.debug <level>		- Generates debug symbols (level is optional, 0 to disable).printDebug [0|1]		- Toggles the printing of input's correspondingstate changes.storeState <filename>	- Store the interpreter's state to a given file.compareState <filename>	- Compare the interpreter's state with the onesaved 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 $binDD=/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-projectgit 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 \laria2c --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.bz2cling_D=/app5/cling-Ubuntu-16.04-x86_64-0.8~dev-c054ac2export PATH_BASE=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH=$PATH_BASE:$jdk8_d/bin:$cling_D/binwhich 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.gztar -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_BASErootD=/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.shecho $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 #没有交互式界面, 直接退出了

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词