2

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.

Community
  • 1
  • 1
ar2015
  • 5,558
  • 8
  • 53
  • 110
  • you mean object file ? – Jagannath Jul 14 '15 at 05:01
  • @Jagannath I want to avoid any binary or object file. Just want to run what is supposed to be built. – ar2015 Jul 14 '15 at 05:02
  • lol. Binary is what it's support to be built and run. – Jagannath Jul 14 '15 at 05:04
  • @ar2015 What are you trying to achieve? – Pradhan Jul 14 '15 at 05:04
  • @Pradhan I have a cpp file inside a project. I don't want it mess the directory with adding new files. I also prefer avoid `/tmp` as it is no cross OS compatible. – ar2015 Jul 14 '15 at 05:06
  • 3
    It sounds like you want to get this to run without leaving any trace that the file exist. Is that your goal? – wuno Jul 14 '15 at 05:06
  • @NichoDiaz Yes. you are right. – ar2015 Jul 14 '15 at 05:06
  • `gcc somefile.c -o /dev/null` also genrates compiled and linked executable but `/dev/null` discards all data written to it. – Ashwani Jul 14 '15 at 05:07
  • @AshwaniDausodia however, `/dev/null` does not run the executable nor is it cross OS compatible. – ar2015 Jul 14 '15 at 05:08
  • @ar2015 Why do you want to run it in the first place? Why wouldn't the syntax check mentioned in the linked question suffice? If the only thing you want to control is the location where the binary is built(for example, outside your source directory), there will be a solution specific to your build system. – Pradhan Jul 14 '15 at 05:08
  • What if after the program is executed it deletes the compiled file? – wuno Jul 14 '15 at 05:09
  • @Pradhan I am sure that the source has no error and it is a small minor code. – ar2015 Jul 14 '15 at 05:10
  • 1
    http://stackoverflow.com/questions/3457040/how-to-write-a-program-in-c-such-that-it-will-delete-itself-after-execution – wuno Jul 14 '15 at 05:13
  • @NichoDiaz Thank you very much. It does the job. However, i do not delete the question just in case there is any gcc based solution. – ar2015 Jul 14 '15 at 05:16
  • Cool ill post the answer than so you can accept. – wuno Jul 14 '15 at 05:17
  • You could use the idea that @NichoDiaz pointed to, along with [`std::remove`](http://en.cppreference.com/w/cpp/io/c/remove). Looks like that might be more platform independent. – Pradhan Jul 14 '15 at 05:18
  • [GCCJIT](https://gcc.gnu.org/wiki/JIT) is internally doing that, at least on Linux. Actually it is generating temporary files and removing them. – Basile Starynkevitch Jul 16 '15 at 07:00
  • Please **edit your question** to improve it. Give the real motivations and some context. Do you care about trace debug files or about temporary executables? – Basile Starynkevitch Jul 23 '15 at 05:53
  • @BasileStarynkevitch, I prefer avoid generating any file and no change on disk. I don't have aim of using tracing of debug files too. – ar2015 Jul 23 '15 at 06:06

4 Answers4

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks Basil for your answer. However it is very important for me to hear if it is possible by GCC directly or not. I don't know why people avoid talking about it. Even saying `No` does not make me annoyed. But at least I know that I should not continue searching for it. – ar2015 Jul 23 '15 at 05:27
  • I think the problem is not compilation on wind, but running a generated executable file on the wind. – ar2015 Jul 23 '15 at 05:29
3

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 ;)

Hux
  • 1,427
  • 1
  • 9
  • 6
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

Community
  • 1
  • 1
wuno
  • 9,547
  • 19
  • 96
  • 180
1

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

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

Pratap07
  • 41
  • 3