They say compiled languages are inconvenient for scripting because you have to recompile your script everytime you edit it. But you don't have to do it manually. Here's the solution for C++.
Disclaimer. Don't tell me writing scripts in C++ is bad idea. Don't be dogmatic. All rules have exceptions.
In bash scripts, first line always starts with "shebang" followed by name of a bash interpreter. When you run bash script, effectively bash interpreter is started with script's filename in first command-line argument.
#!/bin/bash
echo "Hello world!"
Same approach is used for Python scripts, Scala scripts, etc. And I did just the same.
-
Clone or download this repo.
-
Build and install:
make && sudo make install-system
(installs into/usr/local/bin/
) ormake && make install-user
(installs into~/bin
). -
Create C++ source file, prepend it with shebang line
#!/usr/local/bin/build-n-run
, make it executablechmod +x script.cpp
. Seeexamples/hello.cpp
. -
You can specify custom build command in script's 2nd line, or add custom options to default build command.
build-n-run
expands~
into current user's home directory in-I~/...
and-L~/...
options. Seeexamples/custom-build1.cpp
,examples/custom-build2.cpp
. -
Run your script:
./script.cpp
.build-n-run
will compile it on first run and after everytime you edit / touch it.
Script /aaa/bbb/ccc.cpp
is compiled into ~/.cache/build-n-run/aaa--bbb--ccc
. If compilation fails, build-n-run
will exit with status code 1
.
ATTENTION: Compiler's output is not redirected. So if compilation succeeded but with warnings, those warnings will still go to stdio
and will prepend your script's output.
-
linux.org.ru (Russian)