Compiling C++-code without a file

2019-01-19 23:45发布

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

main.cpp:

#include <iostream>

int main(){
    std::cout << "Hello World!\n";
    return 0;
}

I would prefer to do something like

g++ ... "#include <iostream>\n int main(){ std::cout << \"Hello World!\n\"; return 0;}"

A previous post from stackoverflow showed that

echo "int main(){}" | gcc -Wall -o testbinary -xc++ -

works but I would like to know how it works and better yet, if there is a way to do this without the need to pipe the contents.

EDIT:

I'm doing run-time code generation where I need to generate a shared library and load the functions created.

I thought there would be a flag to tell the compiler "hey, I'm giving you the source code and not the file".

Thanks again for the help!

5条回答
女痞
2楼-- · 2019-01-20 00:20

You mention generating C (or C++) code on the fly and then compiling it and dlopen-ing it.

I'm doing the same in MELT (a domain specific language to extend GCC).

I don't see any valid reason to avoid putting the code in some (temporary) file. Writing even a million line of generated C or C++ lines is quite quick (in MELT, less than a few seconds, and most of that time is not the I/O!). Compiling that is much much slower (a few dozens of seconds at least), and the interest of generating C or C++ code is notably to take advantage of the optimizations provided by GCC (or some other compiler). Avoiding generating a file would win you just some milliseconds (you won't even be able to significantly measure the difference).

So just generate your file in some temporary *.c file (you could use hash or timestamp techniques to generate a unique file name), then have GCC compile it into some *.so (perhaps by fork-ing some make process, like I do in MELT), then remove that temporary file (perhaps using atexit).

BTW, this technique is practically compatible with human interaction on current PCs. MELT has a read-eval-print-loop which generates a new C++ file of a few hundred lines, compiles and dlopen-s it, on each interaction, and it is quite usable!

Avoiding the generation of the file is painful, and the gain is absolutely negligible. You might generate it in some tmpfs filesystem on Linux (e.g. in /tmp/). Most of the time would be spent by GCC compiling that file (especially if you compile with some optimization e.g. gcc -O -fPIC -shared somefile.c -o someplugin.so). The time to write it on disk (by your program) and to read it (and even parse it, for C) by GCC is negligible. Use the -ftime-report option of GCC to understand where GCC is spending its time, it is not in parsing as soon as your pass -O to GCC.

Some versions of GCC or of other C compilers might reject stdin as input; some C compilers might want to mmap the C source file (e.g. by using "rm" as mode for fopen on Glibc). In general, compiling something which is not a *.c file as C is non-standard, etc... So better avoid doing that, since the gain is negligible.

If you don't care at all about optimization and want quick compilation of C (not C++) into very slow machine code, consider using instead tinycc (at least on 32 bits machines, on 64 bits it could be buggy) which has a library able to compile some C code inside a string. tcc and its libtcc.a is able to compile very fast (more than 10x times GCC) some C code but the performance of the produced machine code is very bad (very slow, unoptimized code).

Once GCC has compiled your generated C code, you can of course remove the generated source code file. (and you could even also remove the .so after having dlopen-ed it).

BTW, you could use clang++ to compile the generated C++ file. And you might use LLVM (and generate internal LLVM representation, without using any file) instead.

See also this answer.

查看更多
老娘就宠你
3楼-- · 2019-01-20 00:20

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

查看更多
虎瘦雄心在
4楼-- · 2019-01-20 00:24

echo "int main(){}" | gcc -Wall -o testbinary -xc++ -

works but I would like to know how it works and better yet, if there is a way to do this without the need to pipe the contents.

Alternatively you can say (e.g. in a shell-script):

gcc -Wall -o testbinary -xc++ - << EOF
int main(){}
EOF
查看更多
Bombasti
5楼-- · 2019-01-20 00:24

The compiler reads from an input source - either the stdin or a file supplied. You need a pipe to supply something from elsewhere to the compiler. There is no other choice (and of course, some compilers may not have an option to read from stdin either)

查看更多
做个烂人
6楼-- · 2019-01-20 00:31

How about popen("gcc -o -xc++ -", "w");? Gives you aFILE*` but the output goes straight into GCC.

BTW, there is no point in using the -Wall flag. That's for human consumption. In fact, -w -Wfatal-errors makes sense. Either it compiles or it doesn't.

查看更多
登录 后发表回答