Linux-windows cross C ++ application

I am developing an application that should run on Linux and Windows. I have an object called obj that I want to use in code, and it has different behavior on Linux and Windows. so I inherit aaa and call the WindowsObj object for Windows and LinuxObj for the Linux object.

My question is: how to use this object in code? What should I write that it will work for both Linux and Windows?

For swiching types, I use typedef as:

typedef uint32_t DWORD; 

but what do i need to use for objects? I want to write this code:

 tr1::shared_ptr<WindowsObj> windowsobj (new WindowsObj(parameter)); tr1::shared_ptr<LinuxObj> linuxobj (new LinuxObj(parameter)); 

Any idea?

+4
source share
5 answers

Same:)

 class _object { }; class WindowsObject : public _object { }; class LinuxObject public _object { }; #if defined(WIN32) typedef WindowsObject Object; #else typedef LinuxObject Object; #endif Object myObject; 

EDIT: Naturally, the interface that WindowsObject and LinuxObject expose must be the same. In this example, _object will be the abstract base class that defined the interface, and LinuxObject and WindowsObject then implement this interface, hiding platform-specific things in their implementation files.

Example

_object.h

 class _object { public: virtual void doSomething() = 0; }; // eo class _object 

WindowsObject.h

 #include "_object.h" class WindowsObject : public _object { public: virtual void doSomething(); }; // eo class WindowsObject 

WindowsObject.cpp

 #if defined(WIN32) #include <windows.h> void WindowsObject::doSomething() { // do something totally reliant on windows here }; // eo doSomething #endif 

Then you will do the same for LinuxObject.h and LinuxObject.cpp , with the latter having completely different preprocessor instructions. for example, #if defined(UNIX) or some such flavor. Pay attention to WIN32 safeguards around the implementation. Then you will have the main header file that you use:

 #if defined(WIN32) #include "WindowsObject.h" typedef WindowsObject Object; #else #include "LinuxObject.h" typedef LinuxObject Object; #endif 

Now in your program

 Object a; a.doSomething(); 

It is worth noting that if this is just an odd line of code that differs in your complex object (for example, initialization call during initialization, destruction), you might be better off with one object-agnostic object and put the defenders in the implementation.This solution makes more sense. when there are huge differences.

+3
source

I usually use conditional compilation for this, for example.

 #ifdef WIN32 // windows-specific code #else // non-windows #endif 
+3
source

I suggest using a platform with a cross platform like GTK + or wxWidgets, so you don't have to reinvent the wheel ...

Then you should have the least possible code, depending on the platform, and possibly deep inside your classes. There you can use #ifdef to conditionally include code for Windows or for Unix.

+1
source

You can use the same header file with two different implementations in two different .cpp files; The PIMPL IDOM makes this possible even if two implementations require completely different data members. You simply compile and link one source when compiling for Windows and the other when compiling for Linux.

One of the areas that I thought was useful is email interfaces. Two operating systems have completely different ways of sending letters.

+1
source

Hi, if you can use C ++, use boost and size_t is your best way.

-1
source

All Articles