c++ - C++ 程序和 SDL 中的 main() 冲突

标签 c++ qt sdl

<分区>

我在 Windows 操作系统下使用 QT Creator 的简单程序中使用 SDL。但是当我构建代码时,遇到以下链接错误。

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

我通过代码发现 main() 在我的 .cpp 文件中无法识别,因为在 SDL_main 中引用了相同的关键字.h,如下图:

#define main SDL_main

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

当我单击 .cpp 文件中的主函数时,我跳转到 SDL_main.h 中的定义,它验证是否存在某种冲突。

所以,我猜链接器无法识别我的 .cpp 文件中的 main()。 这是我的代码:

#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;
}

这里发生了什么以及我如何以合理的方式解决这个问题。 任何帮助将不胜感激。

更新:

有些人说使用 #undef main 但是当我这样做时,会弹出以下错误,这是预期的:

cannot initialize SDL

最佳答案

SDL Wiki 基本上提供了 this example :

#define SDL_MAIN_HANDLED
#include "SDL.h"

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

    ...

    SDL_Quit();
}

定义 SDL_MAIN_HANDLED 避免将 main 定义为宏。

调用 SDL_SetMainReady() 是模拟 SDL 的 main 效果所必需的(该函数用于此特定目的)。

为了避免 SDL 定义 main 函数,看来您需要做的就是不链接“SDL2main”库。

引用 a posting somewhere by someone ,关于这个:

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.

关于c++ - C++ 程序和 SDL 中的 main() 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33097988/

相关文章:

linux - 如果我仅使用 OpenGL ES 1.x 调用构建并链接 OpenGL 应用程序,它仍然可以工作吗?

console - 使用多个文件制作 File 和 SDL

c++ - Qt - Unresolved 重载函数类型

c++ - Qt5 QtQuick 2.0(Qt Quick Application) 在一个窗口中切换 View (qml文件)

qt - Qt 支持 RSA 加密吗?

qt - 在 QML 中将 TabButton 动态添加到 TabBar

c++ - SDL2/SDL 图像奇怪的 PNG 行为与 RGB 值

C++ getline(cin, str) 给我额外的行

c++ - 在 ICU 中使用文字字符串

c++ - 检查拼写数字是否在 C++ 的范围内