#undefined reference to WinMain
242 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
The tutorial I originally followed in installing SDL2 did use C++, but it also said that it was applicable to C.
I recommend just using gcc main.c -o main && ./main in your terminal, cause it achieves the same thing and is more readable
Can you send the tutorial?
https://www.youtube.com/watch?v=H08t6gD1Y1E
I just looked through it again, and I guess it didn't actually ever actually explicitly say it worked with C. Also anything after him making the Makefile seemed to be strictly for C++, so I didn't follow that part.
The best way to contact me is with Discord: https://discord.gg/eqz5jHSUy4
We set up SDL2 for MinGW on Windows. That is pretty much it.
sdl website: https://www.libsdl.org/
the sample code: https://github.com/HerbGlitch/SDL2-Setup
I tried that, and it just said that && wasn't a valid argument separator. I removed the && ./main bit, and it gave the same error as in the screenshot.
You're using PowerShell I think, since the first line is PS, which might cause general weirdness. If you press this dropdown button you should see the option to open bash or cmd, and then try using gcc main.c -o main && ./main again
The && here basically means "if gcc succeeded, run the program"
Same code; very similar error with Git Bash.
you need to change directory (cd) into your project first
So specifically cd "GUI Game"
ok yeah exact same error as last time
Yeah OK, so what you just need to do is watch the full video. It shows how to compile in such a way that SDL is used as a library.
I did watch the full thing multiple times, and I honestly just don't understand anything after the Makefile. The Makefile command looks to just be for C++ as it starts with g++, and I don't know how much I'd need to change it to be for C. If I just edit the g++ part to gcc, and replace the .cpp with .c, it still doesn't work.
It gives very similar errors, but instead about being unable to find most of the different arguments. It also says "skipping incompatible" a bunch.
Well, you'll want to send that error instead then
I recommend forgetting about wanting to write C for a moment, and just following the tutorial exactly first. You can then always come back and change the .cpp to .c later on
Again, you are trying to compile your main.c using gcc main.c, but the tutorial shows you that you need a longer command that tells the compiler where to find the SDL2 library
So start by recreating the Makefile, and following every step exactly to the best of your ability, and then come back with how that went
I used gcc -Isrc/Include -Lsrc/lib -o main.c -lminw32main -lSDL2main -lSDL2
Well, the screenshots you sent didn't show what that printed
Well if I follow it exactly, I get this.
Pasting the command exactly makes it say that main.cpp doesn't exist, which would make sense given that it's not a C++ file, and trying to run it via VSC gives the error saying <SDL2/SDL.h> doesn't exist. Replacing that with the actual location of the imported file leads to where I was at the start of the post.
I'm going to recheck if I even have mingw installed. I'm quite confident I do, but I'll check.
Make sure to use cmd like you did in the earlier screenshot
This is again PowerShell
not too different.
Yeah, I have mingw
I'll boot up Windows myself for a minute to check how I did it with mingw
So in my case I can do C:/msys64/mingw64/bin/mingw32-make.exe
You may have put it in a different folder, but try it
I don't seem to have that file
My C:/msys64/mingw64/bin/ directory doesn't have a mingw32-make.exe file.
Try to look for a file with make in it in there
I just installed make from the video's decription
Ah ok
I do have C:/program Files(x86)/GnuWin32/bin/make.exe, which was just downloaded.
This stuff is all over the place, wow.
Yeah, I highly recommend switching to Ubuntu asap
I do not know what you mean by that.
Writing C and C++ on Windows is a pain in the ass
Linux distributions like Ubuntu make it significantly easier
So far it hasn't been so bad if you ignore this library stuff.
Library stuff is actually why this isn't my first attempt. I keep trying for hours and giving up. This has to be like #7 in the last year.
You can ignore the Makefile for now, just using the command that he puts inside of it, that should work fine
I am ok with that
Just put the command in a README.md in your project so it's easy to find
Or a simple run.sh
It is basically the same as a Makefile here, yeah
A Makefile is intelligent in the sense that it easily allows you to only recompile the program whenever its code changes, rather than doing it every single time you run it, but it should compile super fast anyways
This isn't too complicated of a program.
I still have that issue from the very beginning. WinMain has an undeclared reference.
Show a screenshot of your files in VS Code, where you expanded all of the subfolders
And a screenshot of that undeclared reference error again
Not opening construction because it's irrelevant, and src/include has a ton of header files that would cover up the screenshot. The Bash screenshot is when the command is pasted directly into the terminal, and the PS screenshot is when the debug run button is clicked.
You didn't screenshot your main.c in the filetree, but rename that to main.cpp for now
The command here also looks different from the one you used, so use this exact one
the PS screenshot is when the debug run button is clicked.
Don't use that for now
What's a main.c filetree?
You didn't screenshot your main.c in the filetree
Soyour main.cis just saying "your main.c file"
File tree is this
Or file explorer or whatever other name you like
But when people say file explorer they usually mean this thing on Windows
That's what I thought it was. I did send that screenshot.
So rename main.c to main.cpp, and run this exact command
I have to go do something. brb.
(I'm still doing that other thing, so I'm oing to be replying quite slow)
for the undefined reference to winmain, do
int main(int argc, char** argv){}
if it still pops up, add -DSDL_MAIN_HANDLED to your compiler args
or #define it in the same file you have main
Pretty sure they already have a main function, since they sent this screenshot
But that -DSDL_MAIN_HANDLED might very well be it
yeah my bad i dont really check screenshots
they are a pain to read without vencord
yeah SDL uses a macro for main that calls stuff from their API first and then calls WinMain on windows
You're getting cannot find -lmingw32main: No such file or directory as the first error, so can you show a screenshot of where you think it should be found in your file tree?
Cause the YTer did -lmingw32 there
they probably dont need to link mingw32main anyways
you only need to link against SDL2 and SDL2main (if you link statically)
and I think order matters and SDL2 should be before SDL2main
but using sdl2 is kinda obsolete now no 
I'd personally use something like raylib
It's what I was told to use by a lot of people. Is there a better GUI library for C?
SDL isn't a GUI library
it's a windowing/event API
but it's fine
That's what I meant.
it's just that there is SDL3 if you are willing to build it
Building SDL2 is already being a pain. How much worse is SDL3? What are its advantages?
I'd just stick with SDL2 for now, rather than switching
i mean sure
Doesn't really matter what a beginner uses, but it's just a shame that it always takes so much effort to set this stuff up
idk
git clone
open VS solution
press ctrl+b
done
it shouldn't be
but that's the curse of not using a build system
And Windows
I was just about to say, I really just want to work in VS Code and not VS
And every time it's annoying on Windows
VSC is just as bloated and bad in performance as VS was for me
i use neither anyways
Fair, but I mostly mean in terms of feel and looks
but the amount of boilerplate c++ requires, I can't live without an IDE
There was a reason I chose VS Code and not VS because I remember originally downloading VS not knowing the difference, trying to do something, then finding out that I had to use VS Code for it. I don't remember what I was trying to do or why I couldn't on VS.
I recommend just using what you have right now, cause you're almost at the finish line
I'm at the same place I was two hours ago.
You're getting cannot find -lmingw32main: No such file or directory as the first error, so can you show a screenshot of where you think it should be found in your file tree?
Cause the YTer did -lmingw32 there
Well yeah, but you're just missing something small now
you should try just not adding that anyways
Huh, weird. I don't know why that extra main is there.
and then if there is a problem you add it ¯_(ツ)_/¯
What nevemlaci said, try removing -lmingw32main entirely for a moment
Same issue
At least you don't have that error anymore at the top
hmm
Well yeah but that's because it's near the bottom
That's what the guy in the tutorial did, so I think so.
I am saying the error that used to show up as the first one at the top about mingw32main is now gone
try the 64 bit one
Ok, I'll do that.
I am wondering why this happens
So do I just delete lib, include, and SDL2.dll then readd?
Right, yeah then probably 32 vs 64 bit issue
use critical thinking sometimes
tutorials are usually shit
yes
everything related to sdl
win32-x64, right?
i just downloaded the lib for mingw from the releases on github
this
and itll be in this one
That's the one I used previously
Ah.
but you really should be using a build system like CMake
add_library(SDL3 SHARED IMPORTED ${LIBRARY_DIR}/mingw/SDL3.dll)
set_target_properties(SDL3 PROPERTIES
IMPORTED_LOCATION "${LIBRARY_DIR}/mingw/SDL3.dll"
IMPORTED_IMPLIB "${LIBRARY_DIR}/mingw/SDL3.dll"
INTERFACE_INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/include"
)
target_link_libraries(my_project PRIVATE SDL3)
this is how I linked SDL3 in cmake(dynamically)
Yup, none of that makes any sense to me.
you dont need the IMPLIB property for statics
add a library
set its properties(where the library file is, where its includes are)
link it to the build target(named my_project in this case)
But before you jump down this rabbit hole, just try to use this x86_64 version of SDL2
It's almost certainly the exact issue
saw this problem a few dozen times
someone follows a 10 year old tutorial where the guy builds on 32 bits
doesnt tell the viewer
Ok I'm back for a bit. I did install the 64 bit version and seems to be working, but the resulting exe file doesn't seem to do anything, even after inserting the line printf("test");
Try g++ main.cpp -o main and then ./main with this:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
}
To make sure it's not SDL being fucky
It's not SDL.
So did you try the main.cpp with the command I gave that you say that?
I did gcc main.c -o main
What does the main.c look like
Okay cool, yeah then it's not SDL
Though try the same thing with g++ instead of gcc
Same thing
wdym "not doing anything"
does it return 0?
or crash?
did you put SDL2.dll in the same folder as the executable?
Is it the 64bit one?
In order to check what happened you can run echo $?, which prints 0 if your program exited successfully
It creates the executable file, but running it seemingly does nothing.
probably, but I can't tell as nothing pops up.
No crash message
Yes
Yes
Try echo $?
Cause I have had the exact same issue before, and it was due to having several versions of compilers on my computer
Try echo %ERRORLEVEL% otherwise
Yeah it looks to be returning 0
wait no
I misread
It says bash: echo: command not found despite sill saying wefg when I type echo "wefg"
You'll probably need this on Windows
so what does this do?
Prints what your program returned
You wrote return 0;, so it should print 0
Oh, gcc main.c -o main && ./main did print Test like it should
So running the file via GUI didn't work, but through command line it did. I expected this button to run the code.
We both have absolutely no clue what that button actually does under the hood, so please stop using it
That is funny. I really thought it would just execute the executable file, but I guess not.
fair enough, but at least hopefully you can see where my misunderstanding sprang from. I'll try SDL now.
yeah no worries, easy beginner mistake to make. The main lesson to take away from it is that if someone is telling you to execute a command, you do that exact thing :P
What's some very simple SDL function I can run to test things?
probably some sort of init call that you can glean from his video
or just copy his code if he published it
woo
(Ignore the return value. That was a random number to test things.)
Finally. It took four hours, but I finally got it loaded. This is the reason I gave up all those other times. I can rest. Finally.
Haha, great job!
Now to draw something on the screen
Just copy this main.cpp and see if it works:
https://github.com/HerbGlitch/SDL2-Setup/blob/master/main.cpp
btw, C and C++ have the rule that main() implicitly returns 0, so you can just get rid of that weird return 65678; at the end of your main()
Yeah, I know.
It was for the echo bit
Any non-zero return code is what is causing the red dot here, indicating failure btw
That is helpful to know, thanks.
So no need for echo $? since you have this dot indicating whether it's 0 or not
Also the show window part worked.
ayy
This was such a pain.
yeah, from this point on you're free from having to do any setting up
just coding now
So, in conclusion: To get it working, download SDL2, and from the 64 bit version indicated by the text x64, move/copy the lib and include folders into the src folder, move/copy the SDL2.dll from bin into the main folder, make a build.sh file with the text gcc -Isrc/Include -Lsrc/lib -o main main.c -lmingw32 -lSDL2main -lSDL2 in it, then open the Git Bash terminal, and finally run bash build.sh to build the executable file and ./main to run it. Do not use a run button; we do not know what they do.
So !solved time?
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity

