Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

Can I run any c or c++ spesific commands at linux terminal without excecute?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can I get output of specific command without compile, debug, execute?

No. You have to compile and then execute a C or a C++ program.

Is there any way to do on Linux terminal?

You can write a small script that creates a temporary file, compiles the code, runs it and then removes the temporary file. Save the following file in PATH, ex. as /usr/local/bin/ccrun or as ~/bin/ccrun and add ~/bin to PATH. Remember to add executable permissions.

#!/bin/sh
tmp=$(mktemp)
trap 'rm "$tmp"' EXIT
gcc -xc - -o "$tmp" && ./"$tmp" "$@"

then you can:

printf "%s
" "#include <stdio.h>" "int main() { printf("%d
", 5); }" | ccrun

or inline, without script:

printf "%s
" "#include <stdio.h>" 'int main() { printf("%d
", 5); }' | ( gcc -xc - && ./a.out; rm ./a.out )

But it's simpler to try functions in one of online compilers, there are so many of them, and you do not need to install and change anything - just your browser is enough. https://godbolt.org/ http://cpp.sh/ https://www.onlinegdb.com/online_c++_compiler to name a few of my first google hits for "online C++ compiler". The popular site with C and C++ documentation: https://en.cppreference.com/w/c/io/fprintf has examples with "run this code", then you can edit the code and execute it.

PS. I may share scripts ,ccrun and ,c++run that work just like the script above with some special semantics that like add all possible #includes (but c++ is super slow then) to the source file and add -fsanitize options to the file when compiling and running. The scripts are mostly unfinished and were made for very simple cases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...