I set up my application with the SDL Framework and it works without any error.
But when I try to start my program it terminates immediately, even before entering my simple main method. Here the code:
#include "CApp.h"
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Hello";
return 0;
}
I know that SDL implements its own main function in SDLMain.m and starts manually my main function. I think that I found the code in STLMain.m that executes my main function (line 222ff):
/* Create SDLMain and make it the app delegate */
sdlMain = [[SDLMain alloc] init];
[NSApp setDelegate:sdlMain];
/* Start the main event loop */
[NSApp run];
When I set a breakpoint on [NSApp run] and make a step forward the program terminates.
SDL #defines main to SDL_main in order to transparently use its own main implementation. Since you haven’t included any SDL headers, you don’t have that macro in scope. It should work to simply rename your main to SDL_main or include an SDL header such as SDL.h.
Related
Original Post
I have a Qt application. This application needs to call some function in a dynamic library that is loaded implicitly. In the dynamic library, there is one global variable that is created when the dll is loaded and destroyed when it is unloaded.
Here's the code:
#include <QApplication>
#include <base/BASE_TEST.h>
int main(int qargc, char** qargv)
{
QApplication application(qargc, qargv);
BASE_TEST::myDLLFunction(); // call to a function in an implicitly loaded dynamic library.
return 0;
}
Implementation of myDLLFunction and of the private class of the global object.
#include <base/BASE_TEST.h>
#include <stdio.h>
class MyTest
{
public:
MyTest() { printf("------------------------------\nTEST BEGIN\n------------------------------\n"); }
~MyTest() { printf("------------------------------\nTEST END\n------------------------------\n"); }
};
MyTest test; // created at the library's loading
void BASE_TEST::myDLLFunction()
{
printf("Call from dynamic library\n");
}
If I run the application, here's what being printed in the command prompt:
------------------------------
TEST BEGIN
------------------------------
Call from dynamic library
------------------------------
TEST END
------------------------------
Up to here all is well. However, if I retrieve some information about the number of screens using QApplication::desktop(), the global object of the dynamic library isn't destroyed.
int main(int qargc, char** qargv)
{
QApplication application(qargc, qargv);
QDesktopWidget* desktop = QApplication::desktop(); // This call prevent the global objects to be destroyed.
BASE_TEST::myDLLFunction(); // call to a function in an implicitly loaded dynamic library.
return 0;
}
Here's what is printed in the command prompt:
------------------------------
TEST BEGIN
------------------------------
Call from dynamic library
The main function still returns normally and no exception is thrown.
I looked at the code of QApplication and QDesktopWidget and the QDesktopWidget destructor is being called at the end of the main function's scope and QDesktopWidgetPrivate::cleanup() is called.
I'm on Windows, using Qt 4.8.6.
Does someone has any idea?
Thanks! :)
Edit
As mentionned in the answer below, the problem seems to be linked to loading wintab32.dll which will load the Wacom driver's dynamic library if installed.
I finally found the source of the issue:
Calling QApplication::desktop() made Wacom_Tablet.dll be loaded. By uninstalling the Wacom driver, the problem went away.
I was able to reduce the sample program to:
#include "../baseTest/BASE_TEST.h"
#include <wtypes.h>
#include "wintab.h"
typedef UINT(WINAPI *PtrWTInfo)(UINT, UINT, LPVOID);
static PtrWTInfo ptrWTInfo = 0;
int main(int /*qargc*/, char** /*qargv*/)
{
BASE_TEST::myDLLFunction(); // call to a function in an implicitly loaded dynamic library.
HMODULE hWintab = LoadLibrary(L"wintab32.dll");
PtrWTInfo pWTInfo = (PtrWTInfo)GetProcAddress(hWintab, "WTInfoW");
WORD thisVersion;
pWTInfo(WTI_INTERFACE, IFC_SPECVERSION, &thisVersion);
if (hWintab)
FreeLibrary(hWintab);
return 0;
}
and still be able to reproduce the issue.
I've contacted Wacom about it and am waiting their reply.
everybody.
I am working on a gtkmm app and need some help getting a "Close" button to work. As suggested by the gtkmm documentation, I derived a class for the main window object, created some members, and left the main() function mostly for reading the glade UI file, instantiating the form and starting the main loop.
There are 3 files, named conveniently for explanation: Declarations.h, Declarations.cpp, Program.cpp
In "Declarations.h" I have the class inherited from the Gtk Window:
#include <gtkmm.h>
class MainWindowClass : public Gtk::ApplicationWindow
{
protected:
Gtk::Button *button_close;
// other buttons here
protected:
void on_button_close_clicked();
// other callback functions here
public:
MainWindowClass(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &refGlade); // Constructor
// Destructor, other public members
};
In "Declarations.cpp" I have the implementations:
#include "Declarations.h"
using namespace Gtk;
// Implementing the constructor
MainWindowClass::MainWindowClass(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &refGlade) :
Gtk::Window(cobject), builder(refGlade)
{
builder->get_widget("button_close", button_close);
// Getting other widgets from the glade file
button_close->signal_clicked().connect(sigc::mem_fun(*this, &MainWindowClass::on_button_close_clicked));
// Connecting other callback functions here
}
// Implementing the callback for the "Close" button, ** PROBLEM IS HERE **
void MainWindowClass::on_button_close_clicked()
{
//gtk_main_quit(); Apparently GTK+/C only, compiler doesn't complain but causes a segfault when clicking the button
//Gtk::Application::quit(); Won't compile
}
The Program.cpp reads the UI from a file and starts the main program loop:
#include <gtkmm.h>
#include "Declarations.h"
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "Damn this close button");
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("Program_UI.glade");
MainWindowClass our_main_window;
return app->run(our_main_window);
}
I am omitting some non-relevant code (of other objects and callbacks) because they work, it is the close procedure that is causing me trouble, though closing the app with "X" works.
I have also thought about trying to call a quit() or destroy() function (if they exist) of "app", but then the callback function doesn't know "app" exists.
What do you guys suggest?
Thanks a lot.
** Edit: fixed this using FormMain::hide(), which is inherited from GtkWindow.
I thought the static procedure Gtk::Main::hide() would do it, but the compiler says that hide() is not a member of Gtk::Main...
Well, moving forward one step at a time.
Used FormMain::hide() (inherited from GtkWindow). The static procedure Gtk::Main::hide() was not being recognized by the compiler.
I need to call a subproject from a main project and implemented two ways of doing that.
It turns out, that the second way is a factor 4 slower than the first.
Can anybody explain this to me?
The subproject looks like this:
#include "fancyProject.h"
int main (int argc, char *argv[])
{
std::string controlFile = argv[1];
return runFancyProject(controlFile);
}
First way: Call the binary of the subproject in the main project via the system() function:
std::string command = "fancyProject controlFile.dat";
int result = system(command.c_str());
Second way: Create a library from the subproject, link this library with the main project and call the specific function:
#include "fancyProject.h"
std::string controlFile = "controlFile.dat";
int result = runFancyProject(controlFile);
In the meantime, I created a minimal example. However, it behaves as expected: The system() function is slower than the call of the linked function. Thus, the error has to be somewhere else in the project. Nevertheless thank you very much for your time, especially to dsboger. I will do further investigations in this direction.
WinMain is a function that 'replaces' the default main entry point 'main'.
The user can then define its main entry point like
int WINAPI WinMain(...) { }
How is this kind of encapsulation done?
Well, most likely, at some point it looks like this:
int main() // This must be defined somewhere in windows.h
{
return WinMain(...);
}
Question: How can I accomplish such an encapsulation of my own, which then calls WinMain? Note: The library which I made is a DLL, so it will look like this:
// This is the minimal code for the application which uses 'MyLibrary'
#pragma comment(lib, "MyLibrary.lib")
include "MyLibrary.h"
void Main(MyCustomParameter *params)
{
// Write user code here
}
The problem however is, that the DLL doesn't 'know' the Main() function and therefore throws an 'unresolved external symbol' compile error. So how can I encapsulate it like this?
You have to decide on a signature of your custom main function and declare it as "extern" (extern "C" in case of C++). Then, application code will have to define that function and link against your static library that has the actual _main entry point. For example:
extern "C" int my_main(int argc, char *argv[]);
int main(int argc, char *argv[])
{
return my_main(argc, argv);
}
Actually, the real entry point is neither main nor WinMain. The real entry point is one of wWinMainCRTStartup, WinMainCRTStartup, wmainCRTStartup, and mainCRTStartup. But they're not defined in Windows.h, they're part of the CRT. You can see their code in <VS installation folder>\VC\crt\src\crtexe.c. They each do some initialization and then call one of wWinMain, WinMain, wmain, and main, respectively.
As mentioned by someone else you can override the entry point with the /ENTRY switch, but you still can't have custom parameters, which is the whole reason you want to do this in the first place.
The linker default entry point name is "main".
You can override the default to start with any function you want.
/ENTRY (Entry-Point Symbol)
I've been working on this program using Qt in c++ and so far so good. However I then needed to get this program moved to another machine. I have subversion, so I committed every file in the project folder and checked it out on the new machine. After jumping through some hoops to get it to successfully build and running, I get this error:
ASSERT: "dst.depth() == 32" in file qgl.cpp,.
invalid parameter passed to c runtime function qt
I tried stepping through the program to find the point where it crashes and found that it was after everything had been initialized and show() is called for the class that inherits the QMainWindow class. The c->showView() line calls QMianWindow->show().
----------main.cpp------------
#include <QApplication>
#include "ModelI.h"
#include "ControllerI.h"
#include "Model.h"
#include "Controller.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ModelI *m = new Model();
ControllerI *c = new Controller(m);
c->showView(); <- ERROR HERE
return a.exec();
}
The confusing part of the problem is that the program works perfectly fine on my machine when show() is called. I don't know what could be different between the two machines to make the program behave so differently. Both use the same version of Qt (SDK 2010.05). Both are developing with Eclipse. The only difference I can find is that my compiler is MinGW 4.5.0 and the other machine's is MinGW 4.5.2.
EDIT 1:
This is what Controller::showView() looks like.
void Controller::showView()
{
mView->show();
}
And this is how mView is initialized.
mView = new View(mModel, this);