SDL 2 Undefined Reference to WinMain@16 and several SDL functions
post the compiler commands.
ex: g++/gcc ....
you are probably not linking the library.
http://content.gpwiki.org/index.php/SDL:Tutorials:Setup
you should have the path to the lib, included in the ide.
(I see you are using codeblocks)
add to the linker settings:
-lmingw32 -lSDLmain -lSDL
http://www.sdltutorials.com/sdl-tutorial-basics
undefined reference to WinMain@16 C++, SDL-2
I think you want
#define SDL_MAIN_HANDLED
in your main file, BEFORE the line
#include <SDL2/SDL.h>
Explanation:
In SDL / SDL2, in an effort to try to make cross-platform development of certain kinds of applications simpler, SDL creates a custom "entry-point" to your application. That is, your int main()
is not the real main. What happens is, main
is defined as a macro in the SDL header and this causes your main to be renamed to SDL_main
or similar. Then, in the "SDL_main" library a different main
is defined which will be the real main
of your application. This main
just fetches the command-line arguments in whatever way is appropriate for the platform, and calls your main
(which was renamed SDL_main
).
On windows, there are also some differences regarding whether your application is started as a console program vs. as a gui program, iiuc.
Sometimes you want SDL to do these things for you, but if you are developing a traditional console program, usually you don't. So you pass SDL this SDL_MAIN_HANDLED
define in order to prevent from doing all this stuff.
The #undef main
approach will work also, but it's not quite as good because this way, you tell SDL what is going on, with the other method, SDL thinks that all it's stuff is going to be used and in fact you crudely disable it with #undef
later.
If you want to see details of the various macros / platform checks, you can look in the SDL_main.h
header. If you want to know what the benefits of the SDL main system are, you can look at SDL documentation.
Can't install SDL on MinGW, getting `undefined reference to WinMain@16`
SDL2-devel-2.0.0-mingw.tar.gz
contains both 32-bit libraries (i686-w64-mingw32
directory) and 64-bit libraries (x86_64-w64-mingw32
directory).
The error was caused by using a 64-bit version of the library with a 32-bit compiler.
SDL2 Cmake undefined reference to `WinMain@16' on Win10 MinGW
The solution was to add a new line to the end: add_definitions(-DSDL_MAIN_HANDLED)
.
Now the CMakeLists.txt code looks like this:
cmake_minimum_required(VERSION 3.15)
project(SDL_project)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(SDL_project main.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY} -lmingw32 -mwindows)
add_definitions(-DSDL_MAIN_HANDLED)
And in the project folder in a separete folder called cmake
there is the FindSDL2.cmake
file, to locate SDL2:
set(FIND_SDL2_PATHS D:/SDL2/SDL2-devel-2.0.12-mingw/SDL2-2.0.12/x86_64-w64-mingw32)
find_path(SDL2_INCLUDE_DIR SDL2
PATH_SUFFIXES include
PATHS ${FIND_SDL2_PATHS})
find_library(SDL2_LIBRARY
NAMES SDL2 SDL2main
PATH_SUFFIXES lib
PATHS ${FIND_SDL2_PATHS})
From the CMakeLists.txt I removed -lSDL2 and -lSDL2main flags, because they resulted in errors:
====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
[ 50%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2
A:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
Makefile:117: recipe for target 'SDL_project' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
Also, it is important to use 64-bit mingw-w64, because mingw-w32 also gives errors, like:
====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
[ 50%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\SDL_project.dir/objects.a(main.cpp.obj):D:/SDL_project/main.cpp:17: undefined reference to `SDL_Init'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\SDL_project.dir/objects.a(main.cpp.obj): in function `main':
D:/SDL_project/main.cpp:19: undefined reference to `SDL_GetError'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:24: undefined reference to `SDL_CreateWindow'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:27: undefined reference to `SDL_GetError'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:32: undefined reference to `SDL_GetWindowSurface'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:35: undefined reference to `SDL_MapRGB'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:35: undefined reference to `SDL_FillRect'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:38: undefined reference to `SDL_UpdateWindowSurface'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:41: undefined reference to `SDL_Delay'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:45: undefined reference to `SDL_DestroyWindow'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:48: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
Makefile:117: recipe for target 'SDL_project' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
And, the SDL.dll file from the binary folder has to be added to the compiler, to avoid exit code -1073741515 (0xC0000135)
. It can be done by setting a path to the folder, or by copying the SDL.dll from the SDL\bin folder to the mingw64\bin folder.
GCC C++ linking error : Undefined reference to 'WinMain@16'
Rename your main
to be WinMain
with the following parameters
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR lpCmdLine, INT nCmdShow)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Quit();
return 0;
}
Most of those parameters will require #include <windows.h>
If that header is not available, then declare as:
int __stdcall WinMain(void*, void*, char*, int)
undefined reference to `WinMain' : When using Cygwin, SDL2 and Netbeans
Have you tested with an #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
May also help:
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 )
Related Topics
Typecasting Eigen::Vectorxd to Std::Vector
Assignment Operator with Reference Members
Instantiate Class with or Without Parentheses
Determining If an Unordered Vector<T> Has All Unique Elements
Shift Image Content with Opencv
Is Using Increment (Operator++) on Floats Bad Style
Std::Map of Member Function Pointers
Is There a Reason Declval Returns Add_Rvalue_Reference Instead of Add_Lvalue_Reference
Partial Ordering with Function Template Having Undeduced Context
Dropping Privileges in C++ on Windows
Converting Cv::Mat for Tesseract
Multiplying a String by an Int in C++
How to Determine the Correct Size of a Qtablewidget
How to Pass an Std::String to Glshadersource
Get Local Ip-Address Using Boost.Asio
Avoid Warning 'Unreferenced Formal Parameter'
C++ Performance of Accessing Member Variables Versus Local Variables