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

Evil Spork picture Evil Spork · Jun 13, 2010 · Viewed 14.7k times · Source

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?

Answer

jolyon picture jolyon · Jan 20, 2011

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