3

I'm using SDL in a simple program with QT Creator under windows operating system. But When I build the code, I encounter the following linkage error.

MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

I got through the code and found that the main() is not recognized in my .cpp file, since the same keyword has been referenced in SDL_main.h, as shown below:

#define main SDL_main

/** The prototype for the application's main() function */
extern C_LINKAGE int SDL_main(int argc, char *argv[]);

When I click on the main function in my .cpp file, I jump to the definition in SDL_main.h which verifies that there is a kinda conflict.

So, I guess the linker does not recognize the main() in my .cpp file. Here is my code:

#include <QtCore>
#include <opencv2/opencv.hpp>
#include "vlcvideocapture.h"



int main()
{
    clsVlcVideoCapture videoCapture("ip.camera");
    int key = 0;
    while(key != 27) {
        cv::Mat frame;
        int frameNumber = videoCapture.grabFrame(frame);
        if(frameNumber == -1)
            continue;
        cv::imshow("frame", frame);
        key = cv::waitKey(30);
    }
    return 0;
}

What's going on here and How I can fix this issue in a justifiable manner. Any help would be appreciated.

Update:

Some guys say to use #undef main but when I do so, the following error pops up which is expected:

cannot initialize SDL
Saeed
  • 742
  • 1
  • 7
  • 21
  • Also check out (http://stackoverflow.com/questions/118659/how-do-i-use-qt-and-sdl-together?rq=1). But it doesn't address the `main` issue. As I recall the part of the SDL header file that you quote, contains information about how to disable SDLs `main`, and the SDL `main` issue is discussed in numerous SO questions about it. But while I was able to find several such questions, I was unable to find the simple answer that is spelled out in the SDL code. Mostly the answers are about conforming by doing Unholy Gymnastics, instead of just taking charge. – Cheers and hth. - Alf Oct 13 '15 at 08:48
  • 1
    Why would you ever want SDL and Qt at the same time? O_O – Ivan Aksamentov - Drop Oct 13 '15 at 08:51
  • @Drop: No that's not a duplicate, although its (IMHO very undesirable) answer of using `int main(int argc, char* argv[])` would work technicallty. First it's not about QT, and second it's not about how to disable the SDL `main` thing, it's about *why* the mechanism is there. And the answers are all wrong about the *why*: the only reasonable answer is sheer incompetence. Which is important to know. – Cheers and hth. - Alf Oct 13 '15 at 08:59
  • don't include sdl_main.lib file in linker tab – Aladdin Oct 13 '15 at 10:56
  • @AboAnas That's not the way to solve it. It has not been added. – Saeed Oct 13 '15 at 11:36
  • @Cheersandhth.-Alf Any idea how to solve this issue? – Saeed Oct 13 '15 at 11:36
  • @Saeed the only time I faced this problem was when I included sdl_main.lib in linker tab. – Aladdin Oct 13 '15 at 11:55

1 Answers1

3

The SDL Wiki provides essentially this example:

#define SDL_MAIN_HANDLED
#include "SDL.h"

int main()
{
    SDL_SetMainReady();
    SDL_Init(SDL_INIT_VIDEO);

    ...

    SDL_Quit();
}

Defining SDL_MAIN_HANDLED avoids having main defined as a macro.

Calling SDL_SetMainReady() is necessary to emulate the effect of SDL's main (the function is there for this specific purpose).

To then avoid SDL defining a main function, it appears that all you need to do is to not link with "SDL2main" library.

Quoting a posting somewhere by someone, about this:

SDL2main and SDL2test are two auxillary libraries for SDL2.

SDL2main provides SDL_main(), which is a global entry point for all SDL apps. You are not required/forced to use it, but is presence is based on the variety of the systems SDL supports. Windows uses WinMain(), Linux uses main(), Android needs JNI and some Java to actually use SDL, so SDL_main() makes things a little easier.

SDL2test on the other hand, provides some functions not directly related to the purpose of SDL. Some are useful for ordinary programs, others are directly related to the test programs, it's your choice to use them or ignore them.

Neither SDL2main nor SDL2test are required if you want to use SDL2, they are just some good choices.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331