0

Edit with respect to solution suggestion: The solution for the author of the question was that he put the main function into a namespace. Then he added parameters to the main function, then he used extern "C" in order to get his main to be recognized as entry point. Answers, which looked detailed suggested that the code author did not define a main/win main function (which was debated). I also don't use Visual C++, but a mingw version which works on my AMD computer (its a win 64 variant on a windows 10 home edition).

I have a problem, which seems frequently to find its way to StackOverflow and I could not locate any answer which helped me resolve my problem.

I used codeblocks and made a project as a console application. This is the code I tried to compile and I keep on getting the "undefined WinMain" error.

I used the win64 MinGW compiler, which came with codeblocks Compiler Flags were: (-std=C++11, -std=C++14, and -std=C++17. I used all of previous one at a time, before), -Wall, and -pedantic

This is the code and naturally, I was not able to resolve my problem. The attached images are "Build log" and "Build Messages" from the respective codeblock tabs of the "Logs & other window".

Please help.

#include <iostream>
    
using namespace std;

class Rectangle {
protected:
    int width, height;

public:
    Rectangle(int width = 0, int height = 0): width(width), height(height) {};
    
    int get_width() const {return width;}
    int get_height() const {return height;}
    
    virtual void set_width(int width) {this->width = width;}
    virtual void set_height(int height) {this->height = height;}
    
    int area() const {return width * height;}
};
    
class Square : public Rectangle {
public:
    Square(int size = 0) : Rectangle(size, size) {
        set_width(size);
        this->width = this->height = size;
    }
    
    void process(Rectangle &r){
      r.set_height(10);
      cout << "expected area  was 30, got " << r.area() << std::endl;
    }
    
    int main() {
      Rectangle r(3,4);
      process(r);
      std::cout << "Template" << std::endl;
      return 0;
    }
};
  • 1
    Does this answer your question? [undefined reference to \`WinMain@16'](https://stackoverflow.com/questions/5259714/undefined-reference-to-winmain16) Also https://stackoverflow.com/q/21077636/62576, which is specific to CodeBlocks. Please search this site for the error message before posting, as chances are quite good that it's been asked about before. – Ken White Jun 24 '20 at 01:21
  • 1
    You have to switch from subsystem windows to subsystem console to fix this. Currently your project is set to build a windows GUI application. I can't find a good link that explains this for mingw. – drescherjm Jun 24 '20 at 02:02
  • Further to drescherjim's comment, the option is found in the menus at Project->Properties->BuildTargets(tab)->Type(dropdown) it will currently be set to "GUI application", you need to set it to "Console application" – enhzflep Jun 24 '20 at 02:12
  • In Project->Properties->BuildTargets(tab) The dropdownbox showed Console; I changed that to GUI, and the project compiled (exe file in the debug directory), but no cmd window shows the result of the output, because (a windows window is expcected to be addressed). Please bear with me. – Michaelinscarbororough Jun 24 '20 at 02:27
  • Subsequently to above I uninstalled codeblocks with the associated compiler, downloaded the setup program, again and re-installed codeblocks. The result remains, I am not able to compile a console application. I can create an executable with GUI application;further on, this problem arose sometime today and did not leave ever since. Thank you all for your efforts. – Michaelinscarbororough Jun 24 '20 at 03:04
  • Try creating a new console application for "Hello World" and see if that works. – drescherjm Jun 24 '20 at 13:25
  • In this code why is `int main()` a member of the Square class? `int main()` must always be a free function. – drescherjm Jun 24 '20 at 13:26
  • Yes, that solved it. – Michaelinscarbororough Jun 24 '20 at 22:34

1 Answers1

0

Possibly this is part of the solution: I know that extra compiler flags can be created. This is a warning, which showed, after I wrapped my main function with extern "C" as suggested by one of the answers in your solution suggestion ||warning: command line option '-std=c17' is valid for C/ObjC but not for C++|. In my case I chose the option, where it said -std=c++17, still this warning came up.

  • You should not be wrapping your main with extern "C" and your compiler is probably too old to support c++17. And nither of these have anything to do with the fact that the linker wants a WinMain() instead of main because you have the compiler settings for GUI application enabled instead of console. – drescherjm Jun 24 '20 at 01:48
  • Is this really an answer to the problem? I don't think it solves the issue or helps. – drescherjm Jun 24 '20 at 01:48
  • Hi drescherjm: I took the extern C and the main parameters outI think you are right, somehow a GUI application was selected. Here is what I did: I selected File / new project, I filtered the codeblock start combo list ("All categories") by selecting "Console" , then I chose Console, which was the first entry of the combo box list, I chose C++ (rather than C) and after using **Go** I entered the name of the project (no spaces). – Michaelinscarbororough Jun 24 '20 at 02:16