Coding for multiple platforms

J

Joe C

Just learning C++...
I've recently made a leap and compiled some code I wrote with Windows in
mind for Linux. It wen't smoothly (other than having to learn about a new
compiler/os!!), However, I did have to make some changes to the code. For
example, the code needs to be aware of how filenames/paths are defined so
that it can parse filespecs and come up with new names. In the first
iteration, I just swapped out the symbols. However, As I'd prefer not to do
such work more than once, I decided to use a header ("version.h") that
contains some constants such as endianness, operating_system. THen I
extern-link to these variables and have pieces of code in the various
translation units that activate, depending on the contents of version.h.

My question...is this a fairly standard way to deal with platform-specific
issues in "portable" code, or is there an easier way? So far, I'm not using
the preprocessor to deal with this stuff.

An brief example follows...

//--version.h--
const int operating_system(0); // (0 = Win, 1 = Linux);

//--parser--
#include"version.h"
extern const int operating_system;

void parseit(){

string temp = fs;
char path_seperator('\\'); // default values...Windows
char extension_indicator('.'); // default values...Windows

extern const int operating_system;
switch(operating_system){
case 0: // Windows
path_seperator = '\\';
extension_indicator = '.';
break;
case 1: // Unix
path_seperator = '/';
extension_indicator = '.';
break;
}
// do stuff
}


Thanks, Joe
 
R

Ron Natalie

Joe C said:
My question...is this a fairly standard way to deal with platform-specific
issues in "portable" code, or is there an easier way? So far, I'm not using
the preprocessor to deal with this stuff.

Actually, you usually know the UNIX versus Windows at compile time, so I'd do
something more along these lines:

#if WINDOWS
const char path_separator = '\\';
#else //UNIX
const char path_separator = '/';
#endif

and put it some common include files
 
C

cK-Gunslinger

Joe said:
Just learning C++...
I've recently made a leap and compiled some code I wrote with Windows in
mind for Linux. It wen't smoothly (other than having to learn about a new
compiler/os!!), However, I did have to make some changes to the code. For
example, the code needs to be aware of how filenames/paths are defined so
that it can parse filespecs and come up with new names. In the first
iteration, I just swapped out the symbols. However, As I'd prefer not to do
such work more than once, I decided to use a header ("version.h") that
contains some constants such as endianness, operating_system. THen I
extern-link to these variables and have pieces of code in the various
translation units that activate, depending on the contents of version.h.

My question...is this a fairly standard way to deal with platform-specific
issues in "portable" code, or is there an easier way? So far, I'm not using
the preprocessor to deal with this stuff.

An brief example follows...

//--version.h--
const int operating_system(0); // (0 = Win, 1 = Linux);

//--parser--
#include"version.h"
extern const int operating_system;

void parseit(){

string temp = fs;
char path_seperator('\\'); // default values...Windows
char extension_indicator('.'); // default values...Windows

extern const int operating_system;
switch(operating_system){
case 0: // Windows
path_seperator = '\\';
extension_indicator = '.';
break;
case 1: // Unix
path_seperator = '/';
extension_indicator = '.';
break;
}
// do stuff
}


Thanks, Joe

Usually, you know your platform at compile time, so you could just
include a common header file that contained platform-specific macros.
 
S

Samuele Armondi

Joe C said:
Just learning C++...
I've recently made a leap and compiled some code I wrote with Windows in
mind for Linux. It wen't smoothly (other than having to learn about a new
compiler/os!!), However, I did have to make some changes to the code. For
example, the code needs to be aware of how filenames/paths are defined so
that it can parse filespecs and come up with new names. In the first
iteration, I just swapped out the symbols. However, As I'd prefer not to do
such work more than once, I decided to use a header ("version.h") that
contains some constants such as endianness, operating_system. THen I
extern-link to these variables and have pieces of code in the various
translation units that activate, depending on the contents of version.h.

My question...is this a fairly standard way to deal with platform-specific
issues in "portable" code, or is there an easier way? So far, I'm not using
the preprocessor to deal with this stuff.

An brief example follows...

//--version.h--
const int operating_system(0); // (0 = Win, 1 = Linux);

//--parser--
#include"version.h"
extern const int operating_system;

void parseit(){

string temp = fs;
char path_seperator('\\'); // default values...Windows
char extension_indicator('.'); // default values...Windows

extern const int operating_system;
switch(operating_system){
case 0: // Windows
path_seperator = '\\';
extension_indicator = '.';
break;
case 1: // Unix
path_seperator = '/';
extension_indicator = '.';
break;
}
// do stuff
}


Thanks, Joe
Why not just use / throughout? AFAIK, windows accepts it as well.
S. Armondi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top