這是不實際的嘗試。這在理論上是可行的,但你需要爲大量的東西創建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.cpp
g++ -shared -o gui.so gui.o $(pkg-config QtGui --libs)
這會給你gui.so
,然後你就可以執行dlopen()在主程序:
#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代碼(我在上例中沒有這樣做)。