I use the smart-compile + extension, it allows you to specify fairly convenient rules depending on the name of the file / buffer you are editing. For example, in my .emacs, I specify the following rule when running smart-compile in the project’s eilers file:
(add-to-list 'smart-compile-alist '("euler.*\\.[Cc]+[Pp]*$" . "g++ -O2 -Wall -pedantic -Werror -Wreturn-type %f -lm -lgmpxx -lgmp -o ../bin/%n && time ../bin/%n") )
First you specify the file name template for the rule, then the path where it should be executed, and then the compilation command. In the above case, I add && time ../ bin /% n to run the program immediately after it has successfully compiled.
source share