Windows to Linux porting library Linux Library
Porting a Windows application to Linux can be easier than you expect .
Windows to Linux Porting Library
Are You looking for linux equivalent functions to Windows critical sections, threads, handles etc. Well, here you found it!
Porting Windows applications to Linux can result to an unpredictable time consuming process especially with OS dependend task like threads, events, critical sections, shared libraries, INI files.
The majority of Windows applications were developed without considering the need to port them ever to Linux or at least in the near future. For some Windows applications porting them to Linux can become a porting nightmare. Using the right tools it doesn’t have to be one.
The Windows to Linux Porting Library (W2LPL) offers a Windows (WIN32 API) style API under Linux and enables a smooth port of an Windows application to Linux.
The Windows to Linux Porting Library (W2LPL) eases the transition from a Windows API application to a Linux application by offering some of the most wanted features from the Windows API inside the Linux environment e.g.
Handling of Threads
beginthread, beginthreadex, GetExitCodeThread, GetCurrentThread, GetCurrentThreadId, .
Handling of Shared Libraries (.so)
LoadLibrary, FreeLibrary, GetProcAddress
Handling of Critical Sections
InitializeCriticalSection, EnterCriticalSection, LeaveCriticalSection, TryEnterCriticalSection, DeleteCriticalSection, .
Event handling
CreateHandle, CloseHandle, SetEvent, ResetEvent, PulseEvent, WaitForSingleObject, WaitForMultipleObjects, .
Handling of profile files (INI files)
WritePrivateProfileString, GetPrivateProfileString, GetPrivateProfileInt, GetPrivateProfileSection, .
Searching for files
FindFirstFile, FindNextFile, FindClose, .
Sleep & Timing
Sleep, GetSysTime, GetElapsedTime, .
WIN32 API C/C++ Types
BOOL, DWORD, HANDLE, __int64, ULONG, LPSTR, LPWORD, LPDWORD, .
. and other helper functions.
Please also consult the manual for more details.
The Windows to Linux Porting Library (W2LPL) offers portable functions to incorporate into a Linux 32 bit or 64 bit application.
Speed-up your Windows to Linux port and create a stable Linux application by using the Windows to Linux Porting Library (W2LPL). The W2LPL library is used in commercial applications worldwide and also in SuperCom for Linux.
Customer’s of the W2LPL library are encouraged to post requirements so our engineers can research if a required feature can be implement in order to ease the porting of even more specific parts.
By using the Windows to Linux Porting Library (W2LPL) one also profits from professional technical support offered.
And, if your application does Serial or TCP data communications consider the many options of SuperCom, the portable data communication toolkit.
A Windows C/C++ sample compiled with GCC under Linux #include #include #include // CRITICAL_SECTION cs; unsigned __stdcall ThreadFuncProducer(void *p) < // Thread function printf("Thread %d, Producer started.\n", GetCurrentThreadId()); while (nProduced < MAX_PRODUCTS) < EnterCriticalSection(&cs); if (nProducts < MAX_STORAGE) < nProducts++; nProduced++; Sleep(PRODUCE_TIME); > LeaveCriticalSection(&cs); Sleep(10); > printf("Thread %d, Producer finished.\n", GetCurrentThreadId()); return 0; > int main() < DWORD dwThreadid; HANDLE hThread; // setup InitializeCriticalSection(&cs); hThread = _beginthreadex(NULL, 1000, ThreadFuncProducer, (void*)0, 0, &dwThreadid); // wait until it's done while ( IsThreadRunning(hThread) ) < printf("nProducts: %2d, nProduced: %2d, nConsumed: %2d\n", nProducts, nProduced, nConsumed); Sleep(1000); > // clean up CloseHandle(hThread); DeleteCriticalSection(&cs); >
License Information
One license per developer. A site license can be used by an unlimited number of developers working for the same entity (e.g. company) within the same physical location (building or city limits).
Executables developed using the W2LPL library can be distributed royalty free. More .
Supported compilers
C, C++, Pascal, and any other compiler / language using Linux shared libraries (.so). Optionaly, a C/C++ static library can also be provided.
Supported Operating Systems
Linux 2.4 or newer and also to it compatible OS.
Q&A
Q: Will this library port GUI code blocks also ?
A: W2LPL supports a function set as listed in the manual. The GUI is a huge part and W2LPL does offer any support for this.
F: no #ifdef’s ?
A: The W2LPL library enables to compile a common source code in Windows and Linux. In order to enable this equivalent functions were implemented for Linux.
F: Speed ?
A: The W2LPL library is well designed and optimized. By using ready made and matured functions the application can gain stability and under circumstances some speed advantage. But the major profit is the short path required to port a Windows application to Linux.
Need any special function? Please don’t hesitate to ask. We are continuously updating this library.
Windows to Linux Porting Tools — Library to easily port a Windows application to Linux — Win32 to Linux library — Port Windows Application to Linux — Critical Sections — Event Handles — Thread — Migrating from Windows to Linux
Porting from Windows to Linux, part 1
Hi and welcome to a blog series about how to port graphics applications from Windows to Linux. The series will have three parts: Today, in the first part, we’ll be looking at prerequisites for porting. These are things you can do any time to facilitate porting later on, while still working on Windows exclusively. In the second part, the actual porting work will be done, and in the last part, I’ll talk a bit about the finishing touches, rough edges, and how to keep everything working. All of this is based on my experience with porting my research framework; which is a medium-sized project (~ 180 kLoC) that supports Linux, Windows and Mac OS X.
However, before we start, let’s assess the state of the project before the porting begins. For this series, I assume you have a Visual Studio based solution written in C++, with Direct3D being used for graphics. Your primary development environment is Visual Studio, and you haven’t developed for Linux before. You’re now at the point where you want to add Linux support to your application while keeping Windows intact – so we’re not talking about a rushed conversion from Windows to Linux, but of a new port of your application which will be maintained and supported alongside the Windows version.
Prerequisites
Let’s start by sorting out the obvious stuff: Your need a source control solution which will work on Linux. If your project is stored in TFS, now is the time to export everything to your favourite portable source control. If you are not sure what to choose, take Mercurial, which comes with a nice UI for all platforms.
Next, check all your dependencies. If you rely on WIC for image loading, you’ll have to find a portable solution first. In my experience, it’s usually easier to have the same code running on Windows and Linux later on than having a dedicated path for each OS. In my project, I wrapped the low-level libraries like libpng or libjpg directly instead of using a larger image library.
Now is also the time to write tests. You’ll need to be able to quickly verify that everything is working again. If you haven’t written any automated tests yet, this is the moment to start. You’ll mostly need functional tests, for instance, for disk I/O, so focus on those first. I say mostly functional tests, as unit tests tend to be OS agnostic. In my framework, unit tests cover low-level OS facilities like threads and memory allocators, while everything else, including graphics, is covered by functional tests.
For testing, I can highly recommend Google Test. It’s not designed for functional tests right away, but it’s very easy to write a wrapper around a Google Test enabled project for functional testing. My wrapper is written in Python and sets up a new folder for each functional test, executes each test in a new process and gathers all results.
Finally, if you have any build tools, make sure that those are portable now. I used to write them in C# when it was really new, but since a few years, I use only Python for build tools. Python code tends to be easy to maintain and it requires no build process whatsoever, making it ideally suited for build system infrastructure. Which brings us to the most important issue, the build system.
Build system
If you are using Visual Studio (or MSBuild from the command line), stop right now and start porting it to a portable build system. While in theory, MSBuild is portable to Linux using xbuild, in practice, you’ll still want to have a build system which is developed on all three platforms and used for large code bases. I have tried a bunch of them and finally settled with CMake. It uses an arcane scripting language, but it works, and it works reliably on Windows, Linux, and Mac OS X.
Porting from Visual Studio to CMake might seem like a huge effort at first, but it’ll make the transition to Linux much easier later on. The good thing about CMake is that it works perfectly on Windows and it produces Visual Studio project files, so your existing Windows developer experience remains the same. The only difference is that adding new source files now requires you to edit a text file instead of using the IDE directly, but that’s about it.
While writing your CMake files, here’s a few things you should double-check:
- Are your path names case-sensitive? Windows doesn’t care, but on Linux, your include directory won’t be found if you mess up paths.
- Are you setting compiler flags directly? Check if CMake already sets them for you before adding a huge list of compiler flags manually.
- Are your dependencies correctly set up? With Visual Studio, it’s possible to not define all dependencies correctly and still get a correct build; while other build tools will choke on it. Use the graph output of CMake to visualize the dependencies and double check both the build order, and the individual project dependencies.
With CMake, you should also take advantage of the “Find” mechanism for dependencies. On Linux, nearly all dependencies are available as system libraries, serviced by the package manager, so it definitely makes sense to link against the system version of a dependency if it is recent enough.
The end result of this step should be exactly the same binaries as before, but using CMake as the build system instead of storing the solutions directly in source control. Once this is done, we can start looking at the code.
Clean code
Did you ever #include system headers like in your code? Use system types like DWORD ? Now is the time to clean up and to isolate these things. You want to achieve two goals here:
- Remove system includes from headers as much as possible.
- Remove any Visual C++ specific code.
System headers should be only included in source files, if possible. If not, you should isolate the classes/functions and provide generic wrappers around them. For instance, if you have a class for handling files, you can either use the PIMPL idiom or just derive a Windows-specific class from it. The second solution is usually simpler if your file class is already derived from somewhere (a generic stream interface, for instance.) Even if not, we’re wrapping an extremely slow operating system function here (file reads will typically hit the disk), so the cost of a virtual function call won’t matter in practice.
To get rid of Visual C++ specific code, turn on all warnings and treat them as errors. There are a bunch of bogus warnings you can disable (I’ve blogged about them previously), but everything else should get fixed now. In particular, you don’t want any Visual C++ specific extensions enabled in headers. The reason why you want all warnings to be fixed is that on Linux, you’ll be getting hundreds of compile errors and warnings at first, and the less these are swamped by issues that are also present on Windows, the better.
While cleaning up, you should pay special attention to integer sizes. Windows uses 32-bit long s in 64-bit mode, Linux defaults to 64-bit long s. To avoid any confusion, I simply use 64-bit integers when it comes to memory sizes.
The better you clean up your code, the less work you’ll have to spend later during porting. The goal here should be to get everything to build on Windows, with platform specific files identified and isolated.
So much for today! Next week, we’ll look at how to get rid of Direct3D and how to start bringing up the code base on Linux. Stay tuned!