简体   繁体   English

Qt在系统调用时崩溃了吗?

[英]Qt crashes on system call?

I have the following cpp file: 我有以下cpp文件:

#include <iostream>                  
#include <stdlib.h>
#include <cstdlib>
using namespace std;

  int main(int,char*[])
  {
    int b = std::system("dot -Tdot ./a.dot -o ./a3.dot");
    cout << "ret: " << b << endl;
    return 0;
  }

Running from cmd with: 从cmd运行:

g++ -O2 -Wall -pedantic -pthread q2.cpp -lboost_graph && ./a.out

Runs fine creating a3.dot 运行良好创建a3.dot

Running the same file with qtcreator I get: 使用qtcreator运行相同的文件我得到:

Segmentation fault (core dumped)
ret: 35584

This is the compiler output from Qt 这是Qt的编译器输出

make: Entering directory `/home/studnet/QtProjects/T2-build-desktop-Qt_4_8_1_in_PATH__System__Release'
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_DECLARATIVE_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../T2 -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtDeclarative -I/usr/include/qt4 -I../T2/qmlapplicationviewer -I. -I../T2 -I. -o main.o ../T2/main.cpp
g++ -Wl,-O1 -o T2 main.o qmlapplicationviewer.o moc_qmlapplicationviewer.o    -L/usr/lib/i386-linux-gnu -lQtDeclarative -lQtGui -lQtCore -lpthread 

I tried debugging it in Qt, but I am fairly new to it, and get an assembly Window. 我尝试在Qt中调试它,但我相当新,并获得一个程序集Window。 I put a.out in the build folder from where qt usually reads the file and in the source folder just in case. 我把a.out放在build文件夹中,qt通常读取文件,在源文件夹中以防万一。

Any suggestions how I can fix this? 有什么建议我可以解决这个问题吗?

Thank you. 谢谢。

EDIT: 编辑:

I tried this: 我试过这个:

QProcess process;
process.start("dot", QStringList() << "-Tdot ./a.dot -o ./a3.dot");
process.waitForFinished(-1);

But then I get no file. 但后来我没有文件。

Your question is quite when you claim "running it with Qt". 当你声称“用Qt运行它”时,你的问题就很明显了。 I will assume you mean qmake at this point based on the lack of QtCreator or any other IDE tag for the question. 我会假设你的意思是qmake ,因为缺少QtCreator或问题的任何其他IDE标签。

Not that it should crash, but you seem to have used a GUI project when creating it even though this seems to be a command line based application. 不是它应该崩溃,但你似乎在创建它时使用了一个GUI项目,即使这似乎是一个基于命令行的应用程序。 This is the proof for that: 这证明了这一点:

-I../T2/qmlapplicationviewer

I would encourage you to either reconfigure it or create a new one like that. 我鼓励你重新配置或者创建一个新的。 Basically, the code with the project file below works fine for me. 基本上,下面的项目文件的代码对我来说很好。

main.cpp main.cpp中

#include <iostream>                  
#include <cstdlib>

using namespace std;

int main(int,char*[])
{
  int b = std::system("dot -Tdot ./a.dot -o ./a3.dot");
  cout << "ret: " << b << endl;
  return 0;
}

main.pro main.pro

TEMPLATE = app
TARGET = main
QT -= core gui
SOURCES += main.cpp

Build and run 建立并运行

qmake && make

In order to reply to your lat edit: 要回复您的lat编辑:

QProcess process;
process.start("dot", QStringList() << "-Tdot ./a.dot -o ./a3.dot");
process.waitForFinished(-1);

This is wrong. 这是错的。 You will need re-read how the start method works: 您需要重新阅读start方法的工作原理:

void QProcess::start(const QString & program, const QStringList & arguments, OpenMode mode = ReadWrite void QProcess :: start(const QString&program,const QStringList&arguments,OpenMode mode = ReadWrite

You need to pass the arguments as string list, rather all the arguments as a string within a string list. 您需要将参数作为字符串列表传递,而将所有参数作为字符串列表中的字符串传递。 Thereby, the correct code would be something like this: 因此,正确的代码将是这样的:

QProcess process;
process.start("dot", QStringList()
                      << QString("-Tdot")
                      << QString("./a.dot")
                      << QString("-o")
                      << QString("./a3.dot"));
process.waitForFinished(-1);

Please note that for either of two approaches, you may need to use the correct path rather than the one assumed to be in the folder where the application is rum from as it is currently presented in both snippets. 请注意,对于两种方法中的任何一种,您可能需要使用正确的路径而不是假定位于应用程序朗姆酒的文件夹中的路径,因为它目前在两个片段中都显示。

You could try to use absolute paths either statically or dynamically by getting the application dir from the application instance. 您可以尝试通过从应用程序实例获取应用程序目录来静态或动态地使用绝对路径。 Alternatively, you could also set the working directory on the QProcess instance. 或者,您也可以在QProcess实例上设置工作目录

This is especially important when it comes to for instance QtCreator because the IDE tends to change the build directory to a custom one not to produce the build files beside the source files for having a clear separation. 这对于例如QtCreator来说尤为重要,因为IDE倾向于将构建目录更改为自定义目录,而不是在源文件旁边生成构建文件以实现明确的分离。 If you do not do this, the files might still be generated by your invoked process, but not in the source directory where you would look for it, but the build directory. 如果不这样做,文件可能仍然由您调用的进程生成,但不会在您查找它的源目录中生成,而是生成目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM