简体   繁体   中英

linux- windows cross c++ application

I'm developing an application which has to run on Linux and Windows. I have an object called obj which I want to use in the code and it has different behavior on Linux and Windows. so I inherit aaa and called WindowsObj for Windows object and LinuxObj for Linux object.

My question is: How to use this object in the code? what do I have to write that it will run both for Linux and Windows?

For swiching types I use typedef like:

typedef uint32_t       DWORD;

but what do I have 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?

The same thing :)

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 would be an abstract base-class that defined the interface, and LinuxObject and WindowsObject would then implement this interface, hiding away the platform-specific stuff in their implementation files.

Sample

_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 would do the same for LinuxObject.h and LinuxObject.cpp , the latter having completely different preprocessor instructions. eg, #if defined(UNIX) or some such flavor. Note the WIN32 guards around the implementation. Then you'd have some core header file you'd 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's worth noting that, if it's just the odd line of code that differs in your complex object (like a init call at initialisation, destruction) you might be better off with a single platform-agnostic Object and put guards in the implementation. This solution makes more sense when there are huge differences.

I tend to use conditional compilation for this, eg

#ifdef WIN32
// windows-specific code
#else
// non-windows
#endif

I suggest to use a cross platform framework, like GTK+ or wxWidgets, so that you hadn't to reinvent the wheel...

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

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

One area that I've found this useful is for email interfaces. The two OS have completely different methods for sending emails.

嗨,如果您可以使用c ++,请使用boost和size_t类型更好的方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM