Compiling a simple Qt “Hello World!” application within Visual Studio 2010 Express?

空扰寡人 提交于 2019-11-30 05:32:52

问题


I'm trying to build a basic Qt "Hello, world!" application inside Visual Studio.

I got the moc step to work (I think), but now I am at a loss as to how to fix this linker error:

1>moc_mainwindow.obj : error LNK2001: unresolved external symbol "public:
static struct QMetaObject const QMainWindow::staticMetaObject"
(?staticMetaObject@QMainWindow@@2UQMetaObject@@B)

I've done a lot of searching but I am at a loss.

Here are my include directories:

  • i:\Qt\4.6.3\include\QtCore;
  • i:\Qt\4.6.3\include\QtGui;
  • i:\Qt\4.6.3\include;
  • i:\Qt\4.6.3\include\ActiveQt;
  • reease;
  • .;
  • i:\Qt\4.6.3\mkspecs\win32-msvc2008

Here are the libraries I am linking against:

  • i:\Qt\4.6.3\lib\QtGui4.lib;
  • i:\Qt\4.6.3\lib\QtCore4.lib;
  • gdi32.lib;
  • comdlg32.lib;
  • oleaut32.lib;
  • imm32.lib;
  • winmm.lib;
  • winspool.lib;
  • ws2_32.lib;
  • ole32.lib;
  • user32.lib;
  • advapi32.lib;
  • libpng.lib;
  • msimg32.lib;
  • shell32.lib;
  • kernel32.lib;
  • uuid.lib;

Does anyone have any ideas?


回答1:


qmake will generate the moc voodoo from the header file in .pro file. As you aren't using qmake, by the sound of it, but a native visual studio project, this is probably the cause of the problem.

If you use qmake to generate your visual studio project all your problems will go away and life will be sweet. Probably!

I am using the open 2010.05; obviously you want to substitute the correct path for your version.

set up the environment

start 2010 command environment from the start menu
-set include=%include%;C:\Qt\2010.05\qt\include
-set lib=%lib%;C:\Qt\2010.05\qt\lib -set path=%path%;C:\Qt\2010.05\qt\bin
-set QMAKESPEC=win32-msvc2010

write code, create files etc

generate the initial pro and makefile and fire up VS
-qmake -tp vc
-qmake

you should now have a makefile - check that it works by running:
-nmake

now launch visual studio
-VCExpress.exe /useenv
-XXX.vcxproj can now be opened

If this doesn't work you may need to build qt at against visual studio. This is very straightforward - go to the qt directory (from within the visual studio express command window) and type:

configure.exe -platform win32-msvc2010 -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-multimedia -no-qt3support -fast



回答2:


You cannot install the Qt VS plugin on the Express edition of VC++. Assuming you got the moc to compile, you also need to make sure you're including the appropriate libraries (*.lib files) at link time. This goes under Project properties > Linker > Input > Additional Dependencies.

You will need qtcore4.lib at a minimum.

Also make sure the Qt library path is in your library search path. On my computer it's c:\qt\4.6.2\lib.




回答3:


I was able to get QT to work with Visual C++ Express 2010 using http://rajorshi.net/blog/2009/01/using-qt-with-msvc-express-2008/ and http://portfolio.delinkx.com/files/Qt.pdf as guides. Just in case anyone still is having problems.




回答4:


Have you create the visual studio project using qmake first? The problem seems to be the moc compilation. Do you have qt plug-in installed and the qt path in enviromental variables? Can you add you hello world code so I can have a look at it?




回答5:


You need to add commands to generate QT metaclasses, then also include the generated files in your project as c++ code.

Generating the QT metaclasses:

  • First, add your QT bin path into the Executable Directory. (This is in Configuration Properties > VC++ Directories)

  • Add your Header files that contain Q_OBJECT macros to the project.

  • Multi-select your header files, then right click on a header file, click Properties.

    • Change "Item Type" from "C/C++ Header" to "Custom Build Tool".
    • Set Command line to this: moc.exe "%(FullPath)" > "$(ProjectDir)MetaObjects\moc_%(Filename).cpp"
    • Set Description to this: QT: Generate $(ProjectDir)MetaObjects\moc_%(Filename).cpp (optional)
    • Set Outputs to this: $(ProjectDir)MetaObjects\moc_%(Filename).cpp
  • Run Build just to make it generate the metaobject code
  • Add the generated C++ files from the Project Directory Metaobjects folder into your project


来源:https://stackoverflow.com/questions/3031512/compiling-a-simple-qt-hello-world-application-within-visual-studio-2010-expre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!