For some reason, I am unable to start a process using QProcess on Ubuntu, and I do not understand why...
int main(int argc, char *argv[])
{
//Run the process:
QString procName = "./path/to/executable/Individual";
QProcess *proc = new QProcess();
proc->start(procName);
if(!proc->waitForStarted())
{
std::cout<<"Fail!"<<std::endl;
getchar();
return 0;
}
int exitCode = proc->exitCode();
std::cout<<"Exit code: "<<exitCode<<std::endl;
getchar();
return 0;
}
This always prints 'Fail' to the terminal.
If I type ./path/to/executable/Individual into the terminal, it works just fine, so the path seems to be correct.
Can anyone tell me why the program will not start?
I am using Qt version 5.9.1 on Ubuntu.
Since you're using Linux, you can
sudo ln -s /path/to/executable/Individual /usr/local/bin/individual
then try:
proc->start("individual");
You can try with:
proc->start("cd /usr/local/bin/ && individual");
Related
I want to embed a python code written in version 3.5 in c++. Also, the python script needs to be executed as the whole script repeatedly. My code as follows. It executes well for the first iteration and gives a segmentation fault at the second iteration.
main.cpp:
#include <Python.h>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
int i = 0;
while(i<3){
cout<<"start"<<endl;
Py_Initialize();
FILE* file;
wchar_t* _argv[argc];
for(int i=0; i<argc; i++){
wchar_t *arg = Py_DecodeLocale(argv[i], NULL);
_argv[i] = arg;
}
PySys_SetArgv(argc, _argv);
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));
file = fopen("./example.py","r");
PyRun_SimpleFile(file, "./example.py");
fclose(file);
Py_Finalize();
cout<<"Done"<<endl;
i++;
}
return 0;
}
example.py:
import tensorflow as tf
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_float('learning_rate', 0.01, 'initial learning rate')
def main(argv=None):
print(FLAGS.learning_rate)
if __name__ == '__main__':
main()
I build the project using:
cmake --build . --target Demo -- -j 2
and executed as:
./Demo --learning_rate 0.02
The output was:
start
0.02
Done
start
Segmentation fault (core dumped)
Then I replaced the content of "example.py" as:
print("Hi")
The output was:
start
Hi
Done
start
Hi
Done
start
Hi
Done
How can I fix the above mentioned segmentation fault ?
See the docs
Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_Finalize() more than once.
I'm trying to get Qt creator to print a user input by using a push button on an UI into the terminal. As of now, the code is executable on the terminal via human input. Here is the code:
void MainWindow::on_pushButton_clicked()
{
QProcess::execute("/catkin_ws/devel/lib/submodbus");
system("cd catkin_ws/devel/lib/submodbus");
system("./submodbus_node");
}
Current output when using the code
Output via human input
The versions i'm running on are:
-Ubuntu 16.04
-QT Creator 3.5.1
system can't change the current directory globally. but could use like this:
system("cd /catkin_ws/devel/lib/submodbus && ./submodbus_node");
or using QProcess::setProgram with QProcess::setWorkingDirectory
QProcess p;
p.setProgram("submodbus_node");
//p.setArguments(QStringList()<<args); // if you need
p.setWorkingDirectory("/catkin_ws/devel/lib/submodbus");
p.start();
or QDir::setCurrent
QDir::setCurrent("/catkin_ws/devel/lib/submodbus");
QProcess::startDetached("submodbus_node");
Test demo, create three files in the parent directory:
#include <QApplication>
#include <QProcess>
#include <QDir>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
system("cd ../ && touch test1.txt");
QProcess p;
p.setProgram("touch");
p.setArguments(QStringList()<<"test2.txt");
p.setWorkingDirectory("../");
p.start();
QDir::setCurrent("../");
QProcess::startDetached("touch test3.txt");
return a.exec();
}
I have the following small program:
#include <unistd.h>
#include <pwd.h>
#include <QCoreApplication>
#include <QDir>
const char * homeDir()
{
return getpwuid(geteuid())->pw_dir;
}
int main(int argc, char *argv[])
{
printf("Qt homedir: %s\n", qPrintable(QDir::homePath()));
printf("Native homedir: %s\n", homeDir());
QCoreApplication a(argc, argv);
return a.exec();
}
Now:
when run directly by a "normal" user, ./program, the output is:
Qt homedir: /home/user
Native homedir: /home/usr
which is ok
when run directly by root, ./program, the output is:
Qt homedir: /root
Native homedir: /root
which is ok
when run by root as a different user by the means of sudo, e.g. sudo -u user ./program, the output is:
Qt homedir: /home/user
Native homedir: /home/user
which is ok
when run by root as a different user by the means of startproc, e.g. startproc -u user /full/path/to/program, the output is:
Qt homedir: /root
Native homedir: /home/user
which is NOT ok, or not expected (at least for me)
And my question is: why does the last run give a different result than the others? Is it a bug in Qt (doesn't take into account the fact, that the effective user is different than the real user, or something different), or am I missing some background info (e.g. the mechanism of how startproc works)?
The version of Qt in question is 5.6.1.
Qt's QFileSystemEngine uses the contents of the HOME environment variable on Unix - see its implementation. Yet startproc -u does not set HOME: that's why it fails.
The getpwuid call can be potentially very expensive and can block, i.e. by getting information from an LDAP or AD server, etc., and it's best if you take care of it yourself. Furthermore, it's not thread-safe, and you should use getpwuid_r instead.
An implementation might look as follows:
static QString getHomeDir() {
auto const N = sysconf(_SC_GETPW_R_SIZE_MAX);
auto *buffer = std::make_unique<char[]>(N);
passwd pwd;
passwd *result;
getpwuid_r(geteuid(), &pwd, buffer.get(), N, &result);
if (result) {
auto *dir = result->pw_dir;
auto const decoded = QFile::decodeName(dir);
return QDir::cleanPath(decoded);
}
return {};
}
enum class HomeDir { Default, Init };
QString homeDir(HomeDir option = HomeDir::Default) {
// needs a C++11 compiler for thread-safe initialization
static QFuture<QString> home = QtConcurrent::run(getHomeDir);
return (option == HomeDir::Init) ? QString() : home;
};
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
homeDir(HomeDir::Init);
// do other time-consuming initializations here
QString () << homeDir();
}
I have a simple Qt programm which start's another program with QProcess::execute().
It works under Windows (MSVC 32/64, Mingw-32), Linux (GCC), MAC (CLang).
But if I try to run it in a bash under Windows (Bash on ubuntu on windows) the call to QProcess::execute() never returns.
I've also tried to use QProcess::start() but neither the QProcess::started nor the QProcess::errorOccured signal is issued, and QProcess::start() also never returns.
Is there some incompatibilty with this configuration?
The programm was compiled with GCC 4.8.4 and Qt 5.7 for Linux
The programm looks like this:
#include <QProcess>
#include <QCoreApplication>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv)
QStringList args = app.arguments().mid(1);
int retcode = QProcess::execute("programm", args);
if (retcode == 0)
{
// do something here
}
return retcode;
}
"programm" is an executable located some where in the search path.
I have a ubuntu application and I'm trying to execute bash scripts from it but it doesn't seem to be working. I tried doing this with system()
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
// tried both
system("./script.sh");
// system ("script.sh")
}
Also, i've tried researching this but did not find a solution; is it possible to also read the output and display in textbox.
Use popen().
FILE *script;
char line[LINESIZE];
script = popen("./script.sh", "r");
while (fgets(line, sizeof(line), script)) {
...
}
pclose(script);
It's not relevant that you're running a script. This will work with any shell command.
For anyone looking to do this in QT, here's what i did:
QProcess proc;
proc.start("gnome-terminal", QIODevice::ReadWrite);
if (proc.waitForStarted() == false) {
qDebug() << "Error starting terminal process";
qDebug() << proc.errorString();
return (-1);
}