2

I am writing a QT application and I wanted the compiled binary to be compatible with GUI and CLI(installed without X11) environments.

The following is my main function which uses QApplication or QCoreApplication :

int main(int argc, char *argv[]){
    // SOME COMMON CODE will be executed before this
    bool myGUI = getenv("DISPLAY") != 0;

    if(myGUI){
        QApplication a(argc, argv);
        client w; // A MainWindow Object
        w.show();
        doStuff();
        return a.exec();
    }else{
        QCoreApplication a(argc, argv);
        doStuff();
        return a.exec();
    }

    return 1;

}

Now, QT builds the binary having libQtGui as a dynamically shared object. I wanted to know if its possible to rather have the libQtGui loaded dynamically so that it can work in CLI environments without having to install all libraries required by libQtGui.

2 Answers 2

3

It's not practical to attempt this. It's theoretically possible, but you would need to create C wrappers for a huge amount of stuff.

What you can try instead is split the GUI part of your application into its own shared library and dlopen() that. For example, gui.cpp:

// Needs to be extern "C" so that dlopen() can find it later.
extern "C"
int runGui(int argc, char *argv[])
{
    QApplication a(argc, argv);
    client w;
    w.show();
    doStuff();
    return a.exec();
}

You compile the above as a shared library, linking against QtGui. For example:

g++ -c -fPIC $(pkg-config QtGui --cflags) -o gui.o gui.cpp
g++ -shared -o gui.so gui.o $(pkg-config QtGui --libs)

This will give you gui.so, which you can then dlopen() in your main program:

#include <dlfcn.h>

int main(int argc, char *argv[])
{
    // SOME COMMON CODE will be executed before this
    bool myGUI = getenv("DISPLAY") != 0;
    int ret = 0;

    if (myGUI) {
        void* handle = dlopen("./gui.so", RTLD_NOW);
        if (!handle) {
            // Error: dlopen failed
        }
        dlerror(); // Clear/reset errors.

        // Create a function pointer type for runGui()
        typedef int (*runGui_t)(int, char**);

        // Load the address of runGui() and store it in a
        // function pointer. The function pointer name doesn't
        // have to be the same as the function we're loading.
        runGui_t runGui = (runGui_t)dlsym(handle, "runGui");

        const char* dlsym_error = dlerror();
        if (dlsym_error) {
            // Error: dlsym failed.
            // 'dlsym_error' contains the error msg.
        }

        // Call the function as usual by using our 'runGui' pointer.
        ret = runGui(argc, argv);
        dlclose(handle);
    } else {
        QCoreApplication a(argc, argv);
        doStuff();
        ret = a.exec();
    }
    return ret;
}

Note that when building the above main.cpp, you must not link against QtGui, so that it will run on systems where libQtGui.so is not available. In that case, dlopen() will fail to load gui.so. At that point you can fall back to your non-GUI code (I didn't do that in the above example.)

0

This is not a question about Qt, it is about c++

You can find answer here Dynamically load a function from a DLL

But basically i think this is bad idea, if you want a pure console application or mixed console/gui application, you should resolve it at compile time with #ifdef.

#ifdef WITH_GUI
if(myGUI){
    QApplication a(argc, argv);
    client w; // A MainWindow Object
    w.show();
    doStuff();
    return a.exec();
}else{
#endif    
    QCoreApplication a(argc, argv);
    doStuff();
    return a.exec();
#ifdef WITH_GUI
}
#endif

And add some launch args. For example ./myapp --start-gui for version compiled with gui support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.