Question

I need to run a program after my main program exits, is it possible to get the main program to run it on exit?

Was it helpful?

Solution 3

The simplest way to run something after main ends in a C++ program, is to do it in the destructor of a static object.


If you're talking about after the program terminates, then that is wholly outside the realm of standard C++. You'd have to use OS-specific means for that. However, the simplest then (assuming you need the whole thing to happen automatically) is to invoke your program via a command or script that runs first your program, and then the second program.

OTHER TIPS

You could make a script (or again better another program) which executes the your two programs in sequence.

You might find atexit useful (not sure what's your actual need). It will run the registered function just before your program ends. Note that in order for the code to run after your program ends, you must create a new process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top