Search code examples
c++sdl

When linking SDL, getting `unresolved external symbol SDL_main`


I followed SDL2 installation guide but still i could't make to work

Its shows LNK2019 and LNK1120 error

I downloaded SDL2-devel-2.30.0-VC.zip from the download site and extracted it in a separate folder.

then i created a new project in visual studio community.

then Debug>Properties and added library, include and linker too. and also verified i am using the right architect (x64)

1>------ Build started: Project: Project1, Configuration: Debug x64 ------
1>work.cpp
1>SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol SDL_main referenced in function main_getcmdline
1>D:\RayTracing\Project1\x64\Debug\Project1.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 13:19 and took 02.133 seconds ==========

i have added the library path to the environmental variable too.

but still i am getting the error

guide me to fix this error


Solution

  • From https://wiki.libsdl.org/SDL2/FAQWindows#i_get_undefined_reference_to_sdl_main_:

    I get "Undefined reference to 'SDL_main'" ...

    Make sure that you are declaring main() as:

    #include "SDL.h"
    
    int main(int argc, char *argv[])
    

    You should be using main() instead of WinMain() even though you are creating a Windows application, because SDL provides a version of !WinMain() which performs some SDL initialization before calling your main code. If for some reason you need to use !WinMain(), take a look at the SDL source code in src/main/win32/SDL_main.c to see what kind of initialization you need to do in your !WinMain() function so that SDL works properly.

    Now i could understand why it didn't work