0

I'm trying to use SDL 2.0 in Qt Creator 3.1, using the Qt Build Suite (QBS) in Windows 7.

I've put the links in my .qbs to locate the SDL folders and I think everything works ok, because in my main.cpp it's recognizing the SDL functions, trying to autocomplete them.

The problem is, whenever I try to compile, I get the 'undefined reference to WinMain@16'

I'm using MinGW and set in the Compiler preferences the linker flags to: -lmingw32 -lSDL2main -lSDL2 -mwindows

The output is:

 c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/libmingw32.a(main.o): In function `main':
    e:\p\giaw\src\pkg\mingwrt-4.0.3-1-mingw32-src\bld/../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/main.c:91: undefined reference to `WinMain@16'
    collect2.exe: error: ld returned 1 exit status

And my .qbs is:

import qbs 1.0

CppApplication {

    name: "testeSDL2"

    cpp.includePaths: [".",
        "C:/SDL2-2.0.3/i686-w64-mingw32/include"]

    cpp.libraryPaths: [
        "C:/SDL2-2.0.3/i686-w64-mingw32/lib"]

    cpp.dynamicLibraries: "SDL2"

    files: [
        "main.cpp"
    ]

    qbsSearchPaths: "C:/CppLibs/"
}

I've been searching a lot and only find answers for common Qt projects using the .pro but not the .qbs... And there's a section in the SDL site that says it's a linker problem, but I set them in the preferences window, my guess is that I also need to set them somehow in the .qbs file, but I don't have a clue on how to do so. Can anyone help?

Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
BloodySinner
  • 95
  • 1
  • 9
  • Are you 100% sure you have a main function being linked? – chris Mar 19 '14 at 20:15
  • Yes, if I remove the #include from the main.cpp it compiles perfectly – BloodySinner Mar 19 '14 at 20:16
  • That's odd considering the main function would still be there both times. Does this help? http://stackoverflow.com/questions/17048072/sdl-2-undefined-reference-to-winmain16-and-several-sdl-functions Seems to need a `-lSDLmain`, whatever that translates to here. – chris Mar 19 '14 at 20:19
  • I saw that thread while searching for the error. I did put the linker flags in the mingw prefence window, just like that. I also tried Code::Blocks for this and apparently it works there, but not in Qt with QBS... That's why I'm thinking it's something related to these linker flags that need to be in the .qbs file too... – BloodySinner Mar 19 '14 at 20:24

1 Answers1

2

As I recall, SDL defines a macro main that expands to some SDL-specific function name.

Find that macro definition and you also find the macro symbol you need to disable that thing.

Oh well maybe this should have been a comment not an answer, but when/if you find that (sorry I don't have time to check those sources) then you can mark this as “the solution”, he he. :-)


Addendum: since the OP kindly posted the SDL code, I've looked at it and found this comment:

“If you provide your own WinMain(), you may define SDL_MAIN_HANDLED

… before including the SDL headers.

Then the macro called main is not defined.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    I didn't know that, and found similar problems here: http://stackoverflow.com/questions/11976084/why-sdl-defines-main-macro. I tried adding that #undef main in main.cpp and...well, it worked... But, as the guy said, I don't know if doing this will result in some negative consequences, haha. – BloodySinner Mar 19 '14 at 20:55
  • i think the best is to find the symbol you need to define to avoid having `main` macro defined – Cheers and hth. - Alf Mar 19 '14 at 21:56
  • Sorry, I'm still not sure about what you're meaning by "symbol". I uploaded the SDL_main here: http://pastebin.com/beaM84uG, can you tell me what I need to define? Aside from that, I tried this, too: http://stackoverflow.com/questions/396183/how-do-you-get-a-minimal-sdl-program-to-compile-and-link-in-visual-studio-2008-e. the part that uses the #ifdef _WIN32 #undef main #endif and it still works. – BloodySinner Mar 19 '14 at 23:17
  • Quoting from that code, "If you provide your own WinMain(), you may define SDL_MAIN_HANDLED". Which means just `#define` it before including SDL headers, or alternative define it via compiler option (usually `-D`). The latter may be safer, come to think of it, but for my own code I prefer to have everything relating to correctness in the code itself since that makes it more easy to switch build system – Cheers and hth. - Alf Mar 19 '14 at 23:29