#undefined reference to WinMain

242 messages · Page 1 of 1 (latest)

prime ferry
#

This is my fire time (certainly not first attempt) using any external library in C, and nothing I find seems to make sense. This is the farthest I've been able to get, but I still don't understand what is going on.

tawny galleonBOT
#

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.

prime ferry
#

The tutorial I originally followed in installing SDL2 did use C++, but it also said that it was applicable to C.

vast prairie
#

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?

prime ferry
#

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

▶ Play video
prime ferry
vast prairie
#

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"

prime ferry
#

Same code; very similar error with Git Bash.

vast prairie
#

you need to change directory (cd) into your project first

#

So specifically cd "GUI Game"

prime ferry
#

ok yeah exact same error as last time

vast prairie
#

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.

prime ferry
#

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.

vast prairie
prime ferry
vast prairie
#

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

prime ferry
#

I did.

#

That's what I did at first.

vast prairie
#

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

prime ferry
vast prairie
#

Well, the screenshots you sent didn't show what that printed

prime ferry
#

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.

vast prairie
#

This is again PowerShell

prime ferry
#

Yeah, I have mingw

vast prairie
vast prairie
#

You may have put it in a different folder, but try it

prime ferry
#

I don't seem to have that file

#

My C:/msys64/mingw64/bin/ directory doesn't have a mingw32-make.exe file.

vast prairie
prime ferry
#

I just installed make from the video's decription

vast prairie
#

Ah ok

prime ferry
#

I do have C:/program Files(x86)/GnuWin32/bin/make.exe, which was just downloaded.

#

This stuff is all over the place, wow.

vast prairie
#

Yeah, I highly recommend switching to Ubuntu asap

prime ferry
#

I do not know what you mean by that.

vast prairie
#

Writing C and C++ on Windows is a pain in the ass

#

Linux distributions like Ubuntu make it significantly easier

prime ferry
#

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.

vast prairie
#

You can ignore the Makefile for now, just using the command that he puts inside of it, that should work fine

prime ferry
#

I am ok with that

vast prairie
#

Just put the command in a README.md in your project so it's easy to find

#

Or a simple run.sh

prime ferry
#

A run.sh is a good idea. I'll just do that.

vast prairie
#

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

prime ferry
#

This isn't too complicated of a program.

#

I still have that issue from the very beginning. WinMain has an undeclared reference.

vast prairie
#

And a screenshot of that undeclared reference error again

prime ferry
#

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.

vast prairie
#

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

vast prairie
#

You didn't screenshot your main.c in the filetree
So your main.c is 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

prime ferry
vast prairie
#

So rename main.c to main.cpp, and run this exact command

prime ferry
#

I have to go do something. brb.

prime ferry
#

(I'm still doing that other thing, so I'm oing to be replying quite slow)

scenic shale
#

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

vast prairie
#

But that -DSDL_MAIN_HANDLED might very well be it

scenic shale
#

they are a pain to read without vencord

scenic shale
vast prairie
# prime ferry

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

scenic shale
#

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 thonk

vast prairie
#

I'd personally use something like raylib

prime ferry
scenic shale
#

it's a windowing/event API

#

but it's fine

prime ferry
scenic shale
#

it's just that there is SDL3 if you are willing to build it

prime ferry
vast prairie
#

I'd just stick with SDL2 for now, rather than switching

scenic shale
#

i mean sure

vast prairie
#

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

scenic shale
scenic shale
#

but that's the curse of not using a build system

vast prairie
#

And Windows

scenic shale
#

premake/cmake can work wonders

#

or even MSBuild and VS

#

<#memes message>

vast prairie
#

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

scenic shale
#

i use neither anyways

vast prairie
#

Fair, but I mostly mean in terms of feel and looks

scenic shale
#

but the amount of boilerplate c++ requires, I can't live without an IDE

prime ferry
#

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.

vast prairie
#

I recommend just using what you have right now, cause you're almost at the finish line

prime ferry
#

I'm at the same place I was two hours ago.

vast prairie
#

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

vast prairie
scenic shale
#

you should try just not adding that anyways

prime ferry
scenic shale
#

and then if there is a problem you add it ¯_(ツ)_/¯

