unresolved external symbol \"__declspec(dllimport)

2019-09-05 02:03发布

问题:

I am trying to get libqglviewer to work in visual studio, I have installed Qt5.2.0 64-bit for VS 2012.

I have added the include directories for QGLViewer and QT in the project settings together with the lib-directory and the lib-files for qt (both Release and Debug).

I have a simple example code to test it:

main.cpp
#include "simpleViewer.h"
#include <qapplication.h>

int main(int argc, char** argv)
{
 // Read command lines arguments.
 QApplication application(argc,argv);

 // Instantiate the viewer.
 /* Viewer viewer;


 viewer.setWindowTitle("simpleViewer");


// Make the viewer window visible on screen.
 viewer.show();

// Run main loop.
 return application.exec();*/
}


simpleViewer.h
#include <QGLViewer/qglviewer.h>

class Viewer : public QGLViewer
{
protected :
virtual void draw();
virtual void init();
virtual QString helpString() const;
};

When I try to compile it I get

 main.obj : error LNK2001: unresolved external symbol "_declspec(dllimport) public: virtual class QString __cdecl QGLViewer::mouseBindingsString(void)const " (__imp_?mouseBindingsString@QGLViewer@@UEBA?AVQString@@XZ)

Plenty of them. I have looked around, but I cannot understand what the error is.

If I comment out everything except QApplication(argc,argv) it compiles, but if I uncomment Viewer viewer I get a lot of errors. I don't see what is missing since it compiles when I only use QApplication.

It feels like there is something missing with the linking, but I have added all .lib files I can find.

One thing is that if I download the libQGLViewer and run the following

qmake -t vclib QGLViewer.pro -spec win32-msvc2013

and then opens it in Visual Studio 2013 ( I changed today without success) it builds and generates the QGLViewer2.dll files as it should according to the instruction on libQGLViewers homepage. But if I try the simpleViewer example it gives the same problems again.

回答1:

Edit:

By re-installing everything qt and qt visual studio add-in again, for some reason the add-in searched for old versions of qt, when that was fixed I could open an example from libQGLViewer and immitate all includes and links I could finally get it to work.



回答2:

You're getting a linker error. Common causes are: Compiler can't find the .lib file for the dll. You built the program and the dll with a different compiler and the name mangling is making it impossible to find the function in the .lib file.

Try adding he QGLViewer project to your project in QCreator and then right click on yours and choose 'add libary' choose internal and reference QGLviewer, then thy will both compile together avoiding and name mnlging issues and QCreator should take care of the lib paths and all.



回答3:

Had the same error on QT v5.7. Solved it by adding

QT += widgets

in the .pro file at the top.