Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm looking for the best solution to write cross-platform software, aka code that I write and that I have to interface with different libraries and platforms each time.

What I consider the easiest part, correct me if I'm wrong, is the definition of new types, all I have to do is to write an hpp file with a list of typedefs, I can keep the same names for each new type across the different platforms so my codebase can be shared without any problem. typedefs also helps me to redefine a better scope for my types in my code.

I will probably end up having something like this:

include
|-windows
| |-types.hpp
|-linux
| |-types.hpp
|-mac
  |-types.hpp

For the interfaces I'm thinking about the same solution used for the types, a series of hpp files, probably I will write all the interfaces only once since they rely on the types and all "cross-platform portability" is ensured by the work done on the types.

include
|
|-interfaces.hpp
|
|-windows
| |-types.hpp
|-linux
| |-types.hpp
|-mac
| |-types.hpp

For classes and methods I do not have a real answer, I would like to avoid 2 things:

  • the explicit use of pointers
  • the use of templates

I want to avoid the use of the pointers because they can make the code less readable for someone and I want to avoid templates just because if I write them, I can't separate the interface from the definition.

What is the best option to hide the use of the pointers?

I would also like some words about macros and how to implement some OS-specifics calls and definitions.

share|improve this question
1  
You're asking quite a few different questions here. Also, I think you're vastly oversimplifying the challenges associated with writing useful cross-platform code. Can you edit to narrow the scope of the question to something that someone could reasonably answer in a few paragraphs? –  Caleb Jun 29 '12 at 20:30
2  
There are many resources already available on this topic. What specifically can we help you with? –  Robert Harvey Jun 29 '12 at 20:30
    
@RobertHarvey i can't use google, every time i search for something on the topic i end up with a list of links for writing things in QT, in GTK, or in others libraries. I want to focus on the generic rules about the style and the efficiency of the code. –  user827992 Jun 29 '12 at 20:36
    
@Caleb my biggest problem right now is the last part about methods and classes, if you find difficult to answer the entire question this is the part in which i'm interested the most. –  user827992 Jun 29 '12 at 20:38
1  
@user827992 Take a look at what the QT and GTK dudes have done. Check out their API, see if you can draw some inspiration from their work. The products have usage history, their designers have undoubtedly tried things out, ran into problems, and figured out some good solutions. You should be able to learn quite a few things about cross-platform solutions just by learning about their API. –  dasblinkenlight Jun 29 '12 at 20:40
show 2 more comments

3 Answers

The easiest way to think about cross-platform programming is that you need a strategy for dealing with changes in the behaviour of the program's environment, across space (i.e. different hardware architectures and operating systems) and time (i.e. different versions of an operating system with new features and different bugs). There already is a pattern for implementing strategies.

You can imagine defining a polymorphic type with member functions that define the "logic" of what you're trying to do. These member functions call abstract member functions that are responsible for entertaining the underlying platform's needs. Clients call a factory method to get an instance of your class: the factory method chooses what concrete implementation to return based on the details of the platform.

This approach results in a public interface with no templates and (with the information I know about the problem) no explicit pointer use. Clients just call a factory method and then use the public interface on your virtual base class. It also completely hides all of the complexity you want to introduce regarding headers and their locations - your clients just include the headers for your virtual types.

share|improve this answer
add comment

The best example of coding for multiple platforms that I know of is the "Apache Portable Runtime"

This is an excellent C library that was initially written to move all platform OS dependent code out if the main fork of the Apache web server code.

It may or may not be suitable for your use (either because of licensing restrictions or you really need C++ code). Either way it is an excellent example of how to go about insulating your code form platform dependencies, and, there are some great examples on dealing with platform weirdness.

share|improve this answer
add comment

How long have you been programming, and what size of applications?

This question is not the "who do you think you are?" kind, but points to the fundamental problem of all frameworks. Be it a simple stream parser module, a GUI manager or "the overall platform independent frame", they must restrict direct coding, and as a replacement, provide a service that is equivalent or better for "any" task that might appear. You must have seen many problems before starting any framework.

I have spent 20 years in programming, I have searched for the core concepts of this "ultimate framework" all the time, and realized that I have always been going "in the wrong direction on a highway". I think I have some answers to consider, the ones that apply here are:

  • the question is wrong. Platform independence means language independence too: there are platforms where C++ is not available or not the best answer;
  • so don't waste your time on implementing language tricks for C++, they somehow limit the services of all platforms. Get used to the idea that you will generate those header files from an external (therefore truly platform independent, like JSON) type declaration ;-)

Following the comments and downvotes, I decided to add the following:

  • When mentioning the years, I wanted to express the importance of learning from mistakes. You might think that your first ideas are perfect, and the codes bug free. My codes always have bugs, although I do my best to avoid them - and my concepts were formed during creating different frameworks under "IRL" applications. They were quite weak for the first times, and I had to throw several versions and learn a lot from those experiences. This is only a warning, and a reason why my findings should be considered (and not accepted just because of that).
  • I did not want to do any "self marketing" here, I thought that anyone interested enough to judge my words would check my profile as well. If not: here you can see what I call platform independent programming, and for the impatient, a short summary might explain why I have spent almost 20 years on it.

Cheerio!

share|improve this answer
2  
-1: While the "experience is needed for good framework design" point is true, the proposed language abstraction is a road straight into maintenance hell. At the end of the day, you'll have an underspecified meta language which is processed into intermediate languages not understood by all developers. If you develop cross-platform, use a language supported on all platforms. If there is none, you will have to develop one, one way or the other. That will be costly, so better reconsider the whole project. –  thiton Jul 16 '12 at 7:30
    
@thiton The meta language is correct (yet I think the type/attribute/message structure does not require too much specification), but the "intermediate" is wrong: the tool should produce standard C / C++ / Objective C headers that developers understand and use. I would not throw away Objective C property features to become linux-compatible :-) I don't invent the wheel here, UML tools do the same; I have not recommended them because I think they are not answer to the question. "Developing a language" leads too far, like to the separation of the language and the runtime, out of scope here. –  Lorand Kedves Jul 16 '12 at 10:48
    
... I just write this because I have walked the same path, I have hated the idea of generating sources until I have finally adapted it - due to reasons (and with proof of concept results) that I should not detail here if I want to keep my reputation above zero :-D –  Lorand Kedves Jul 16 '12 at 10:58
1  
I think part of the reason you are being downvoted for is that it's totally irrelevant how much experience you have. IRL it might add some weight to your words (although I'm always skeptical when someone uses that as an argument), but not here, especially with such an unusual idea. If you think your idea has merit, please, do add details and reasons. "I walked the same path" is not proper reasoning because it assumes that you are correct (the very thing you want to prove with your reasoning). –  Tamás Szelei Jul 16 '12 at 11:07
1  
I think you misunderstood me. Your reasoning sounds like: "I think this and that because I have 20 years of experience" and not "I think this and that because foo and bar. I'm not commenting on whether or not you're right, only that using your amount of experience as an argument is irrelevant. It's only relevant when clashing arguments, but even then it's not a strong one. The core of your answer is that platform independence should come with language independence. Why exactly? You are not telling, only that you have a lot experience and worked on this for a long time. –  Tamás Szelei Jul 16 '12 at 12:04
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.