Migrate Win32 C/C++ Applications to Linux 393
An anonymous reader writes "This series of articles helps you migrate your Win32 C/C++ applications to Linux on POWER. Win32 C/C++ Apps to Linux Part-1 of this series covers the Win32 APIs mapping to Linux on POWER regarding the initialization and termination, process, thread, and shared memory services. Win32 C/C++ Apps to Linux Part-2 illustrates how to map Win32 to Linux with respect to mutex application program interfaces (APIs)."
Re:Portable code (Score:5, Informative)
The problem usually comes when dealing with the GUI or other slightly lower level system functions which sometimes can't be wrapped elegantly.
Re:Portable code (Score:5, Informative)
DirectX - OpenGL is a good example (for graphics). Both accomplish pretty much the same, have similar features and perform the same. Yet, telling OpenGL "hey, draw me a few polygons here" can be completely different than telling DX the same. Now, OpenGL is supported in Linux, so if your Windows program uses it, converting it to Linux becomes more or less inmerdiate (you'll still have to retouch here and there, but not much). Not the other way arround - converting DX to OGL might be doable, but doing it consistently all over big programs it's an awful chore.
This also happens in quite a few other areas, not all related to the so called "multimedia". For example, widgets (the windows and buttons drawn on screen) might be handled completely the same. Libraries allowing you to do similar things on both OSs can be completely different to implement. Audio, file management, program intercomunication, etc.
The way to deal with this is thinking ahead. It's actually not hard to write portable code - the problem is porting code which wasn't designed for it.
Re:Portable code (Score:3, Informative)
If to systems have very similar network protocols (as most do), it becomes trivial to write a cross platform app that mostly deals with the net in a higher level language. But when you try to optimize it more and more or start linking it to more unique parts of a system (cocoa for instance), the process becomes exponentially more difficult.
Repeating libraries across systems helps this as show by wine, but then the software is nolonger running native to the system, it thinks that it is still running on a windows box.
Re:Portable code (Score:5, Informative)
Well, that's an interesting question. My current work is probably pure evil in the eyes of most slashdotters - I'm currently porting a compiler/interpreter/virtual machine of a special variant of prolog from unix->win32 native code.
While writing c/c++ that's portable seems relatively straight-forward, there's lot of gotchas. Different compilers support standards differently, and GCC is one of the worse in some ways - it supports many, many extensions to the standards - what we'd normally bash Microsoft for 'embracing-and-extending'. As a consequence, code that heavily relies on these features can be somewhat involved to port to another compiler/OS. For example, GCC supports zero length arrays, which are currently causing headaches for me as I attempt to fix a garbage collector.
Also, should you wish to interact with the operating system (ie: write any program more complex than hello_world.c), there is no choice - you must use the relevant api. If you look at the article, you'll see tables, with things like GetCurrentProcess() mapping to getpid() under unix. You don't have a choice which one to use under what OS unless you write a compatibility layer of your own.
And that's the whole thing really - code has to be dependant upon specific libraries if you value your time and/or don't want to reinvent the wheel. Thankfully, it appears that cross-platform libraries are starting to become a bit trendier (eg: QT, GTK, etc). Hopefully this will help here a little.
Should you choose a language like Java, however, these issues are supposed to just go away - a nice ideal... ("Write once, test everywhere", I believe
(I would just like to add that I'd rather code C/C++ under unix with GCC any day - I'd just rather eat.)
Re:Portable code (Score:3, Informative)
Why Not Port Wine?!? (Score:5, Informative)
Winelib was specifically designed to help with porting and you can do it tiny controlled steps:
1. Port from MSVC++ to the MinGW compiler and develop things like a Makefile.
2. Test the app.
3. Port the code from Windows to Linux (not as trivial as you'd think - there are problems with case sensitivity, etc.)
4. Test compiling.
5. Finish compiling using the custom Wine tools, such as winegcc (a wrapper around gcc that makes it behave like MinGW).
6. Test your Winelib app.
7. Begin ripping out chunks of Win32 specific code and replace them with native equivalents - Wine dialogs for KDE/GNOME ones.
8. Test
That's a valid and controlled migration path. Asking developers to basically rewrite massive chunks of code involving threading, memory management, and other such exciting things is a nightmare. Oh, and after rewriting you get to debug it all. Would you trust an enterprise app that just had it's whole threading model replaced?
The part I find amazing though, is just how much they haven't addressed. What do you do when your app relies on COM? What about Windows common controls? What about networking?
Anyway, it looks like IBM missed the ball - a few engineers porting Wine to Power could have solved many of their migration issues. It certainly would have solved every single one mentioned in these articles. Instead they want the software developers of the world to take on the task of rewriting their apps.
Re:Programming in C++ on Linux (Score:5, Informative)
That's because you're running it under the debugger, and the assumption is that if you want it to stick around, you most likely have a breakpoint set somewhere.
Run it outside of the debugger - namely, by hitting CTRL+F5, or the "!" icon - and it'll run and when it terminates it'll ask you to press a key before finishing.
There you go. Learn your tools, and you'll find it much easier to use them.
Re:Why Not Port Wine?!? (Score:3, Informative)
So even if Wine was for some reason compiled to run on Power, you'd still have to add something to emulate an x86 chip.
Comment removed (Score:2, Informative)
Re:Programming in C++ on Linux (Score:3, Informative)
Your main in Visual Studio is probably "void main(void)" and returns nothing. To compile it in gcc, declare "int main()" and make the last line of main "return 0;" and it will probably compile.
Where did this come from? (Score:5, Informative)
Re:Why Not Port Wine?!? (Score:5, Informative)
Of course the matter is oversimplified, and wine definately doesn't cover the entire win32 api yet. It can still provide an easier path than completely rewriting an app. I hope this clarifies things a bit about how wine can be used to not only port windows code to linux, but to linux on a different architecture as well.
How to port CLI programs to OS 9 (Score:3, Informative)
SIOUX basically just creates a simple MacOS 9 app with a window that acts as a console. Just add that, and standard I/O will behave almost exactly like it does on a *nix machine.
binary semaphore and mutex is not the same !!!! (Score:4, Informative)
Re:Where did this come from? (Score:3, Informative)
pSeries = the line of IBM servers that use the PowerPC architecture
Re:Should be: Migrating an App the Worst Possible (Score:5, Informative)
What do you think WINE is? It's exactly that library, plus a loader and relinker and other related tools. You certainly can compile win32 programs against it for porting purposes.
WINE is a binary solution only in the worst case -- if the port is being done by someone with the source, they can recompile against winelib and so be portable to non-x86 targets.
Direct X is not much of a problem. (Score:1, Informative)
Just expand dxglwrap to incude the functions you use. This is Direct X to opengl. Note there are performance problems ie Slower.
The trick is having a platform independant lib. Openoffice and Mozilla depend on a corelib system.
Note porting from Linux to Mac or Amiga is not a problem. It is just porting from windows due to no standard compad because they did not want it.
Only direct hardware access software not access by a standard lib have problems. Ie direct device access. This is they way it sould be.
I18N - UTF-8 vs UTF-16 (Score:4, Informative)
[1] The header is part of a library but you can easily remove the library specific #defines.
int b[0] when you mean int* b is a *bad* habit (Score:4, Informative)
struct {
int a;
int b[0];
} test;
Assuming a 4 byte int and the object is at address 0. (yes, yes, NULL pointer and all that rot)
sizeof(test) = 4
&test.a = 0x0
&test.b[0] = 0x4
&test.b[1] = 0x8
One use is to allocate the memory yourself and then use the struct to map that memory. The sizeof() would give you the header size. Then (sizeof(header) + allocated data storage) is the total size to alloc.
See: Zero Length (gcc) [gnu.org]
IMO, it is a very bad habit to use "type name[0]" when you mean "type* name".
Doing what you claim (sorry, you're AC) is the "Microsoft way" will cause memory corruption in GCC.
Re:Portable code (Score:4, Informative)
If you find the latter lacking the expressiveness, go Hungarian, but don't abuse a unportable laxity in certain compiler. The reason should be obvious from the existance of the grandparents post.
What about good old C++ abstraction and #ifdef? (Score:5, Informative)
The same can be applied to GUIs, though for Windows you'll have to write a WinMain wrapper.
Testing (Score:3, Informative)
I cant write code myself, so obviously there is a lot that i dont know. But is it really that hard to write code that is portable?
Part of the problem is that unless a developer tests their code on another platform, you don't know if it is truely portable (unless you are writing a pretty basic application that is compiled with -ansi -pedantic). As you can imagine, a lot of developers will not take the time to do this testing, maynot want to support other platforms, or maynot own other platforms on which to test their code.
Re:Portable code (Score:3, Informative)
Building on multiple compilers also requires writing code to the subset of functionality common to all of them and avoiding dangerous stuff like STL / exceptions etc. Instead of STL, Mozilla uses its own string classes and portable functionality for threads, collections, memory allocation etc. exposed by the NSPR.
Re:"windows" (Score:4, Informative)
- HN seems, for many people, to serve as a replacement for the actual name. It is easy to write psz1 and psz2 when you want two pointers to zero-terminated strings, but really - I'd rather have their meaning than their type. You may argue that this is simply bad usage (and I'd agree) but I've seen too much code by now that does this to believe it is coincidental. Coming up with good names for variables is hard. Once you have a few characters, even if they don't mean much, people tend to stop thinking and just use it.
- HN adds long strings of unpronouncable garbage throughout the source. That decreases general readability.
- When a variable changes type, you should also rename it everywhere it is used. People tend to not do this, resulting in a system that actually gives the wrong information.
- HN places a great deal of stress on variable type. In my not so humble opinion type is important, but not quite _that_ important. In reality I find myself caring about meaning, then about scope, and finally about type - in that order. And you should be thinking "objects" anyway, not actually caring about type since that's all encapsulated.
More here [triumvir.org]...
Re:Where did this come from? (Score:2, Informative)
Yes and yes (and no) (Score:5, Informative)
While what you say is true to an extent, I think you're ignoring a valid point. Sure, Microsoft plays fast and loose with its own apps, particularly the menu, toolbar and open/save dialogs. Others tailor the interface somewhat as well. But the vast majority of the conventions -- the things an average user will just assume to work -- are the same in pretty much all native Windows apps. A right-click brings up a context menu. You save your work by going to File->Save, and you'll find the Open, Print and Exit commands in the same place. The max/min/restore buttons are at the top-right of the window if applicable. And so it goes.
Some variations on the theme work very well. I was impressed with the simple but effective variation Firefox has adopted for its "find" feature, for example. Others suck: after my first experience using the GIMP on Windows, when it insisted on trying to use completely unfamiliar UI conventions, I gave up.
But the bottom line is that most native Windows apps do have a high level of UI consistency, and if you're going to vary the theme, it should be for a clear reason (such as the Firefox feature I mentioned) and not just because you're developing an app with an inadequate toolkit that can't handle native widgets properly.
Native look and feel (Score:3, Informative)
And if I were on a Mac, I'd expect it to look and feel like on a Mac. I would _not_ want an app that looks and acts like Windows (e.g., wants me to right-click) in the middle of an Aqua desktop.
You are 100% right --- ported apps should have "native look and feel" on each platform! A good cross-platform API that has been designed with this goal in mind since the start is wxWidgets [wxwidgets.org].
Re:Where did this come from? (Score:3, Informative)
Firstly, simple things like spawning threads/processes are typically the least of your worries when porting a Win32/MFC app, because these are small simple APIs with no or little dependencies and easy to abstract. It usually takes a few hours to give these wrappers and add a few simple preprocessor directives (e.g. #ifdef WIN32) to compile the right implementation on the right platform. GUI stuff is usually the big pain.
Secondly: I wonder what all these classes are then: wxCriticalSection, wxThread, wxMutex, etc. wxWidgets covers a lot of stuff, not just UI stuff. We use it for our apps, and it really is a great solution, not only is it a well-designed easy to use API, but has "native look and feel" on each platform.
Re:Portable code (Score:3, Informative)
Reliance on COM/ActiveX controls and libraries can make porting quite a chore - even when similiar functionality exists on Linux (and it usually does), the API is totally different and that affects the basic application architecture..
All my desktop apps are totally cross-platform (Score:1, Informative)