0

TL;DR: How can I make CLion kill off any running versions of the code before building the new one?

I've started playing with CLion for C++. I noticed that I often forget to stop the last iteration before trying to compile again, which leads to "Permission denied" errors -- when I first encountered this, it took me almost half an hour of fiddling with permission settings before realizing that it was because the old version was still running, and therefore couldn't be replaced with the new executable.

As far as I can tell, there's no way to do this in CMake without embedding a Batch (since I'm on Windows) script. I'd like to avoid that, because it'd be a lot of unnecessary complexity for... not that much reward.

In short, is there an option in CLion or something in CMake that will stop the previous iteration when running the new one?

Fund Monica's Lawsuit
  • 5,770
  • 8
  • 46
  • 65

2 Answers2

3

Yes there is. Simply press ctrl + F2 or goto Run > Stop to terminate the previous iteration in case it keeps running.

Alternatively, you can set to run only single instance. This way previous instance will always be terminated before running the new one. To enable this, goto Run | Edit Configurations and select Single Instance Only.

  • The last half of your answer actually answers my question, which was how to automatically terminate the previous instance before starting another. Thanks! – Fund Monica's Lawsuit Jun 09 '17 at 13:32
  • @QPaysTaxes That's great to hear. I am still leaving the first part in the answer, in case someone want's to _manually_ terminate it (often helpful in the case when you close the console without stopping the debugger and the process just won't die). – Saurabh Shrivastava Jun 09 '17 at 16:57
0

As far as I know, this is not possible by default. One solution I have found was to create a batch file with the following content:

@echo on
tasklist /FI "IMAGENAME eq %1" 2>NUL |find /I "%1">NUL
if "%ERRORLEVEL%"=="0" taskkill /F /im %1

(Second line checks whether the process is running - found here: How to check if a process is running via a batch script)

And edit the build configuration to make CLion call the batch file and pass the processname to it before every build.

This is how it works:

  1. Run > Edit Configurations > select the configuration to change (in my case "Build All")
  2. In the "Before launch: External..."-section click the green plus
  3. Choose "Run external tool" and click the green plus in the pop-up window
  4. Choose a name for the tool
  5. Add the path of the batchfile in the "Program:" field
  6. Write $ProjectName$.exe into the "Parameters:" field
  7. Click ok until you are back in the config window
  8. Give the script a sufficient priority by selecting it and clicking the arrow up

Now it should try to kill the running process before every build.

Community
  • 1
  • 1
DJJD
  • 1
  • 2