Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML g++ undefined reference to 'WinMain@16'  (Read 4985 times)

0 Members and 1 Guest are viewing this topic.

AnthoJack

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
SFML g++ undefined reference to 'WinMain@16'
« on: February 05, 2019, 10:16:30 pm »
Hi there,

I'm trying to compile some SFML C++ code on Windows 10 with Visual Studio Code and I keep having that mistake. I'm running VSCode tasks to compile and link SFML library with g++ and while compilation (g++ -IC:\lib\SFML\include -c main.cpp -o main.o) has no problem I'm getting that error whenever I try to link the libraries (g++ -LC:\lib\SFML\lib -o GUImeOfLife.exe -o main.o -lsfml-graphics -lsfml-window -lsfml-system) as indicated in the tutorial on how to compile with g++ (a little different of course since I'm doing it on Windows)

C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

I've looked on the internet for quite some time now and apparently it can happen when there is no main.cpp file or main or WinMain function to compile but I've triple checked my code and even tried to replace main with WinMain or even to add the "argc" and "argv" arguments to my function but it won't fix my problem.

Here's my main.cpp code to review

#include <SFML/Graphics.hpp>

int main(int argc, char *argv[])
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(shape);
        window.display();
    }
    return 0;
}
 

Could the fact that I'm working on VSCode have any impact on that ?

I have to have SFML working by the end of the week for a school project so if anyone could help me figure this out I'd be the happiest man on earth

Thanks

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: SFML g++ undefined reference to 'WinMain@16'
« Reply #1 on: February 05, 2019, 10:50:30 pm »
Either you didn't list it or the TDM GCC version has some additional weird quirk and defines -mwindows by default. You can quite easily fix this by linking sfml-main.

I have to have SFML working by the end of the week for a school project so if anyone could help me figure this out I'd be the happiest man on earth
Note that your teacher/professor/assistant is being paid to help you, so you should always ask them first, as we're here on our own time. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

AnthoJack

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: SFML g++ undefined reference to 'WinMain@16'
« Reply #2 on: February 06, 2019, 08:15:06 am »
I've tried it
g++ -LC:\lib\SFML\lib -o GUImeOfLife.exe -o main.o -lsfml-main -lsfml-graphics -lsfml-window -lsfml-system
 

Sadly it hasn't fixed my problem

C:/TDM-GCC-32/bin/../lib/gcc/mingw32/5.1.0/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
 

Quote
Note that your teacher/professor/assistant is being paid to help you, so you should always ask them first, as we're here on our own time. ;)

It is an individual and quite free project where we got to choose which "technology" we wanted to use and my teacher has confessed to not being quite familiar with SFML but he let me pick it because they are here to monitor and not to teach us how to use a technology in particular. Even if he was, there's a rotation between teachers who monitor the project and none of them could fix my problem and my teacher isn't there till friday

AnthoJack

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: SFML g++ undefined reference to 'WinMain@16'
« Reply #3 on: February 06, 2019, 09:34:09 am »
OK I kinda fixed it.
I linked lmingw32 such that now my command looks like that
g++ -LC:\lib\SFML\lib -o GUImeOfLife.exe -o main.o -lmingw32 -lsfml-system -lsfml-window -lsfml-graphics -lsfml-main
 
and TADAAAAA no error anymore.
One more problem though: I couldn't find my GUImeOfLife.exe but I fixed it by swapping "GUImeOfLife.exe" and "main.o" like that
g++ -LC:\lib\SFML\lib -o main.o -o GUImeOfLife.exe -lmingw32 -lsfml-system -lsfml-window -lsfml-graphics -lsfml-main
 
putting that here so eventual other people finding this won't do this same mistake by blindly copying

Now I've just got one last problem: my program crashes and won't display anything else than a command prompt and the infamous "GUImeOfLife.exe stopped working" even though I have my DLLs in the same folder as my program.
Any thoughts ?