Question

I'm using a cross platform GUI library (FLTK) to make some GUI with, I'm not using the Win32 API. I want this program to run on a Windows, Linux and Mac OS.

I don't really get how cross platform mumbo jumbo works (look at my account name), but I think understand what it means to be cross platform. But that's not my question.

My problem is that my entry function right now is simply int main(), but when I use this entry function, the console pops up. I don't want any consoles, so I know that I could make my entry function like this:

int wWinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPWSTR nCmdLine, int nCmdShow)

and there would be no console.

My fear is that since I have to use the Win32 API to have this wWinMain entry function (because of the HINSTANCEs and the LPWSTR), the program would no longer be compatible with other OS's.

So my question is, will my program work on a Mac and Linux OS if I use the wWinMain function? If not, is there a function that I can use that creates no console and will work on all OS's?

Finally, perhaps I'm completely delusional about being able to have the same .exe program work on any OS, and that is in fact not possible. Is this so?

Was it helpful?

Solution

*NIX systems shouldn't spawn terminal windows by default, so you'll probably only have to do something special on Windows.

If you can wrap the WinMain function, that is, put #define guards around it and call another function that acts as the "real" main inside of it, and then call that function from main on other platforms there should be no major issues.

However, I'd strongly suggest avoiding wide characters (wchar_t) whereever possible because they differ from platform to platform and handling platform-specific API calls that need them by internally storing all strings as, for instance, UTF-8, converting them (For example via MultiByteToWideChar) only to make the API call.

Basically, non-Windows platforms will need you to use a standard main function, Windows will need you to use WinMain. Make them both call a third function that handles what your main function should and return.

Also, any Windows PE binary (.exe file) will only work on Windows. You'll have to compile your program for each platform separately regardless of what you do.

OTHER TIPS

Have a look at linker options. Linker controls what kind of the executable your program will be.
Having multiple main functions is also an option. #ifdef's can control which one will be used for each platform.

perhaps I'm completely delusional about being able to have the same .exe program work on any OS

this is not possible

It's an old question, but I've just found it and I think it's useful, I've followed user308323 advice and came out with this, which works great once I define WIN32 as a preprocessor define in Visual Studio project.

#ifdef WIN32
int wWinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPWSTR nCmdLine, int nCmdShow)
#endif
#ifndef WIN32
int main(int argc, char **argv)
#endif

{
    Model model;
    UIController controller(model);

    controller.run(NULL, NULL);
}

Notice I've only changed the function signature and I'm sending null as argc and argv arguments, we will not need those anyway because this is not a command line program

About being able to have the same .exe for every platform, well, that's impossible, but that is not a problem as long as you are able to compile the same program on different platforms. That's what FLTK library is for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top