views:

233

answers:

8

How can I make my C++ code cross platform capable? I need it to work on Windows and Xubuntu.

A: 

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....

Dirk Eddelbuettel
the codes are written by me, so i have the source code. actually i'm a bit dummy at linux :( i read other comments over too. But my main code is on windows, I can not make it work on linux. And the linux code is for communication... I have to bind them.
Cmptrb
The OP got edited since it was first posted. The original question stated lack of source code.
Dirk Eddelbuettel
Well if you can't use linux right now you have two choices: learn linux, or redefine your scope and stay on windows.
Dirk Eddelbuettel
either, winsock.h is not good for network programming so i have done my job on linux. i have compilied my code. i have still a server code and a client code where clients can easily communicate over server. the problem is my desktop application [c++ code] is working on windows. i do not know how to work them together. i have less time, that's why i have asked it here. if i had time, to learn detailed will not take much time.
Cmptrb
A: 

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.

Zepplock
+4  A: 

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.

Matthieu
thanks, i'm looking it.
Cmptrb
+3  A: 

cygwin can help you to get your linux/unix code working on Windows.

Carl Norum
A: 

Use preprocessor directives:

#ifdef WIN32
/* WINDOWS CODE */
#else
/* LINUX CODE */
#endif
Aviral Dasgupta
+3  A: 

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?

abelenky
This is one of those ansers where i wish I had a +10 button...
Tim
@tim: thank you for the flattery. :)
abelenky
+1  A: 

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.

alex tingle
+1  A: 

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.)

Marcin