Reputation: 2315
I have the following cpp file:
#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:
g++ -O2 -Wall -pedantic -pthread q2.cpp -lboost_graph && ./a.out
Runs fine creating a3.dot
Running the same file with qtcreator
I get:
Segmentation fault (core dumped)
ret: 35584
This is the compiler output from 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. I put a.out
in the build
folder from where qt usually reads the file and in the source folder just in case.
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.
Upvotes: 0
Views: 1321
Reputation: 53225
Your question is quite when you claim "running it with Qt". I will assume you mean qmake
at this point based on the lack of QtCreator or any other IDE tag for the question.
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. 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.
#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;
}
TEMPLATE = app
TARGET = main
QT -= core gui
SOURCES += main.cpp
qmake && make
In order to reply to your lat edit:
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:
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.
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. 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.
Upvotes: 2