AlisonXuuu 2019-04-09 20:53 采纳率: 0%
浏览 2219

Linux系统下Qt Creator中使用QProcess调用外部程序没有输出。

如题。
可执行的独立程序如下,是用C语言实现的哲学家就餐问题

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#include<semaphore.h>
#define N 3
#define LEFT i
#define RIGHT (i+1)%N

sem_t mutex[N];
int id[N]={0,1,2};
pthread_t thread[N];
void* solve(void*param){
    int i=*((int*)param);
    while(i>=0){
        if(i%2==0){
           sem_wait(&mutex[LEFT]);
           sem_wait(&mutex[RIGHT]);
           printf("phlio %d is eating\n",i);
           sleep(1);
           sem_post(&mutex[RIGHT]);
           sem_post(&mutex[LEFT]);
           printf("phlio %d is over\n",i);
           sleep(1);
        }else{
           sem_wait(&mutex[RIGHT]);
           sem_wait(&mutex[LEFT]);
           printf("phlio %d is eating\n",i);
           sleep(1);
           sem_post(&mutex[LEFT]);
           sem_post(&mutex[RIGHT]);
           printf("phlio %d is over\n",i);
           sleep(1); 
        }
    }
}
int main(){
    int i=0;
    for(i=0; i<N; i++){
       sem_init(&mutex[i],0,1);
       pthread_create(&thread[i],NULL,solve,&id[i]);
    }
    for(i=0; i<N; i++){
       pthread_join(thread[i],NULL);
    }
    return 0;
}

Qt中使用按钮启动外部C程序,已经尝试过的方法如下(4种方法):

//第一种方法:
void Dialog::on_pushButton_clicked()
{
   QStringList strList;
   strList <<"null"<<"null";
   QProcess::startDetached("/home/alison/ph1/a.out",strList,"/home/alison/ph1");
}

//第二种方法:
void MainWindow::on_pushButton_clicked()
{
    QProcess *process=new QProcess;
    QStringList args;
    args<<"./a.out";
    process->start("/home/alison/ph2/a.out",args);
}

//第三种方法:
void MainWindow::on_pushButton_clicked()
{
    QProcess *process=new QProcess;
    QStringList str;
    str <<"";
    process->start("/home/alison/ph2/a.out");
}

//第四种方法:
void MainWindow::on_pushButton_clicked()
{
    QProcess *process=new QProcess;
    QProcess::execute("/home/alison/ph2/a.out");
}

Qt程序编译没有错误,但运行之后点击按钮没有相应的C程序运行的结果
希望各位大佬可以帮我解答一下哪里需要修改,十分感谢!

  • 写回答

3条回答 默认 最新

  • 恰同学少年12138 2019-12-25 16:01
    关注

    楼主,刚好遇到跟你一样的问题,我尝试了你的四种方法,确实不行。我的解决办法是:将要调用的可执行文件与主程序的可执行文件放在同一个路径下,然后在
    QProcess::execute("a.out")中直接写入可执行文件名称即可。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog