Issue
I am wanting to measure the time it takes for my C++ video processing program to process a video. I am using CLion to write the program and have Cmake set up to compile and automatically run the program with a test video. However, in order to find execution time I have been using the following command in the MacOS terminal:
% time ./main ../Media/test_video.mp4
Is there a way for me to configure Cmake to automatically include time
in the execution of ./main
to streamline my process further?
So far I've tried using set(CMAKE_ARGS time "test_video.mp4")
and some command line argument functions but they don't seem to be acting in the way that I'm looking for.
Solution
It is possible to use add_custom_target
to do what you want. I'll not consider this option further as it seems abusing the build system for something it wasn't designed to do. Yet it may have an advantage over using CLion configuration: it would be available to be used outside of CLion. That advantage seems minor: why not run the desired command directly in those contexts?
The first CLion method would be to define an external tool which run time on the current build target. In File|Settings...|Tools|External Tools define a new tool with /bin/time
as program, $CMakeCurrentProductFile$ $Prompt$
as arguments. When choosing that tools (in Tools|External Tools) it will now prompt you for the argument and then run /bin/time
on the current target with the provided arguments. Advantage: you don't have to define the tool once, it will be available in every project. Inconvenients: the external tools are available in every project, thus it doesn't make sense to be more specific than $Prompt$
for the arguments and the working directory; it isn't possible to specify environment variables, it isn't possible to enforce the need of a build before running the command.
The second CLion method would to be define a Run/Debug Configuration. Again use /bin/time
as program (chose "Custom Executable"), specify $CMakeCurrentProductFile$
as first argument (here it makes sense to provide the other arguments as desired, but note that $Prompt$
is still a valid choice if needed). Advantages: it makes sense to be as specific as needed; you have all the feature of configurations (environment variables, input redirections, specifying actions to be executed before the execution). Inconvenient: it doesn't work with other CLion features which assume that the program is the target such as the debugger, the profiler, ... so you may have to duplicate configurations to get them.
Note that the methods aren't exclusive: you can define an external tools and then add configurations for the case where it is more convenient.
Answered By - AProgrammer Answer Checked By - Gilberto Lyons (WPSolving Admin)