vast prairie
#

What nevemlaci said, try removing -lmingw32main entirely for a moment

scenic shale
#

it might just link it implicitly

#

for me i just needed -lSDL2

#

when using SDL

prime ferry
#

Same issue

vast prairie
#

At least you don't have that error anymore at the top

scenic shale
#

hmm

prime ferry
scenic shale
#

do you by any chance use the 32 bit version of sdl

#

and build for 64 bit?

prime ferry
#

That's what the guy in the tutorial did, so I think so.

vast prairie
scenic shale
#

try the 64 bit one

prime ferry
#

Ok, I'll do that.

vast prairie
#

I am wondering why this happens

scenic shale
#

look above

prime ferry
#

So do I just delete lib, include, and SDL2.dll then readd?

vast prairie
#

Right, yeah then probably 32 vs 64 bit issue

scenic shale
scenic shale
#

everything related to sdl

prime ferry
#

win32-x64, right?

scenic shale
#

no clue how they named itbing_shrug

#

this one

prime ferry
#

That's not on here

#

This is the github page for SDL2.30.4

scenic shale
#

i just downloaded the lib for mingw from the releases on github

#

this

#

and itll be in this one

prime ferry
#

That's the one I used previously

scenic shale
#

yes but there are 2 versions in this

#

and

#

u need the x86_64 one

prime ferry
#

Ah.

scenic shale
#

but you really should be using a build system like CMake

prime ferry
#

I tried that, but I couldn't understand how.

#

It was just as confusing as this

scenic shale
#
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)

prime ferry
#

Yup, none of that makes any sense to me.

scenic shale
#

you dont need the IMPLIB property for statics

scenic shale
vast prairie
# scenic shale

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

scenic shale
#

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

prime ferry
#

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");

vast prairie
#

To make sure it's not SDL being fucky

prime ferry
#

It's not SDL.

vast prairie
#

So did you try the main.cpp with the command I gave that you say that?

prime ferry
#

I did gcc main.c -o main

vast prairie
#

What does the main.c look like

prime ferry
#

I switched the file to a c file

vast prairie
#

Okay cool, yeah then it's not SDL

#

Though try the same thing with g++ instead of gcc

prime ferry
#

Same thing

scenic shale
#

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?

vast prairie
#

In order to check what happened you can run echo $?, which prints 0 if your program exited successfully

prime ferry
#

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

vast prairie
#

Try echo $?

scenic shale
#

no point.

vast prairie
#

Cause I have had the exact same issue before, and it was due to having several versions of compilers on my computer

scenic shale
#

but yes, do echo $?

#

idk if it works on windows but try it xd

vast prairie
#

Try echo %ERRORLEVEL% otherwise

prime ferry
#

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"

vast prairie
prime ferry
vast prairie
#

Prints what your program returned

vast prairie
prime ferry
#

Oh, gcc main.c -o main && ./main did print Test like it should

vast prairie
#

bruh

#

forgot to save or smth?

prime ferry
#

So running the file via GUI didn't work, but through command line it did. I expected this button to run the code.

vast prairie
#

We both have absolutely no clue what that button actually does under the hood, so please stop using it

prime ferry
#

That is funny. I really thought it would just execute the executable file, but I guess not.

vast prairie
#

We had no clue you were using that LOL

#

See if you can get SDL working now

prime ferry
vast prairie
#

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

prime ferry
#

What's some very simple SDL function I can run to test things?

vast prairie
#

probably some sort of init call that you can glean from his video

#

or just copy his code if he published it

prime ferry
#

(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.

vast prairie
#

Now to draw something on the screen

#

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()

prime ferry
#

It was for the echo bit

vast prairie
#

Any non-zero return code is what is causing the red dot here, indicating failure btw

prime ferry
vast prairie
#

So no need for echo $? since you have this dot indicating whether it's 0 or not

prime ferry
#

Also the show window part worked.

vast prairie
#

ayy

prime ferry
#

This was such a pain.

vast prairie
#

yeah, from this point on you're free from having to do any setting up

#

just coding now

prime ferry
#

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.

vast prairie
#

So !solved time?

prime ferry
#

!solved

tawny galleonBOT
#

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