abc
abc

Reputation: 25

C++ - undefined reference to `WinMain@16'

I installed Visual Studio Code, and I'm trying to set up SFML. This is the link to the YouTube video I'm following: https://www.youtube.com/watch?v=rZE700aaT5I (timestamp: 05:17)

The following code block is what I have in my main.cpp so far. I got this code from the SFML website. Here's the link for your reference: https://www.sfml-dev.org/tutorials/2.6/start-linux.php

#include <SFML/Graphics.hpp>

int main()
{
    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;
}

Here's what I typed in the terminal so far. Please remember that I replaced <(sfml-install-path)> with the correct path.

  1. g++ -c main.cpp -I<(sfml-install-path)>/include (this made a main.o)

  2. g++ main.o -o sfml-app -L<(sfml-install-path)>/lib -lsfml-graphics -lsfml-window -lsfml-system

After I press Enter after 2, I get a message saying:

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'

YouTube and StackOverflow offered a few reasons/solutions:

  1. I don't have a main(), so create a main().
    -> I have a main(), so I don't think this is the issue.

  2. I didn't save before running.
    -> I initially did not save before running and thought this was the problem. However, I Ctrl+S and ran it, and I'm still getting this issue. I also went to Settings, and searched up 'Code runner' and 'Save all" to see if the Code-runner:Save All Files Before Run was checked off, and I don't even see that on my screen.

  3. Add -mwindows at the end (so I would type g++ main.o -o sfml-app -L<sfml-install-path>/lib -lsfml-graphics -lsfml-window -lsfml-system -mwindows)
    -> This also did not work, and I'm running into the same issue.

  4. Double-check the path.
    -> I did, and it's the correct path.

What am I missing out on? Also, I'm new to VS Code and SFML, so please be patient with me.

Upvotes: -1

Views: 192

Answers (1)

Blindy
Blindy

Reputation: 67487

If you're compiling for the Windows subsystem, the entry point is expected to be as such:

int __clrcall WinMain(
  [in]           HINSTANCE hInstance,
  [in, optional] HINSTANCE hPrevInstance,
  [in]           LPSTR     lpCmdLine,
  [in]           int       nShowCmd
);

For the console subsystem, the entry point is expected to be as such:

int main(int argc, char *argv[]);

Both, of course, use C linking, so you can get away with simplified (but definitely wrong) definitions if you prefer.

Upvotes: 0

Related Questions