tags:

views:

645

answers:

5

Hi!

Can anyone pls tell me that, why I can't use normal C++ classes within a QT programme. If there is any class which aren't inherited from QObject the compiler give me a linking error called,

error LNK2019: unresolved external symbol _main referenced in function _WinMain@16

I'm using Qt4.5.2 (compiled by myself) with vs2005. Pls help me to solve this !

Edit:

Example...

//UnitManager.h

class UnitManager
{
public:
//-Some code
};

//CivilizationViewer.h

class CivilizationViewer : public QMainWindow
{
Q_OBJECT
//-some code
};

//main

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CivilizationViewer w;
    w.show();
    return a.exec();
}

If I include UnitManager.h in CivilizationViewer.h compiler will give me that error. (eventhough I include UnitManager.h in main.cpp compiler will give me the error)

+6  A: 

The error you gave doesn't have anything to do with what classes you're using. It looks like it's related to the entry point you have set for your application. Usually you want to use main() instead of WinMain() in Qt programs. Make sure your configuration is set up right.

You included a little bit of code in your question. Is that everything? If so, you're missing a main function.

Evan Shaw
Hey!Thanks for the answer. Everything was going perfect untill I added that header file (which contain a normal C++ class). That C++ class was working without Qt also. And also it is a default project opened by vs2005 qt intergration.
Morpheus
Sorry for the short code, I added the main also.
Morpheus
So just removing the line #include "UnitManager.h" will cause the build to succeed? That seems really odd. I can offer a couple suggestions. First, make sure that the end of your UnitManager class has a semi-colon. (It doesn't in your question.) Second, make sure your project is configured as a console program and not a Windows program. If it's a Windows program Visual Studio will look for WinMain instead of main.
Evan Shaw
Hey!Again sorry for the mistack in the class. The whole code (except the Qt code) was perfectly run before adding to Qt. hmm.. about youe second suggestion, where can I see that cofiguration ? I didn't see such a thing in properties. Can you pls guide me ?
Morpheus
You can find that in project properties -> linker -> system -> subsystem
Emile Vrijdags
@ejac thank you very much.
Morpheus
@Chickencha yeah..it was the real error :) no need to change the order of the SDlmain.lib and qtmaind.lib. By changing the Windows to Console solved the probem. Thank you very much.
Morpheus
A: 

It seems like you need to link with qtmain.lib (qtmaind.lib for debug builds). That library provides a WinMain function which is needed if you declared /subsystem:windows.

Source: http://lists.trolltech.com/qt-interest/2005-12/thread00170-0.html

Hey!Thanks for the ans. qtmaind.lib is already added. This is really odd. Why I can't add a header file to main.cpp ??
Morpheus
A: 

It looks like you're not including the QT libraries properly... (the actual .lib file)

cyberconte
A: 

I think you actually made a win32 app. try replacing your main for:

int _tmain(int argc, _TCHAR* argv[]){
  your code
}

Seeing your error msg, i wouldn't guess Qt was your problem. Did you install the Visual Studio Qt integration? Try it and make a new Qt project.

David Menard
Hey! can you pls give me your comment about following answer ?
Morpheus
+1  A: 

Hey! Thanks for everyone. I found the error. There is SDL.h in the UnitManager.h, so I have to add SDL.lib and SDLmain.lib (it is correct, right ?) then there is another definition for main in SDLmain.lib. So, there was a coflict between definitions of main. Therefore I added SDLmain.lib before adding qtmaind.lib. Then the problem solved by only giving a warning called,

warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library

What is that warning ? I can just ignore the warning, but I like to get to know it ! Thanks

Morpheus
The real error was solved by the Chickencha.
Morpheus