How to write a program in C ++ that will be compiled after being launched from the shell?

I often want to try something in C ++ without having to write a Makefile, create a project, or type complex commands.

I wondered if it is possible to make a .cpp file, which is also a bash script, so it can compile and run by itself.

I also wanted to be able to specify command line options in a script if there were dependencies like boost, etc.

+4
source share
3 answers

, script, cpp , , script, cpp ( ), cpp ? inotifywait. , script. :

.cpp

#include <iostream>

using namespace std;

int main (void) {
    cout << endl << "Code Running (ver. 10)" << endl << "and being timed." << endl;
    return 0;
}

inotifywait Script

#!/bin/bash

# watchdir=${1:-$PWD}
src=${1:-app.cpp}   ## set the source and output (exe) names
out=${src%.cpp}

while inotifywait -q -e modify -e modify -e close_write -e delete "$PWD"; do

    printf "\n compiling 'g++ %s -o %s'\n\n" $src $out

    [ -f $out ] && rm $out      ## remove existing exe before building

    g++ -o $out $src            ## build new exe

    [ -f $out ] || {            ## validate new exe created
        printf "error: compilation failed. exiting script.\n\n"
        printf "usage:   %s source.cpp (default: app.cpp)\n\n"
        exit 1
    }

    [ -x $out ] || {            ## validate it is executable
        printf "error: file produced from compilation is not executable.\n"
        exit 1
    }

    time ./$out 2>&1                        ## compute elapsed time
    exesz=$(du -h $out | awk '{print $1}')  ## store disk usage (removing name)

    ## print results
    printf "\n Source file:  %s\n" "$src"
    printf " output file:  %s\n" "$out"
    printf " size of exe:  %s bytes\n\n" "$exesz"

done

script . :.

( app.cpp)

$ ./watch.sh
/home/david/scr/tmp/stack/dat/tmp/bld/ MODIFY app.cpp

 compiling 'g++ app.cpp -o app'


Code Running (ver. 10)
and being timed.

real    0m0.003s
user    0m0.001s
sys     0m0.001s

 Source file:  app.cpp
 output file:  app
 size of exe:  16K bytes
+5

... , :

/*/../bin/ls > /dev/null
 filename=$(basename $BASH_SOURCE)
 dirname=$(cd $(dirname $BASH_SOURCE) && pwd)
 if [ "${dirname}" == "$(pwd)" ]
 then
    mkdir -p ${dirname}/bin > /dev/null
    filename="${dirname}/bin/${filename}"
 else
    filename="./${filename}"
 fi
 filename=${filename%.*}
 if [ $0 -nt ${filename} ]
 then
        c++ -o "${filename}" -std=c++1y $BASH_SOURCE || exit
 fi
 ("${filename}" && exit) || echo $? && exit
 exit
*/
#include <iostream>

using namespace std;

auto main() -> int
{
        cout << "Hello, World" << endl;
        return 0;
}

skeleton.cpp:

$ chmod +x skeleton.cpp
$ ./skeleton.cpp
Hello, World
+3

Multiline Shebang Rosetta Code. 2011 C-:

#!/bin/bash
sed -n -e '7,$p' < "$0" | /usr/bin/gcc -x c -o "$0.$$.out" -
$0.$$.out "$0" "$@"
STATUS=$?
rm $0.$$.out
exit $STATUS
#include <stdio.h>

int main(int argc, char **argv)
{
  int i;
  for (i = 0; i < argc; i++)
    printf("argv[%d] -> %s\n", i, argv[i]);
  return 0;
}

It is easy to adapt to C ++. Basically just change the argument -x cto gcc.

At some point, I had a version of this that would only compile the executable file if it did not exist or the source was newer; The above is always compiled.

0
source

Source: https://habr.com/ru/post/1614662/


All Articles