wxWidgets LINK issue in Visual Studio - c++

I am trying to deal with wxWidgets for the first time, I tried to compile the below Hello World program:
/*
* hworld.cpp
*/
#include "wx/wx.h"
class MyApp: public wxApp
{
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
enum
{
ID_Quit = 1,
ID_About,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
wxSize(450,340) );
frame->Show(true);
SetTopWindow(frame);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _("Welcome to wxWidgets!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("This is a wxWidgets Hello world sample"),
_("About Hello World"),
wxOK | wxICON_INFORMATION, this);
}
The compiler showed:
Error 1 error LNK2001: unresolved external symbol _WinMainCRTStartup C:\Users\550\documents\visual studio 11\Projects\wxWidgitExample\wxWidgitExample\LINK
Error 2 error LNK1120: 1 unresolved externals C:\Users\550\documents\visual studio 11\Projects\wxWidgitExample\Debug\wxWidgitExample.exe 1
What is the problem ? I use MS Visual Studio, I guess I need to use #pragma directive?

Try putting wxDECLARE_APP(MyApp); in the header file where MyApp is defined and wxIMPLEMENT_APP(MyApp); in the cpp file where it is implemented. The description of these macros is avaliable in the documentation.

Check your property sheets under Linker->System->SubSystem. Make sure the subsystem is Windows and not Console.

That linker error you're getting usually indicates a mismatch in your project configuration settings between ansi vs unicode setup. If you're building your wxApplication with unicode enabled make sure UNICODE and _UNICODE are defined.
The entry point for a windows unicode application is wWinMainCRTStartup where as a non-unicode one has WinMainCRTStartup as its entry point. Normally wxWidgets should have that setup correctly for you when you built the framework.
To help troubleshoot your unresolved error I would try passing this switch to the linker when you attempt to build.
If you're example is not using unicode then use /ENTRY:WinMainCRTStartup.
If you're example is using unicode then use /ENTRY:wWinMainCRTStartup.

Related

Why is a wxWidgets hello world GUI application flagged as a virus by various AVs?

I made a GUI application in wxWidgets and kept receiving anti virus alerts by various users. I spent a great amount of time commenting out code and re-uploading the EXE to VirusTotal. It turns out, none of this was my code. Using the wxWidgets framework alone will cause plenty of detections. I tried again by compiling a simple hello world application and sure enough, this is the result:
Full source code:
// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
// Required for static linking
#pragma comment(lib, "comctl32")
#pragma comment(lib, "Rpcrt4")
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_Hello = 1
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Hello, MyFrame::OnHello)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu* menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu* menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
How can these be fixed without contacting each AV vendor? Clearly, the code is not malicious and wxWidgets is a popular cross-platform GUI framework which shouldn't cause any AV detections on its own. The static EXE however is quite big and pulls in a lot of Windows API functions so it's hard to even pinpoint anything.

Making an application bundle with Xcode, C++ and wxWidgets

