Can GCC compile and run a source code without generating object or executable files?

Can GCC compile and run a source code without generating any output file (neither object nor executable), in a manner that is supported cross-platform? Especially, a solution supported by GCC directly.

I want to avoid generation of any trace file since that is a minor code in a big project. It just messes up the bin directory.

An existing question, here, provides a solution for compiling source code without generating any output file, such as:

gcc somefile.c -o /dev/null

However, this only compiles, and doesn't run.

Another similar question here provides a solution that is specific to Windows OS, not cross-platform.


Solution 1:

A simple bash script might help:

#!/bin/bash

echo 'compile... ' $1
gcc $1 && ./a.out && rm a.out

supposed it's named once, then you can do

$ sh once any.c

to compile any.c and just run it once.
You can also make once executable with chmod +x once so you can just type

$ once any.c

Hope it helps ;)

Solution 2:

In order to compile and run the C / C++ program and then remove the compiled file, you should add a function to delete the program after it is executed.

Here is a link to an example of a program that deletes itself. Click Here

Solution 3:

In your case (you want to avoid cluttering the build tree), a practically useful solution might be to have some convention about temporary executables.

For example, you could decide that every intermediate executable or file is named *.tmp or _* or *.tmpbin (for temporary binaries) and have some Makefile rules which removes them. Or you could use mktemp(1) in your Makefile to get a temporary file name. Don't forget to remove it later.

Also, most big projects have a compilation step and an installing step (often make install); and if you don't have that you probably should. You want your installing step to avoid installing the temporary binaries or files; with some naming convention this is quite simple: the first command for install phony target in your Makefile would remove these temporary binaries or files.

Also, you generally build in a file tree different of the final bin/ directory, so you could leave the temporary executables in the build tree.

As several people noticed, removing its own executable is easy on Linux (do a readlink(2) on "/proc/self/exe" (see proc(5) for details) then unlink(2) the result of readlink....) but difficult on Windows.

So practically your question is not a very important issue.... (if you use suitable build conventions). And GCC work on files (because it will run ld internally to build that executable file); however GCCJIT is hiding them. AFAIK, you won't even be able to use /dev/stdout as the executable output of gcc (but you can run gcc -x c /dev/stdin to compile C code from stdin). So GCC cannot avoid making an executable file (but you could have it temporary, or in a tmpfs file system or a FUSE one). So you need something external to your gcc command (perhaps simple an rm in some following line of your Makefile) to remove the produced executable.

You could also decide to have (dynamically loaded) plugins (e.g. use dlopen(3) on Linux). Your main program could load a plugin (with  dlopen on Linux) - perhaps even after having generated dynamically its C++ code and having compiled that generated code into e.g. some shared object .so on Linux (or some DLL on Windows), as I do in MELT -, run functions in it obtained with dlsym, and unload the plugin (with dlclose on Linux) and finally remove it. You might use cross-platform frameworks like Qt or POCO to avoid dealing with OS specific plugin code.

Solution 4:

For c gcc/g++ filname.c && ./a.out && rm a.out

For c++ g++ filename.cpp && ./a.out && rm a.out