对“WinMain”的未定义引用:使用 Cygwin、SDL2 和 Netbeans 时

undefined reference to `WinMain' : When using Cygwin, SDL2 and Netbeans

如有任何帮助,我们将不胜感激。 真是绞尽脑汁了,sooo.

我已经安装了 cygwin 和 Netbeans,并且已经成功地开发、编译和 运行 一个小型 SDL-1.2 windows 游戏,没有任何问题。

问题来了,我已经安装了SDL2,正在尝试编译。具体来说 linking.

我和以前一样,将 "libSDL2.a" 和 "libSDL2main.a" 添加到我在 Netbeans 中的项目的 linker 选项;但我收到 "undefined reference to `WinMain'" 错误

现在,我调查了这个,似乎 linker 不能 link 我的 main() 函数到 WinMain 函数。

我看到的另一个答案是将“-lmingw32 -lSDLmain -lSDL”添加到 linker 选项,但我不使用 mingw,我使用的是 cygwin,什么是 mingw32.lib 的 cygwin 等价物

我想主要问题是: 如果我使用 Cygwin、SDL2 和 Netbeans,我给 link 用户的选项是什么?

如有任何帮助,我们将不胜感激。

你用 #undef main infront of your main 测试过吗?

/*
 * If 'main' is defined we clear that definition
 * to get our default 'main' function back.
 */
#ifdef main
# undef main
#endif /* main */

int main(int argc, char** argv)
{
    // ...
    return 0;
}

Using Netbeans with Cygwin and SDL, including SDL.h creates strange error


也可能有帮助:

I get "Undefined reference to 'WinMain@16'"

Under Visual C++, you need to link with SDL2main.lib. Under the gcc build environments including Dev-C++, you need to link with the output of "sdl-config --libs", which is usually: -lmingw32 -lSDL2main -lSDL2 -mwindows

(http://wiki.libsdl.org/FAQWindows#I_get_.22Undefined_reference_to_.27WinMain.4016.27.22)

对于我刚刚使用 Cygwin 构建的库,我添加了以下编译器标志:

 -shared -Wl,--out-implib,lib$(LIB_NAME).dll.a

这让我能够在没有可怕的 "missing WinMain," 错误消息的情况下构建我的库。