Making a stand alone program on Linux
-
I recently started using QT. I have version 5.6 installed on Linux Mint 17.3. I wrote a C program that just uses a simple terminal interface that crunches some numbers while a simple printf() displays status. The program runs fine while in the QT IDE.
However, the compiled program does not execute when I click on it from the directory folder, nor if i type its name when in terminal and in its directory.
What am I missing here? Does QT IDE have to be running to run the program? I want to simply run the program by clicking on it, or enter terminal and type its name, or from a system() call from another program. What do I need to set or change to make this work?
-
I recently started using QT. I have version 5.6 installed on Linux Mint 17.3. I wrote a C program that just uses a simple terminal interface that crunches some numbers while a simple printf() displays status. The program runs fine while in the QT IDE.
However, the compiled program does not execute when I click on it from the directory folder, nor if i type its name when in terminal and in its directory.
What am I missing here? Does QT IDE have to be running to run the program? I want to simply run the program by clicking on it, or enter terminal and type its name, or from a system() call from another program. What do I need to set or change to make this work?
What am I missing here?
You are probably missing the whole loader conundrum, but what errors you get from the app when you try to run it in the terminal? Is it "ld can't find whateverlibrary"?
- If so, you either install that library in a location known to the loader (also called dynamic linker on Linux). This is usually
/lib
or/usr/lib
and you usually do that through your distro's repository. - Or, you set up the path from the command line
LD_LIBRARY_PATH=/path/to/library/ld/cant/find:$LD_LIBRARY_PATH ./myexecutable
. - Or, you set the
rpath
field in the ELF header of he application.
My advice is go with 1, and if it's impossible then switch to 3. Number 2 is mostly discouraged. Also, please, do search the forums, I remember answering a very similar question yesterday, or the day before.
Does QT IDE have to be running to run the program?
Nope. This is all a Linux specific issue.
Kind regards.
- If so, you either install that library in a location known to the loader (also called dynamic linker on Linux). This is usually
-
Hi, just to add to @kshegunov, if your app runs fine when launched from inside Qt Creator, but not outside of it, most likely it's Qt's dlls/.so files that are not seen find by the app.
When you start your app from Qt Creator, it injects a
LD_LIBRARY_PATH=/home/yourname/Qt/5.6/gcc_64/lib
into your app's environment (@kshegunov's no.2 above) so that your app will find Qt's .so files and start ok.But it should run fine anyway, because when building your program, Qt inserts the same path (
/home/yourname/Qt/5.6/gcc_64/lib
) into your program's rpath so it should start also outside of Qt Creator.Perhaps check that rpath with the chrpath utility?
-
It looks like the problem is not with my program, but with it being a console program. In the QT IDE when I run, a terminal window opens and it works correctly. A text menu shows from using printf() and I choose an option what to do that gets its input from a scanf(); Standard C stuff.
Outside of QT, my program is launching, but the error I get is program not found. I though that meant my program, but it appears it cannot find the terminal or command shell program. My program writes a text log file for any errors encountered. I wrote myself a message and it created the file so my program is executing before it exits at the first printf().
I am not sure what program QT is trying to open to create a console. The IDE seems to be opening a console that is part of QT instead of the Linux one.
The same program compiled and run on my WIndows machine opens a command shell when run automatically. I tried executing the Linux version from the command line while in terminal as root. My program runs, creates and writes my test message file then exits at the first printf() because it cannot open whatever it is that QT opens when run from the IDE.
-
I see, a console program. When you start a console program from inside Qt Creator, it starts a child process called qt_creator_process_stub which then runs the usual /bin/sh shell and your program with it.
So it should really behave the same way as when you start your app from Terminal.Funny though, I remember having also problems with printf(), not that my console program crashed, rather that there was no output (I just now created a simple console test program with a printf("Hello world"); to see if it crashes, but of course it runs just fine, both from Qt Creator and from Terminal).
Anyway, I remember getting around that printf() problem by #including <QTextStream> and setting up a QTextStream instance to stdout, putting out text this way:
QTextStream cout(stdout); cout << "Hello world" << endl;
Maybe it'll work you too.
P.S. Funny about your RPATH just having $ORIGIN and not also :/home/yourname/Qt/5.6/gcc/lib sure you're not doing anything fancy in your .pro file, like QMAKE_LFLAGS or such?
-
-
Sure, it could be some missing special sauce/setting you need.
You could try creating a simple "Hello World" Qt Console Application using Qt Creator's New Project wizard. If printf() works in that app, then try copy/paste your C code into it:
#include <QCoreApplication> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // your C code goes here printf("Hello Qt\n"); // last line return a.exec(); }
-
I am using a fresh install of QT using default everything. I merely copied my C source (not C++) over to my Linux machine and compiled. The program is POSIX compliant code.
Maybe I need to include a QT special header to get the console to come up.