【问题标题】:Using Netbeans with Cygwin and SDL, including SDL.h creates strange error将 Netbeans 与 Cygwin 和 SDL(包括 SDL.h)一起使用会产生奇怪的错误
【发布时间】:2014-07-16 03:21:09
【问题描述】:

我已经设置好 Netbeans C/C++,安装、配置并正确运行 Cygwin。 SDL 是使用 Cygwin 终端从源代码安装的。我已经确认 Cygwin、Netbeans 和 SDL 都运行正常,我可以使用 Netbeans 编写和编译 C++ 项目,而且 Netbeans 可以看到 SDL 而无需包含任何文件或任何东西,它就像默认库一样工作。

#include <cstdlib>
#include <sdl2/SDL.h>
using namespace std;
int main(int argc, char** argv) {

    return 0;
}

这是我要编译的代码,Netbeans 没有突出显示 sdl.h 的包含,但是当我去构建时,我得到了这个:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/Cally/Projects/Test'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/test.exe
make[2]: Entering directory '/home/Cally/Projects/Test'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f "build/Debug/Cygwin_4.x-Windows/main.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin_4.x-Windows/main.o.d" -o build/Debug/Cygwin_4.x-Windows/main.o main.cpp
mkdir -p dist/Debug/Cygwin_4.x-Windows
g++     -o dist/Debug/Cygwin_4.x-Windows/test build/Debug/Cygwin_4.x-Windows/main.o 
/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-1.7.30-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-1.7.30-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/Cygwin_4.x-Windows/test.exe' failed
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/test.exe] Error 1
make[2]: Leaving directory '/home/Cally/Projects/Test'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/Cally/Projects/Test'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 1s)

不包含 SDL 时构建成功。有人知道我做错了什么吗?

【问题讨论】:

    标签: c++ windows netbeans cygwin sdl


    【解决方案1】:

    错误信息看起来很邪恶——但它只是告诉你它找不到WinMain

    这是 SDL 的一个已知问题。请将这些库添加到您的链接器(订单是强制性的!):

    1. mingw32
    2. SDLmain
    3. SDL

    您可以将-lmingw32 -lSDLmain -lSDL 添加到链接器选项中,也可以通过链接器配置的库菜单添加它们。

    您可能也需要SDL_mixer - 如果需要,请最后添加。

    另请参阅:http://content.gpwiki.org/index.php/SDL%3aTutorials%3aSetup


    作为“肮脏的解决方法”,您可以这样做:取消定义main

    SDL 将main() 重新定义为带有一些附加内容的宏。您可以通过 egl 验证这一点。 Ctrl + 单击 main / 转到声明/定义 或检查它是否被格式化为 makro。

    #include <cstdlib>
    #include <sdl2/SDL.h>
    using namespace std;
    
    /*
     * 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;
    }
    

    请参阅SDL_main.h 的来源(第 103+ 行)。

    【讨论】:

    • 这些是 .a 文件,对吧?如果是这样,我找不到他们中的任何一个。
    • 不一定,也有可能是dll/so。也许他们的名字以lib 开头 - 链接器会自动添加。
    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 2021-12-14
    • 2010-09-14
    • 2016-03-15
    相关资源
    最近更新 更多