News:

When registered with our forums, feel free to send a "here I am" post here to differ human beings from SPAM bots.

Main Menu

Whenever I add a class I get "Undefined reference to WinMain"

Started by SuperBoi45, November 10, 2014, 08:11:03 PM

Previous topic - Next topic

SuperBoi45

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?

oBFusCATed

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!]

SuperBoi45

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?

oBFusCATed

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!]


oBFusCATed

(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!]

SuperBoi45


oBFusCATed

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!]

stahta01

Quote from: SuperBoi45 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?

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

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

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.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

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.


-O3 -O2 -O1


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

oBFusCATed

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!]

stahta01

Quote from: oBFusCATed on November 11, 2014, 01:38:13 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++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

oBFusCATed

Quote from: stahta01 on November 11, 2014, 02:16:06 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!]

JohnnyCrow

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.

oBFusCATed

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!]