Compiling C++-code without a file

前端 未结 5 1675
无人及你
无人及你 2020-12-10 20:34

I\'m trying to compile some C++ code using your standard g++ compiler. However, rather than compiling from a file:

main.cpp:

#include 

        
5条回答
  •  忘掉有多难
    2020-12-10 20:37

    On Unix you can automate all process creating C/C++ programs by usage shell.

    1.Create file (example run_c.sh)

    2.Paste code to this file

    #!/bin/bash
    
    # Generate random filename
    RANDOM_FILENAME=`cat /dev/urandom | tr -dc 'A-Za-z0-9' | fold -w 20 | head -n 1`
    RANDOM_FILENAME=_$RANDOM_FILENAME
    readonly RANDOM_FILENAME
    
    # random filename for a C file
    RANDOM_FILENAME_C=$RANDOM_FILENAME.c
    readonly RANDOM_FILENAME
    
    # catch stdin as code
    CODE=$1
    
    # names headers of the C standart language
    declare -a C_STANDART_LIBRARY=('stdio' 'errno' 'assert' 'math' 'stdarg' 'stdbool' 'stdlib' 'string' 'time')
    
    # create the C file
    touch $RANDOM_FILENAME_C
    
    # write a first line to the file
    printf '// Compile C code from stdin\n\n' >> $RANDOM_FILENAME_C
    
    # make include all headers to the file
    for header in "${C_STANDART_LIBRARY[@]}"
    do
        printf '#include <%s.h>\n' $header >> $RANDOM_FILENAME_C
    done
    
    # make include all headers to the file
    printf "\nint main(int argc, char *argv[]) {\n" >> $RANDOM_FILENAME_C
    
    # split the code from stdin by ';' to an array lines
    IFS=';' read -r -a LINES_CODE <<< "$CODE"
    
    # write line by line the code
    for line in "${LINES_CODE[@]}"
    do
        printf '%s;\n' "$line" | sed -e 's/^[[:space:]]//' | sed -e 's/^/\t/' >> $RANDOM_FILENAME_C
    done
    
    # write ending the function 'main'
    printf "\treturn 0;\n}\n" >> $RANDOM_FILENAME_C
    
    # uncomment for display the C code
    # cat $RANDOM_FILENAME_C
    
    # compile the file
    gcc -Wall -std=c11 -o $RANDOM_FILENAME $RANDOM_FILENAME_C
    
    # run programm if no errors
    if [ -f "$RANDOM_FILENAME" ];
        then
        ./$RANDOM_FILENAME
    fi
    
    # rm the file with source code
    rm $RANDOM_FILENAME_C
    # rm the compliled file
    
    if [ -f "$RANDOM_FILENAME" ];
        then
            rm $RANDOM_FILENAME
    fi
    

    3.Make this file as executable

    $ chmod +x run_c.sh 
    $ ls -l | grep run_c.sh 
    -rwxr-xr-x 1 setivolkylany setivolkylany 1589 Dec 26 11:19 run_c.sh
    

    Usage:

    $ ./run_c.sh 'int a = 4; int b = 6; int c = a + b; printf("%d + %d = %d\n", a, b, c)'
    4 + 6 = 10
    
    $ ./run_c.sh 'puts("Worked");'
    Worked
    

    Testing environment

    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Debian
    Description:    Debian GNU/Linux 8.6 (jessie)
    Release:    8.6
    Codename:   jessie
    $ uname -a
    Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
    

    Inspired from http://django-notes.blogspot.com/2013/01/compiling-c-from-stdin.html and https://github.com/vgvassilev/cling

提交回复
热议问题