Zac Gross

Code & More

Running Qt Creator Build Commands as Sudo

| Comments

Recently I had a requirement to install a shared library as a build/deployment step in a QtCreator project, which of course required sudo permissions. Not surprisingly Qt Creator does not simply let you prepend “sudo” to a custom build step, here is the workaround I found:

First I moved all the commands that needed to run with sudo into a single make file like the following, mine was called InstallLib.make:

InstallLib.make
1
2
3
4
5
6
7
install:
  @echo "Installing go shared lib..."
  sudo cp -f libImgSearch.so.1.0.0 /usr/local/lib/
  sudo cp -f libImgSearch.so.1.0 /usr/local/lib/
  sudo cp -f libImgSearch.so.1 /usr/local/lib/
  sudo cp -f libImgSearch.so /usr/local/lib/
  sudo ldconfig

Next in Qt Creator with your project open, goto the project section, add a new “custom process” build step. In the command field type: “ssh-askpass” this program will popup a widget to enter the sudo password when executed. In the build step arguments field enter: “Sudo Password | sudo -S make -f InstallLib.make”. This will make the ssh-askpass program execute the “InstalledLib.make” make file when a correct sudo password is provided.

If you prefer not to use the GUI, you could also edit your projects .user file and add some xml similar to the following:

project.user
1
2
3
4
5
6
7
8
9
   <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.10">
      <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
      <value type="QString" key="ProjectExplorer.ProcessStep.Arguments">Sudo Password | sudo -S make -f InstallLib.make</value>
      <value type="QString" key="ProjectExplorer.ProcessStep.Command">ssh-askpass</value>
      <value type="QString" key="ProjectExplorer.ProcessStep.WorkingDirectory">%{buildDir}</value>
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Custom Process Step</value>
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.ProcessStep</value>
     </valuemap>

Comments