Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C code without having it saved in a file

Tags:

c

compilation

Inspired by this PCG challange: https://codegolf.stackexchange.com/q/61836/31033 I asked my self, if one would try to leave as few trace as possible when compiling such kind of tool (no matter of a browser or something else), is there some way (aimed for gcc/clang as this probably are the preinstalled commandline compillers in such a working enviroment) to hand over source code to the compiler as command line argument or equal mechanism, without need for the source code beeing saved as *.c file, as the user would usually do?

(ofcourse the compiler will produce temp files while compiling, but those probably won't get scanned.)

like image 530
dhein Avatar asked Oct 29 '15 11:10

dhein


People also ask

Can you run C code without compiling?

If you only have a ". c" file, then you don't have a program. You must compile the program first to make an executable, and then you must run the program to get its output.

Can I write C program in notepad?

Though you can write "C" code in Notepad, you must have a C compiler, such as the compiler included with the Microsoft Visual Studio development suite, to compile the code. To write a C code file in Notepad, type your C code into a blank page in the text editor, and then save the file with a ".

Do you need a main C file?

You can compile individual files without main , but you cannot link them and of course cannot run them since they are not complete programs. Note that valgrind is not a static analysis tool but a runtime tool, and therefore it is useless on individual translation units not linked into a runnable program.


1 Answers

At least gcc can as it is able to read source from the standard input. You can also use Unix here string bash construction :

gcc -xc - << "int main() { exit(0); }"

or here file sh construction :

gcc -xc - <<MARK
int main() {
  exit(0);
}
MARK

----EDIT----

You can also imagine using cryptography to encode your source, uncipher the content on the fly and inject the result to the standard input of gcc, something like:

uncipher myfile.protected | gcc -xc -
like image 132
Jean-Baptiste Yunès Avatar answered Oct 16 '22 17:10

Jean-Baptiste Yunès