gpt4 book ai didi

c++ - 动态加载 QTGui

转载 作者:搜寻专家 更新时间:2023-10-31 01:48:26 25 4
gpt4 key购买 nike

我正在编写一个 QT 应用程序,我希望编译后的二进制文件与 GUI 和 CLI(未安装 X11)环境兼容。

以下是我使用 QApplication 或 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;

}

现在,QT 构建具有 libQtGui 作为动态共享对象的二进制文件。我想知道是否可以动态加载 libQtGui,以便它可以在 CLI 环境中工作,而无需安装 libQtGui 所需的所有库。

最佳答案

尝试这样做是不切实际的。这在理论上是可行的,但您需要为大量内容创建 C 包装器。

您可以尝试将应用程序的 GUI 部分拆分为它自己的共享库和 dlopen()。例如,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();
}

你将上面的编译为一个共享库,链接到 QtGui。例如:

g++ -c -fPIC $(pkg-config QtGui --cflags) -o gui.o gui.cppg++ -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;
}

请注意,在构建上述 main.cpp 时,您不得链接到 QtGui,以便它可以在 libQtGui.so 不可用的系统上运行。在这种情况下,dlopen() 将无法加载 gui.so。那时你可以回退到你的非 GUI 代码(我在上面的例子中没有这样做。)

关于c++ - 动态加载 QTGui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17878779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com