473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Coding for multiple platforms

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
Jul 22 '05 #1
3 1402

"Joe C" <jk*****@bellsouth.net> wrote in message news:2G****************@bignews4.bellsouth.net...

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

Jul 22 '05 #2

"Joe C" <jk*****@bellsouth.net> wrote in message
news:2G****************@bignews4.bellsouth.net...
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
Jul 22 '05 #3
Joe C wrote:
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.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
5
by: Jeffrey Barish | last post by:
I have a small program that I would like to run on multiple platforms (at least linux and windows). My program calls helper programs that are different depending on the platform. I think I...
4
by: Mikkel christensen | last post by:
Hi there I wonder if any of you could point me in a direction where I can find some usefull information about coding standarts. I have some generel experiense in programming but I lack many...
7
by: rdh | last post by:
Hi all, I am in process of developing a Server in C++ supporting multiple protocols. The server will be exposing various functionalities, and the clients can communicate over any of the...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
7
by: Carlos Martinez | last post by:
Hi all: I don't know if this is the best newsgroup to ask for this question, but I don't find other. I want to know about C++ Coding Standards and tools for verifing the code. Thanks in...
0
by: Julian Snitow | last post by:
Here is a more visual example of the technique presented in Logan Koester's article, "Live Coding in Python" (http://www.logankoester.com/mt/2006/07/live_coding_with_python_1.html). It's very...
19
by: auratius | last post by:
http://www.auratius.co.za/CSharpCodingStandards.html Complete CSharp Coding Standards 1. Naming Conventions and Styles 2. Coding Practices 3. Project Settings and Project Structure 4....
4
by: gentsquash | last post by:
On some of my course pages, I quote (with attribution) small sections of Wikipedia and the like. E.g, the top of http://en.wiktionary.org/wiki/entropy has "entropia" in Greek font, ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.