PDA

View Full Version : Errors: run command with system call in a qt application



newcfd
17th September 2015, 04:21
I installed a MPI program in /opt/mpi. In my Qt code, I use system( "mpiexec -machinefile host -n 1 app.exe") call to run the command, the code complains mpiexec can not be found
Although /opt/mpi/bin is in the path. There is no issue when I run the command manually. To solve this problem, I have to add the absolute path in front of mpiexec, like
system( "/opt/mpi/bin/mpiexec -machinefile host -n 1 app.exe").

I wrote a small C++ code to test the command above

#include <stdio.h>
#include <stdlib.h>

int main()
{
system( "mpiexec -machinefile host -n 1 app.exe" );
return 0;
}

the command runs successfully. It looks like system call inside Qt does not recognize the settings in Path.

Another problem is that app.exe uses a library abc.so in /opt/app/lib. When I run /opt/mpi/bin/mpiexec -machinefile host -n 1 app.exe, the code complains about abc.so can not be found.
However, from a pure C++ code or command line, no problem at all because I set abc.so path in LD_LIBRARY_PATH. It must be a Qt problem.

system call inside Qt ignores lib path too.

What is the root reason? Thanks for your help in advance.

anda_skoa
17th September 2015, 10:08
Are you sure that you run the Qt program in an enviroment that has these variables set they way you think they are?

What does


qDebug() << qgetenv("PATH") << qgetenv("LD_LIBRARY_PATH");

say?

Btw, a nicer way to run an external program in Qt, especially if the Qt program has a UI (and therefore should not block), is to use QProcess.

Cheers,
_

newcfd
17th September 2015, 18:52
Thank you for your reply. There is no output for the settings in the bash file. It is obvious that settings of PATH and LD_LIBRARY_PATH in bash file are ignored.
I tried with system( "bash -c 'mpiexec -machinefile host -n 1 app.exe'" ); No help.
Even when I copy abc.so to the same folder of app.exe, the error is still there. Right, current dir :.: is not included in the PATH too.
I will try QPorcess.

newcfd
24th September 2015, 17:54
qDebug() << qgetenv("PATH") << qgetenv("LD_LIBRARY_PATH");
Library path is empty. Is there a way to set LD_LIBRARY_PATH inside a Qt application?

d_stranz
25th September 2015, 23:21
Maybe you want to look at QProcessEnvironment. Note that you cannot change the current environment this way, but you can change the environment you pass to a QProcess instance.

newcfd
30th September 2015, 21:57
good to know. Thanks.