Compile string of C code

前端 未结 2 487
面向向阳花
面向向阳花 2021-02-08 23:51

Really off the wall question here, but is there a way to compile a string of C code in GCC without any medium to hold that string (eg. a source file)?

Something along th

2条回答
  •  一整个雨季
    2021-02-09 00:48

    If you use - as the input file in the command line, gcc reads from standard input. Since there is no file name extension gcc could use to find out the language that should be compiled, it has to be specified with the -x flag:

    $ gcc -x c -o tst - < #include 
    > int main(void) {
    >   printf("Hello world\n");
    > }
    > EOF
    $ ./tst
    Hello world
    

提交回复
热议问题