1

I have just started to learn wxWidgets and have zero experience. I was recommended to check this tutorial.
So I created a 'console application' in Code Blocks 13.12 ( Win 7 ) , selected wxWidgets 3.0 , and tried to insert the code from tutorial into my code. The basic code that Code Blocks provided me upon project creation was :

#include <iostream>

using namespace std;

int main()
 {
     // "hello world" with cout
     return 0;
 }

Despite trying to insert the tutorial code in various ways, the program never compiled, I always got many errors. Apart from my lack of skills using wxWidgets, I suspect one of the reasons may be that I have installed wxWidgets 3.0, and tutorial may be using an older version. Can someone please show me how to write this code in a CodeBlocks project to make it build and run successfully ? Thank you.

EDIT :

The code I tried to compile was :

#include <iostream>

using namespace std;

#include <wx/wx.h>


int main()
{

    class OurApp : public wxApp
{
    bool OnInit()
    {
        wxFrame* frame = new wxFrame(NULL, wxID_ANY, "Our First App");
        frame->Show();
        return true;
    }

};

IMPLEMENT_APP(OurApp)

return 0;
}

This particular version returned errors :

N:\projekticpp\wxTestTutorial2\main.cpp|28|error: expected unqualified-id before string constant| N:\projekticpp\wxTestTutorial2\main.cpp|28|error: a function-definition is not allowed here before '{' token| N:\projekticpp\wxTestTutorial2\main.cpp|28|error: a function-definition is not allowed here before '{' token|

But trying other different ways of putting the code together, I got all sorts of different ones.

James C
  • 901
  • 1
  • 18
  • 38
  • You need to show us error messages and the code you actually tried to compile, not just the code that Code::Blocks generated for you. – Kristian Duske Feb 08 '14 at 06:13
  • @KristianDuske - alright. Will show now. I just thought it was pointless, since I didn't know what I was doing in any case, and someone who knows wxWidgets could just write the proper code straight off. – James C Feb 08 '14 at 11:12
  • Sorry, but I think you should read a proper book on C++ programming before you start messing with wxWidgets... look here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Kristian Duske Feb 08 '14 at 11:31
  • @KristianDuske - Thank you for answer, you are right about missing C++ fundamentals. But at the moment I am not in position where I can learn whole C++ first, and then move to wxWidgets. I am trying to study them both in parallel because I need to write an app. Can you please help me get the code to compile and run, and let me do the studying of the code and language ? :) – James C Feb 08 '14 at 11:48
  • Well, you could try to remove the entire main method before you paste the wxWidgets code into the file. – Kristian Duske Feb 08 '14 at 12:02
  • @KristianDuske - sorry, but how do you mean that ? Removing main from the code gives a record number of 50 errors :) And the only source file in my project is main.cpp – James C Feb 08 '14 at 12:10

2 Answers2

1

Are you including libraries (all .a)? Check out this tutorial:

http://wiki.codeblocks.org/index.php?title=Compiling_wxWidgets_3.0.0_to_develop_Code::Blocks_%28MSW%29

macroland
  • 973
  • 9
  • 27
  • 1
    Hi, after carefully looking at your code it seems like you need to differentiate the main functions of wxWidgets and a regular C++ code. In wxWidgets wxApp provides the entry point of the program therefore you should not wrap it around with another main function. Even if your settings are correct to me your code wont work. – macroland Feb 09 '14 at 04:34
  • Thank you for checking the code, it works much better now, although I haven't yet succeeded in doing what I wanted to. But that would be a topic for different question anyway =) – James C Feb 09 '14 at 11:53
1

IMPLEMENT_APP() or, better, wxIMPLEMENT_APP() is a macro which must occur in the global scope, you can't put it inside the main() function.

And, in fact, if you use it, you don't need main() at all as it is generated by this macro expansion.

Finally, while you didn't hit this problem yet, once the code compiles it probably won't link because you chose the wrong application type: it should be a "GUI" (or maybe "Windows") and not a "console" application.

VZ.
  • 21,740
  • 3
  • 39
  • 42