问题
I'm working on a c++ project where I have bunch of Visual Studio generated project files that I want to port to linux. I essentially am using windows.h header file in multiple files on Windows. Now, I'm unsure as there explicitly exists no linux.h file (incase it does, please guide me where to look at). On linux I'm using Eclipse CDT for development. I've two ideas in mind of how possibly it would work on linux but I want your input to know what the right direction is:
(1) To remove the windows API calls with Linux API calls in the C++ files. But this would mean, I've to find equivalent function in linux which I am not sure where to look at. eg. Filetime in Win32 is equivalent to something in linux (haven't found this thing yet).
(2) I copy the basic syntax of these functions (as written in windows.h) and just create a header file (lets say i name it linux.h) and include this header file in project on linux.
So, apparently you might have figured out that I'm confused of how to move things ahead. I just want to work this thing out. Please suggest me ideas/views other than following: (1) No, I don't want to use Boost. (2) I don't want to rewrite the files in Visual Studio.
回答1:
It really depends on your application. If its a non-gui app command line only. You should look at replacing your win32 calls with posix versions of the functions which are compatible with different platforms (linux, Mac OS X).
So that is option 1. You can convert Filetime to posix here and wrap it with your own function that has an #ifdef for each os you want to compile on.
Options 2 is a pain, been there done that. But it can still be an option, it really depends on how many Win32 functions you have in your source code and how different they are from the posix version. I do not recommend this option.
Option 1 is better once you learn the posix versions of the win32 functions and just stop using the win32 function as much as possible.
回答2:
Linux implements the POSIX API. If you follow path (1), you'll need to find the replacement headers and calls from here.
Alternatively, you could use Winelib to try to compile your Windows code "natively" on Linux. The Wine Project implements much of the Windows API, so it could work, and it's theoretically possible to recompile your Windows program to run on Linux for ARM, for instance, but they make no guarantees about that.
However, if you want to follow that second path, you'll need to recompile for each version of Wine that your users might have installed, so if they're on x86, it makes more sense to just give them the binary along with a custom wine prefix configured to a state known to work.
A third alternative is to drop all of that, and rewrite using a toolkit like Qt that will cross-compile across Windows/Mac/Linux, and drop direct calls to the underlying operating system. If you find your code often needing to work across different operating systems, this is probably the best choice.
回答3:
It sounds like you want the Windows APIs on Linux. I would think that you should also consider making your C++ code portable by replacing the Windows API calls with portable libraries like STL and Boost.
But, if you really can't do that, then look into WineHQ, which is an implementation of Windows APIs on Linux.
来源:https://stackoverflow.com/questions/9336282/porting-win32-code-windows-h-to-linux