How can I make my C++ code cross platform capable?
How can I make my C++ code cross platform capable? I need it to work开发者_如何学C on Windows and Xubuntu.
if I understand the question well ... You can use the C/C++ Preprocessor directives :
#if defined(WIN32) || defined(WIN64)
// windows code
#else
// linux and mac code ...
#endif
Update : OK, the question is clear now...
For cross platform c++ code, you can use Qt. It's a powerful c++ framework and it's cross-platform and free.
cygwin can help you to get your linux/unix code working on Windows.
Generally any reasonably complicated program needs to be designed as Cross-Platform from the start. Retro-fitting an existing program to be cross-platform is near-on-impossible.
Without more details, its hard to say more.
Questions that would help fill in the details are: Where is your program NOT cross-platform? In the User-Interface? In specific libraries? In specific OS calls?
Do you have access to the entire program's source-code? or do you rely on external static libraries and/or DLLs?
You either need to use a cross-platform development library, such as Boost or Apache Portable Runtime OR you need to know both platforms' APIs (POSIX & WinApi) well enough to choose a mostly mutually compatible sub-set.
One point of advice is to abstract out functionality which uses os functions. That way, you can minimize the use of #define's and make the os-specific components easy to manage. (See: adapter pattern.)
Without the source code, you can't. You can cheat and emulate one OS on the other (using Virtualbox, or KVM/Xen) but you cannot run Windows binaries on Linux or vice versa.
One of the reasons why source code is so important....
If you project depends on other libraries - you need to find version of those libraries for the target operating system. Then change your build system to properly link those in.
The other solution would be to transition your project into Service Oriented Architecture (SOA) which has many advantages over monolithic applications.
Use preprocessor directives:
#ifdef WIN32 /* WINDOWS CODE */ #else /* LINUX CODE */ #endif
精彩评论