Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Linker error



  • Hey, I seem to get this linker error whenever i try to use anything from qt5, including it goes fine, but as soon as i call anything it bugs.

    I am using qt5 inside of a dll to handle http requests but it even happens when i just construct a QApplication. Someone mentioned it could be because I didn't link qt5 and visual studio, but i have no clue on how to solve this.
    error:

    1>dllmain.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall QApplication::QApplication(int &,char * *,int)" (__imp_??0QApplication@@QAE@AAHPAPADH@Z)
    1>dllmain.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QApplication::~QApplication(void)" (__imp_??1QApplication@@UAE@XZ)
    1>dllmain.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (__imp_?exec@QApplication@@SAHXZ)
    

    code:

    int myLibraryExec() {
    	char* appName = (char*)"Project";
    	int argc = 1;
    	QApplication app(argc, &appName);
    	return app.exec();
    }
    
    int main() {
    	auto app = myLibraryExec();
    	std::cout << app << std::endl;
    	return 0;
    }
    

    Thanks in advance for the help!



  • @Mehodin

    Try this here:

    int myLibraryExec(int argc, char *argv[]) {
    	char* appName = (char*)"Project";
    	int argc = 1;
    	QApplication app(argc, argv);
    	return app.exec();
    }
    
    int main(int argc, char *argv[]) {
    	auto app = myLibraryExec( argc, argv );
    	std::cout << app << std::endl;
    	return 0;
    }
    

    You seem to confuse QApplication. The parameter list of main is really old back from C, which keeps a couple of surprizes.

    Check it out. Just typed brain to keyboard. Not tested and not compiled.



  • @koahnig

    gives me error saying
    1>c:\users\mehodin\documents\github\project\dllmain.cpp(26): error C2082: redefinition of formal parameter 'argc'

    and if i completely remove

    	char* appName = (char*)"Project";
    	int argc = 1;
    

    it gives same error as original post



  • @Mehodin

    Sorry, my fault, try this:

    int myLibraryExec(int argc, char *argv[]) {
    	QApplication app(argc, argv);
    	return app.exec();
    }
    
    int main(int argc, char *argv[]) {
    	auto app = myLibraryExec( argc, argv );
    	std::cout << app << std::endl;
    	return 0;
    }
    


  • @koahnig

    1>dllmain.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall QApplication::QApplication(int &,char * *,int)" (__imp_??0QApplication@@QAE@AAHPAPADH@Z)
    1>dllmain.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QApplication::~QApplication(void)" (__imp_??1QApplication@@UAE@XZ)
    1>dllmain.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static int __cdecl QApplication::exec(void)" (__imp_?exec@QApplication@@SAHXZ)
    

    sadly didn't solve it. Since it's a dll, will it even pass argc and argv?

    int myLibraryExec(int argc, char *argv[]) {
    	QApplication app(argc, argv);
    	return app.exec();
    }
    
    int main(int argc, char *argv[]) {
    	auto app = myLibraryExec(argc, argv);
    	std::cout << app << std::endl;
    	return 0;
    }
    
    BOOL APIENTRY DllMain(HMODULE hModule,
    	DWORD  ul_reason_for_call,
    	LPVOID lpReserved
    )
    {
    	if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
    		hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)main, (LPVOID)hModule, 0, NULL);
    		if (hThread == NULL) {
    			return 0;
    		}
    	}
    	else if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
    	}
    	else if (ul_reason_for_call == DLL_PROCESS_DETACH) {
    	}
    	else if (ul_reason_for_call == DLL_THREAD_DETACH) {
    	}
    	return TRUE;
    }
    
    

  • Lifetime Qt Champion

    Hi,

    Might be a silly question but are you linking your .dll against Qt's widgets module ?