1

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.

Aman Aggarwal
  • 3,905
  • 4
  • 26
  • 38
user1167760
  • 33
  • 1
  • 2
  • 4
  • 6
    "*No, I don't want to use Boost.*" Lol, have fun. – ildjarn Feb 17 '12 at 22:25
  • 2
    *I don't want to rewrite the files in Visual Studio.* - so how are you going to port it? – BЈовић Feb 17 '12 at 22:26
  • In option (2), you still need to write the implementation of all the functions in the windows header files, right? So, there really isn't much of a difference from option (1). – Igor ostrovsky Feb 17 '12 at 22:26
  • 1
    Linux is not Windows. You need to find Linux versions of the Windows APIs that you're using. – SLaks Feb 17 '12 at 22:26
  • 1
    @Igorostrovsky: It looks like he doesn't understand what a header file does. – SLaks Feb 17 '12 at 22:27
  • @SLaks: Yes, I'm sure he knows that. In fact, *he specifically says that in the question*: "I've to find equivalent function in linux which I am not sure where to look at." And I don't see where he's misunderstood header files, either. They contain function prototypes. He wants to create a wrapper for the Linux platform APIs that makes them similar to those found in the Windows headers to make porting his app easier. Akin to a `Windows.h`, he wants to find (or create) a `Linux.h` header. – Cody Gray - on strike Feb 17 '12 at 22:30
  • @CodyGray But then what's the difference between his options (1) and (2)? Also, Linux.h will be the easy part... is he also going to write Linux.cpp that reimplements all of Windows APIs on top of Linux? – Igor ostrovsky Feb 17 '12 at 22:39
  • Thanks @CodyGray. I appreciate your comment. As for SLaks, I very well understand what a header file is. But anyways, thanks for the comment ! – user1167760 Feb 17 '12 at 22:44
  • For everyone who commented above: I'm sorry if I haven't written to the point. What i meant was: (1) Do I need to find linux api calls ? I don't have much knowledge of c++ programming in linux, so this was what I wanted to ask. (2) Should I simply copy the windows function ( eg. copying the filetime struct and including it in my file) ? – user1167760 Feb 17 '12 at 22:49
  • Which Windows APIs specifically do you use? Re-implementing each of them on Linux might be simple (if you've only used one easy one) or it might be a nearly impossible task (well, Wine is well on their way, but they've been doing it for ages -- and make no promises). – sarnold Feb 17 '12 at 23:32

3 Answers3

6

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.

Igor ostrovsky
  • 7,282
  • 2
  • 29
  • 28
4

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.

2

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.

Community
  • 1
  • 1
Eric Sites
  • 1,494
  • 10
  • 16