Execute project in Qt Creator as root

2019-07-12 18:59发布

问题:

I have an existing cmake project which i am opening with qtcreator. it is build successfully but the generated executable has to be run as root since it needs a connection to the eth0 interface.

I have no possibility to add these "custom build settings" (it's disabled) and if i try to

ssh-askpass Sudo Password | sudo -S bash /to/my/executable/file

i got an

cannot execute binary file

error. i don't want to run qtcreator as root. is there any possibility to ask for the root password while running the executable. i am pretty new to cmake and qtcreator.

i'd appreciate any help.

thanx!

回答1:

Just create a one-line file at the same location than your executable:

Content of launcher.sh:

echo "root password" | sudo -S ./executable

Don't forget to chmod:

sudo chmod a+x launcher.sh // Yeah it's dirty I know!

Then, launch QtCreator normally, open your project, go to the settings and create a new Run configuration. The executable should be your launcher.sh file. Tick the checkbox "Run in terminal"



标签: qt cmake root sudo