I'm new to macos, I'm trying to figure out how to make an application bundle so my code won't be just an executable file.
I'm working with xcode version 12.5 and writing a test gui application using c++ language and the wxWidgets library.
Now I tried to make a simple gui with just a button and a menu bar (The bar on top of the screen with different menus like File or About ). I encountered 2 issues :
The first and the major one is that the menubar doesn't respond when the executable starts, it only works when focused on another program and then focuses back to the executable, then the menu is clickable and working. I read that this happens because I build the program as an executable and not as an application bundle. Although, couldn't figure out how to do so in xcode, and the internet doesn't quite answers how to do so.
The button doesn't have a click effect when you only touch the mousepad ( you have to click it to make a blue click effect ), you have to click and hold the button to see the button change color.
CODE :
cpp file of the app ( inherits from wxApp )
bool instaStalkApp::OnInit(){
instaFrame = new instaStalkFrame("InstaStalk", wxPoint(50,50), wxSize(P_WIDTH,P_HEIGHT));
instaFrame->Show(true);
SetTopWindow(instaFrame);
mainPanel = new instaStalkPanel(instaFrame, ID_PANEL_MAIN, wxPoint(-1,-1), wxSize(P_WIDTH, P_HEIGHT), wxTAB_TRAVERSAL, "panel");
testButton = new instaButton(mainPanel, ID_TEST_BUTTON_1, "test1" , wxPoint(P_WIDTH / 2 ,P_HEIGHT / 2), wxSize(100,20), "test1but");
return true;
}
Header file of the button class :
#ifndef instaButton_hpp
#define instaButton_hpp
#include <stdio.h>
#include <wx/wx.h>
class instaButton : public wxButton {
private:
public:
instaButton(wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos, const wxSize &size, const wxString &name);
void On_Button1Click(wxCommandEvent &event);
wxDECLARE_EVENT_TABLE();
};
#endif /* instaButton_hpp */
event table of the button :
wxBEGIN_EVENT_TABLE(instaButton, wxButton)
EVT_BUTTON(ID_TEST_BUTTON_1, instaButton::On_Button1Click)
wxEND_EVENT_TABLE()
cpp file of custom button
#include <wx/wx.h>
#include "instaButton.hpp"
instaButton::instaButton(wxWindow *parent, wxWindowID id, const wxString &label, const wxPoint &pos, const wxSize &size, const wxString &name) : wxButton(parent, id, label, pos, size, 0, wxDefaultValidator, name){
}
void instaButton::On_Button1Click(wxCommandEvent &event){
wxLogMessage("hi");
}
cpp file of the frame with the menuBar :
#include <wx/wx.h>
#include "instaStalkFrame.hpp"
#include "instaStalkPanel.hpp"
#include "consts.hpp"
instaStalkFrame::instaStalkFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, pos, size){
menuFile = new wxMenu;
menuFile->Append(ID_TEST, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
menuBar = new wxMenuBar;
menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar(menuBar);
}
void instaStalkFrame::OnExit(wxCommandEvent& event){
Close(true);
}
void instaStalkFrame::OnAbout(wxCommandEvent& event){
wxMessageBox( "This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION );
}
void instaStalkFrame::OnTest(wxCommandEvent& event){
wxLogMessage("Test!");
}
In order to make an Application Bundle:
You should have been creating an OSX Application project inside Xcode. Check wxWiki (I know it is for older version of Xcode, but it still applies and you can try to match it in the newer version).
If you want to do that later - you can try to build minimal sample from thr wxWidgets distribution, see what it uses in order to create a bundle and replicate that in you test program. But you absolutely should make an Application Bundle project in Xcode to simplify your life.
The menu will start working when you create a Bundle. As you should.
I don't think hovering effect is implemented. You can ask about it on wx-users ML.
Let us know if you need any more guidance.

How to use wxWidgets with SFML?

I am making a series of applications on physics simulations and until now I've done some of them using purely the handy SFML library. Now I want to add some GUI elements to them. I am just a little bit familiar with wxWidgets framework and wxFrames but have no idea how wxDc works, which seems to be at the core of SFML-wxWidgets integration. I will list my queries one by one.
My shot at the old tutorial provided by SFML dev webpage: The final code, after correcting for the trivial errors looks like this:
#include <SFML/Graphics.hpp>
#include <wx/wx.h>
class wxSFMLCanvas : public wxControl, public sf::RenderWindow
{
public:
wxSFMLCanvas(wxWindow* Parent = NULL, wxWindowID Id = -1, const wxPoint& Position = wxDefaultPosition,
const wxSize& Size = wxDefaultSize, long Style = 0);
virtual ~wxSFMLCanvas();
private:
DECLARE_EVENT_TABLE()
virtual void OnUpdate();
void OnIdle(wxIdleEvent&);
void OnPaint(wxPaintEvent&);
void OnEraseBackground(wxEraseEvent&);
void OnSize(wxSizeEvent&);
};
void wxSFMLCanvas::OnUpdate()
{
}
void wxSFMLCanvas::OnIdle(wxIdleEvent&)
{
// Send a paint message when the control is idle, to ensure maximum framerate
Refresh();
}
void wxSFMLCanvas::OnPaint(wxPaintEvent&)
{
// Prepare the control to be repainted
wxPaintDC Dc(this);
// Let the derived class do its specific stuff
OnUpdate();
// Display on screen
display();
}
void wxSFMLCanvas::OnEraseBackground(wxEraseEvent&)
{
}
void wxSFMLCanvas::OnSize(wxSizeEvent&)
{
}
wxSFMLCanvas::wxSFMLCanvas(wxWindow* Parent, wxWindowID Id, const wxPoint& Position, const wxSize& Size, long Style) :
wxControl(Parent, Id, Position, Size, Style) {
sf::RenderWindow::create(GetHandle());
}
wxSFMLCanvas::~wxSFMLCanvas()
{
}
class MyCanvas : public wxSFMLCanvas
{
sf::CircleShape circ;
public:
MyCanvas(wxWindow* Parent, wxWindowID Id, wxPoint& Position, wxSize& Size, long Style = 0) : wxSFMLCanvas(Parent, Id, Position, Size, Style) {
// specify circle
circ.setRadius(50.0f);
circ.setOrigin(50.0f,50.0f);
circ.setFillColor(sf::Color::Blue);
circ.setPosition(100.0f, 100.0f);
}
private:
virtual void OnUpdate() {
// Clear the view
clear(sf::Color(0, 128, 128));
// Display the circle in the view
draw(circ);
}
};
class MyFrame : public wxFrame
{
public:
MyFrame() : wxFrame(NULL, wxID_ANY, "SFML wxWidgets", wxDefaultPosition, wxSize(800, 600)) {
wxPoint tmp1 = wxPoint(50, 50);
wxSize tmp2 = wxSize(700, 500);
new MyCanvas(this, wxID_ANY, tmp1, tmp2);
}
};
class MyApplication : public wxApp
{
private:
virtual bool OnInit()
{
// Create the main window
MyFrame* MainFrame = new MyFrame;
MainFrame->Show();
return true;
}
};
IMPLEMENT_APP(MyApplication);
This throws a bunch of unresolved externals:
Error LNK2001 unresolved external symbol "protected: virtual struct wxEventTable const * __cdecl wxSFMLCanvas::GetEventTable(void)const " (?GetEventTable#wxSFMLCanvas##MEBAPEBUwxEventTable##XZ) test1 C:\Users\rajat\source\repos\test1\test1.obj 1
Error LNK2001 unresolved external symbol "protected: virtual class wxEventHashTable & __cdecl wxSFMLCanvas::GetEventHashTable(void)const " (?GetEventHashTable#wxSFMLCanvas##MEBAAEAVwxEventHashTable##XZ) test1 C:\Users\rajat\source\repos\test1\test1.obj 1
Error LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) test1 C:\Users\rajat\source\repos\test1\MSVCRTD.lib(exe_main.obj) 1
Furthermore, I don't quite understand how in this example wxWidgets is supposed to draw the contents of a sf::RenderWindow. It will be very helpful if someone can rectify this barebone code so that it works. And also hopefully explain how it works.
Is there an alternative to wxWidgets that would work with SFML, or has a handy tool for fast and simple graphical library? The only requirement is that I should be able to compile it on MSVS for x64 framework and statically link to my application. I considered Qt, but I can't get it to either compile on my own, or even if I do, it's only the 32-bit shared library configuration.
How is the code supposed to work? The sf::RenderWindow is created using sf::RenderWindow::create(GetHandle()). Is it supposed to draw everything on its own? Or do I have to copy the bitmap of the SFML window and draw on the wxControl by myself? It will be really helpful if someone could shed some light on how it's supposed to work.
Your error is due to using DECLARE_EVENT_TABLE but not using any wxBEGIN_EVENT_TABLE/wxEND_EVENT_TABLE, i.e. you never define your event table nor connect any event handlers.
I don't know anything about SFML, so I can't really help you with the rest, but apparently drawing is supposed to be done from your wxEVT_PAINT handler -- once you connect it -- by just calling sf::RenderWindow::Display().
I finally got them to work together. Here's a minimal code that shows a circle on a button click:
#include <SFML/Graphics.hpp>
#include <wx/wx.h>
class wxSFMLCanvas : public wxControl, public sf::RenderWindow
{
public:
wxSFMLCanvas(wxWindow* Parent = NULL, wxWindowID Id = -1, const wxPoint& Position = wxDefaultPosition,
const wxSize& Size = wxDefaultSize, long Style = 0);
virtual ~wxSFMLCanvas();
};
wxSFMLCanvas::wxSFMLCanvas(wxWindow* Parent, wxWindowID Id, const wxPoint& Position, const wxSize& Size, long Style) :
wxControl(Parent, Id, Position, Size, Style) {
sf::RenderWindow::create(GetHandle());
}
wxSFMLCanvas::~wxSFMLCanvas() {
}
class cMain : public wxFrame
{
wxButton* m_btn1 = nullptr;
wxSFMLCanvas* mycanv = nullptr;
wxDECLARE_EVENT_TABLE();
public:
cMain();
void onBtnClicked(wxCommandEvent &evt);
};
wxBEGIN_EVENT_TABLE(cMain,wxFrame)
EVT_BUTTON(10001, onBtnClicked)
wxEND_EVENT_TABLE()
cMain::cMain() : wxFrame(nullptr, wxID_ANY, "Window", wxPoint(30, 30), wxSize(800, 600)) {
mycanv = new wxSFMLCanvas(this, wxID_ANY, wxPoint(50, 50), wxSize(700, 500));
m_btn1 = new wxButton(this, 10001, "button1", wxPoint(10, 10), wxSize(100, 50));
}
void cMain::onBtnClicked(wxCommandEvent& evt) {
sf::CircleShape circ(50.0f);
circ.setOrigin(50.0f, 50.0f);
circ.setFillColor(sf::Color::Blue);
circ.setPosition(350, 250);
mycanv->draw(circ);
mycanv->display();
}
class myApp : public wxApp
{
cMain* m_Frame1 = nullptr;
public:
myApp();
virtual bool OnInit();
};
wxIMPLEMENT_APP(myApp);
myApp::myApp() {
}
bool myApp::OnInit() {
// Create the main window
m_Frame1 = new cMain;
m_Frame1->Show();
return true;
}
The executable of this code(everything statically linked) are ~10MB(debug mode) ~4MB(release mode). I'm leaving this code here in case anyone else finds this useful.

two error messeges saying cant excute becuse missing files (wxmsw312u_core_vc_custom.dll,wxbase312u_vc_custom.dll) how to solve it

i installed wxwidgets to visual studio 2019 first i install the wxwidgets for visual studio then i open "sln 15" then i buile it in debug,debug dll ,relese dll and relese then i i open project properties and change some properties to include include folder and liberary
and then i copied the "hello world" example on widgets site but when i pressed the debug button to run the code two error messages showed up the first is cant execute because cant find"wxmsw312u_core_vc_custom.dll"
the second cant find"wxbase312u_vc_custom.dll"
the code is :
`
// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame();
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
};
enum
{
ID_Hello = 1
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame();
frame->Show(true);
return true;
}
MyFrame::MyFrame()
: wxFrame(NULL, wxID_ANY, "Hello World")
{
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets Hello World example",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}`
When you build wxWidgets libraries as DLLs, you need these DLLs to run the applications using them, just as with any other DLL. The DLLs don't need to be in the same directory as the application (although it does work if you do it like this), you may also add the directory containing them to your PATH environment variable.
Alternatively, build wxWidgets as static libraries -- then you won't have any run-time dependencies on them.

Cant focus WxWidgets frame in Mac OSX compiled with SCons

I have this WxWidgets test source code that compiles, and when run, it shows a simple frame:
/*
* hworld.cpp
* Hello world sample by Robert Roebling
*/
#include "wx-2.8/wx/wx.h"
class MyApp: public wxApp
{
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
enum
{
ID_Quit = 1,
ID_About,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _T("Hello World"), wxPoint(50,50), wxSize(450,340) );
frame->Show(TRUE);
SetTopWindow(frame);
return TRUE;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _T("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _T("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _T("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _T("Welcome to wxWindows!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(_T("This is a wxWindows Hello world sample"),
_T("About Hello World"), wxOK | wxICON_INFORMATION, this);
}
Created with this simple SCons script:
env = Environment()
env.ParseConfig('wx-config --cxxflags --libs')
env.Program(target='wxTest/wxTest.exe',source=['src/Wxwidgets.cpp'])
The problem: it wont focus when I run it. The only thing I can focus is the red, yellow and green buttons in the upper left corner.
I use Eclipse as my IDE and run scons as an external tool when I build it.
Is there someone out there that know what Im doing wrong? How can I get the frame to focus?
Hope there is someone out the who can help me.
I assume you start the raw executable that is created? This does not work on Mac OS X, see My app can't be brought to the front!
You will have to create an application bundle for your app to work properly on Mac OS X. I don't know anything about SCons, but maybe the wiki does help?