5

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?

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
Evil Spork
  • 1,013
  • 2
  • 10
  • 16
  • try the steps here: http://stackoverflow.com/questions/5601950/how-to-build-qt-for-visual-studio-2010/5791452#5791452 for me it worked like a charm. – Odin Jul 26 '11 at 12:06

5 Answers5

6

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
  • When I get back in to programming and Qt I will certainly give this a try. Thanks for the clear cut directions. – Evil Spork Jan 30 '11 at 08:28
  • year and a half later... I finally get around to programming in Qt again, and yes this worked. Perhaps a bit clunky and cumbersome.. and I assume I have to redo a step every so often to update the moc files? – Evil Spork Jul 02 '12 at 17:03
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.

Ben H
  • 3,136
  • 3
  • 25
  • 34
  • aye, I built the project using qmake, and it added the libs (which i had to modify a little) I'll update with a list of included libs and paths. – Evil Spork Jun 13 '10 at 10:47
2

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.

Maurycy
  • 3,911
  • 8
  • 36
  • 44
0

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?

cnebrera
  • 695
  • 3
  • 5
  • its express, cant install a plugin.. and the code is more than just hello world.. its currently a commandline program.. that I'm adding a basic qt empty window up with the generic auto created classes. QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); – Evil Spork Jun 13 '10 at 10:59
  • If you cannot install the plugin in the express edition I'm not sure I can help. You need to be able to create the moc files, I'm sure you can do that manually from the command line and then try to change the visual studio parameters to do the compilation. However I doubt the debugger is going to work very well in that case since it will probably try to debug the moc code with the signals and other Qt macros in pure C++ code. Why don't you try QtCreator or Eclipse? Do you need specific visual studio functionality? – cnebrera Jun 13 '10 at 20:58
  • it compiles fine in QtCreator, thats not the issue. the issue is, I /need/ to use visual studio 2008 or 2010.. and i only have express. – Evil Spork Jun 13 '10 at 21:37
  • That make things very complex. I have no idea if is possible to compile in visual studio without the pug-in and even if you can, as I said before probably the debugger is going to be very hard to use. I know is a pain in the ass but I would get the standard edition if it is really compulsory for you to use visual studio. I'm sorry not being able to help you further. – cnebrera Jun 14 '10 at 08:18
  • okay, i got the standard version.. painful to have to shell out for this.. but even more painful that i get the same error. so how do I solve it now? – Evil Spork Jun 14 '10 at 11:35
  • Do you have the visual studio qt plugin installed and the qt bin directory in the path and have created the visual studio project from your pro file calling qmake -t vcapp? – cnebrera Jun 14 '10 at 12:54
  • yes I created it by calling "qmake.exe -tp vc -spec win32-msvc2008", i installed the qt plugin, and its all pathed up. – Evil Spork Jun 15 '10 at 21:13
  • I have no idea of where is the problem I'm sorry :(. I'm using visual studio 2008 with the plug-in and I have no problems at all. – cnebrera Jun 16 '10 at 08:21
0

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
Dwedit
  • 618
  • 5
  • 11