Author Topic: Whenever I add a class I get "Undefined reference to WinMain"  (Read 28021 times)

Offline SuperBoi45

  • Multiple posting newcomer
  • *
  • Posts: 14
Whenever I add a class I get "Undefined reference to WinMain"
« on: November 10, 2014, 08:11:03 pm »
Let's say I am writing a C++ program and I want to add a new class.

I would have to go to File->New->Class... and then I select the class. I select "Create" and then the files appear. Everything was working before but now when I build and run I get

c:\mingw\bin\..\lib\gcc\mingw32\4.8.1\..\..\..\libmingw32.a(main.o):main.c:(.text.startup+0xa7)||undefined reference to `WinMain@16'|

Here is the build log:


-------------- Build: Debug in Hybrid (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -g -O3 -O2 -O1 -Wshadow -Winit-self -Wredundant-decls -Wfloat-equal -Wunreachable-code -Wmain -pedantic-errors -pedantic -std=c++11 -Wfatal-errors -Wall -I -c "C:\Documents and Settings\David Godfrey\Desktop\Projects\Hybrid\Foo.cpp" -o obj\Debug\Foo.o

The only class I added was Foo. Does anyone know why this is happening?
« Last Edit: November 10, 2014, 08:34:56 pm by SuperBoi45 »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #1 on: November 10, 2014, 08:21:00 pm »
Yes, there is a bug that adds an empty include path in the settings.
You probably have to remove the empty path from the build options.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline SuperBoi45

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #2 on: November 10, 2014, 08:30:09 pm »
How exactly would I do that?

Also I notice in the build log that it's not linking with my main.cpp. How would I add it?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #3 on: November 10, 2014, 08:31:04 pm »
Can you post a screenshot with the class wizard dialog, just before you press the Ok button?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline SuperBoi45

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #4 on: November 10, 2014, 08:34:10 pm »

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #5 on: November 10, 2014, 08:40:04 pm »
So you're trying to create a class without a name?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline SuperBoi45

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #6 on: November 10, 2014, 08:41:43 pm »
Here's one with the name:


Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #7 on: November 10, 2014, 11:36:28 pm »
Can't reproduce this problem on Linux.
Can someone with a windows try it?

I suppose you're clicking OK twice in the two dialogs that are shown after this one?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #8 on: November 11, 2014, 12:46:40 am »
How exactly would I do that?

Also I notice in the build log that it's not linking with my main.cpp. How would I add it?

Is the file main.cpp part of your project?
If not, add it.
If yes, try rebuilding the project and post the full rebuild log.

I could NOT duplicate the problem on my own Windows build of Git SVN 10030 (I do NOT think any of my patches would affect this issue).

Edit: Please include the Code::Blocks version information.

Build log for console c++ created project then added class Foo
Code
-------------- Build: ReleaseWin32 in testwinmainclass (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Winit-self -Wmain -pedantic-errors -Wfatal-errors -Wall -fexceptions -O2 -Wmissing-include-dirs -Wno-unused-local-typedefs -Iinclude -c E:\Test\testwinmainclass\main.cpp -o obj\ReleaseWin32\main.o
mingw32-g++.exe -Winit-self -Wmain -pedantic-errors -Wfatal-errors -Wall -fexceptions -O2 -Wmissing-include-dirs -Wno-unused-local-typedefs -Iinclude -c E:\Test\testwinmainclass\src\Foo.cpp -o obj\ReleaseWin32\src\Foo.o
mingw32-g++.exe  -o bin\ReleaseWin32\testwinmainclass.exe obj\ReleaseWin32\main.o obj\ReleaseWin32\src\Foo.o  -s 
Output file is bin\ReleaseWin32\testwinmainclass.exe with size 467.00 KB

Build log for Win GUI c++ created project then added class Foo2
Code
mingw32-g++.exe -Wmain -pedantic-errors -pedantic -Wfatal-errors -Wall -std=c++11 -O2 -Wmissing-include-dirs -Wno-unused-local-typedefs -Iinclude -c E:\Test\guiframeaddnewclass\main.cpp -o obj\ReleaseWin32\main.o
mingw32-g++.exe -Wmain -pedantic-errors -pedantic -Wfatal-errors -Wall -std=c++11 -O2 -Wmissing-include-dirs -Wno-unused-local-typedefs -Iinclude -c E:\Test\guiframeaddnewclass\src\Foo2.cpp -o obj\ReleaseWin32\src\Foo2.o
mingw32-g++.exe  -o bin\ReleaseWin32\guiframeaddnewclass.exe obj\ReleaseWin32\main.o obj\ReleaseWin32\src\Foo2.o  -s  -lgdi32 -luser32 -lkernel32 -lcomctl32 -mwindows
Output file is bin\ReleaseWin32\guiframeaddnewclass.exe with size 11.00 KB

Tim S.
« Last Edit: November 11, 2014, 01:06:38 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #9 on: November 11, 2014, 01:10:50 am »
I suggest trying only one optimization code at a time. Note, it is likely unrelated to your problem; but, it is a bad idea to set all three of them in most cases only one of them works.

Code
-O3 -O2 -O1

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #10 on: November 11, 2014, 01:38:13 am »
stahta01: have you enabled all the options as show on the screenshot?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #11 on: November 11, 2014, 02:16:06 am »
stahta01: have you enabled all the options as show on the screenshot?

Prior to this no, but, I just checked the two that were missing and I still do NOT get the error.

The error would happen if main.cpp was NOT part of the windows project; this is the likely cause of the problem in my opinion.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #12 on: November 11, 2014, 10:19:08 am »
The error would happen if main.cpp was NOT part of the windows project; this is the likely cause of the problem in my opinion.
No you're wrong here (I think). The error happens because CB passes -I without a path and messes the command line.

@SuperBoi45: Can you do a full rebuild and then post the full build log?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline JohnnyCrow

  • Single posting newcomer
  • *
  • Posts: 2
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #13 on: December 17, 2014, 09:54:33 pm »
I ran into the same problem a while ago, but I figured out a simple solution to it in case anyone is still having trouble. After creating a new class (with .h and .cpp files) and linking it to the project, you have to save the project, close it, and reopen it. Problem solved. I'm not sure why this works, but it does.
"I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forgo their use." - Galileo Galilei

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #14 on: December 17, 2014, 11:24:14 pm »
JonnyCrow: Can you post exact steps that reproduce the problem with a new minimal project?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline JohnnyCrow

  • Single posting newcomer
  • *
  • Posts: 2
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #15 on: January 27, 2015, 06:03:02 pm »
Sorry for the late reply. Here's what you asked for:

1. I created a new project (just a console project).


2. Without changing any of the default code, I created a new class.


3. I added it to the current project.


4. I added the #include directive in main.cpp, and upon attempting to build the project, the 'undefined reference to WinMain@16' error appears.


5. I saved the project, closed it, and reopened it again.


6. When rebuilding the project, it works fine.


Not sure why this works, but closing the project and reopening it (or restarting Code::Blocks) solves the problem consistently.
"I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forgo their use." - Galileo Galilei

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #16 on: January 27, 2015, 06:16:13 pm »
Can someone else try these steps on windows and then tell me if the problem can be reproduced?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #17 on: January 27, 2015, 06:17:46 pm »
JohnnyCrow: Can you try to reproduce the bug using the latest nightly build?
Can you show the list of plugins you have enabled?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #18 on: January 27, 2015, 09:28:37 pm »
i can't reproduce this bug with a nightly (probably -10~20 commits) on win7

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #19 on: January 27, 2015, 10:45:02 pm »
BlueHazzard: Can you try with the 13.12 release?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #20 on: January 27, 2015, 10:58:06 pm »
Yop, here it is:

Code
-------------- Build: Debug in tmp4 (compiler: GNU GCC Compiler)---------------

[ 33.3%] mingw32-g++.exe -Wall -fexceptions -g -Wno-unused-local-typedefs -I -c G:\tmp\tmp4\main.cpp -o obj\Debug\main.o
[ 66.7%] mingw32-g++.exe -Wall -fexceptions -g -Wno-unused-local-typedefs -I -c G:\\tmp\tmp4\test.cpp -o obj\Debug\test.o
[100.0%] mingw32-g++.exe  -o bin\Debug\tmp4.exe obj\Debug\main.o obj\Debug\test.o   
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
 
Process terminated with status 0 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Build log saved as:

And yes it is the -I option.... don't know why it is there... and why saving helps, but someone has  fixed it in trunk...

[Edit:] saving project is not enough , you have to re open it, so the parser checks for plausibility...

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Whenever I add a class I get "Undefined reference to WinMain"
« Reply #21 on: January 27, 2015, 11:26:23 pm »
Hm, thanks good to know it is fixed:)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]