Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Portability of C++ Compared to C


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 17 January 2012 - 11:43 AM

In general, how portable is C++ compared to C? For example, if a new device gets released (think back to the days when the iphone or droids were first released) is it just as likely to have C++ support as it is C support? Do embedded systems usually support C++ if they support C? I know C has compiler support for most devices under the sun, but I hear less about C++'s support, which is why I'm asking.

Thanks!
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9662

Like
4Likes
Like

Posted 17 January 2012 - 11:53 AM

Nowadays C++ is more or less just as portable as C in the sense that pretty much every platform with a C compiler has a C++ compiler. However, not every C++ platform supports the full range of C++ features. Some may not have exceptions or have such bad exceptions that they might as well not have exception support, or may have bad template support, etc. That's not even getting into C++11. Then again, not every platform has full C99 support either.

#3 Dunge   Members   -  Reputation: 405

Like
-4Likes
Like

Posted 17 January 2012 - 12:22 PM

iPhone never supported C, it only support Obj-C (Apple way of beeing "different"). Android and almost every other embedded platform ever done is using a Unix base, and it can build using GCC. GCC is supporting C++ perfectly since years so it shouldn't be a problem, just be sure you don't have a OS older than 20+ years.
As SiCrane said however, supported features could be different on some platform. No really a problem for future products, but mostly for older.

#4 bronxbomber92   Members   -  Reputation: 275

Like
1Likes
Like

Posted 17 January 2012 - 12:30 PM

iPhone never supported C, it only support Obj-C (Apple way of beeing "different").

That's wrong. Xcode and the iOS SDKs sport both GCC and Clang. You're free to use C, C++ or Objective-C. Objective-C is just the language Apple choose to implement various frameworks they provide (UIKit, Foundation, GLKit, etc.). Yes, you'll most likely have to use Objective-C somewhere in your codebase, but you can minimize that to only where you need it and use C for the rest of your program if that's what you want.

#5 Antheus   Members   -  Reputation: 2397

Like
-2Likes
Like

Posted 17 January 2012 - 01:41 PM

This question in such general form doesn't make much sense.

C or C++ source code is compiled into something else. It's then linked. Finally, it's executed.

GCC family of compilers, as well as Intel, clang, even MVS all take care of first part. They emit generated native code. Depending on compiler, this code may be limited in what it targets. x86/64 support is fairly decent to a degree, so are common ARM architectures. But having a blob of assembly isn't all that useful.

We then need to link this code with OS or include rudimentary OS replacement (majority of systems do not have an OS). Code that does this using standard library will be portable to systems which provide compatible standard library. For OS-specific functionality, we need platform libraries to link.

Finally, it comes to running it. Run-time platform may very well be, and often is different from development platform. Running such code may not be as straightforward as simply navigating the file system and running an executable.


C or C++ code do not have notion of portability since they don't define any of the above. Both syntaxes are only relevant to compiler that generates native (or even some intermediate) code. The linker and run-time environment simply aren't part of this equation.

From engineering perspective - a project is portable precisely to the platforms it has been ported to. Any other assumption is a simple fallacy. Only exception are certified runtimes, such as Java.

#6 Hodgman   Moderators   -  Reputation: 31786

Like
3Likes
Like

Posted 17 January 2012 - 05:42 PM

This question in such general form doesn't make much sense.

Yes it does, you just chose to interpret it differently so you could go off on an arrogant rant.

When a new platform comes out, you're usually stuck with the toolchain that's given to you in the official SDK for that platform. If you're a licensed developer for that platform, you may not even have a (legal) choice about using a 3rd party SDK vs the official SDK.
If the official SDK has a buggy C/C++ compiler, or a buggy Java VM, or is missing a C# compiler -- then those are your limitations when it comes to portability.

In an ideal world, every C++ compiler would follow the spec to the letter, and your C++ code would Just Work™ on any toolchain.
It may be that you've got to massage your code, or translate it to another language, or emulate some feature, in order to port your code to that platform.

#7 daviangel   Members   -  Reputation: 600

Like
0Likes
Like

Posted 17 January 2012 - 06:09 PM

Neither C nor C++ has a standard ABI last time I checked.The C++ language standard strictly defines semantics of all language constructs. However, it does not specify how these constructs must be implemented at binary level (in object code). As there is no common application binary interface (ABI) standard, C++ compiler vendors have come up their own ABI variations. As a result, problems can arise when object files produced by different compilers are linked together. These problems are usually caused either by the differences in memory layout of compiler-generated data structures, different calling conventions, or differences in name mangling. The main differences of an EABI with respect to an ABI for general purpose operating systems are that privileged instructions are allowed in application code, dynamic linking is not required (sometimes it is completely disallowed), and a more compact stack frame organization is used to save memory.
I remember Stroustroup for example talking about how recursion wasn't allowed on development of JSF.
Another example iPhone. Although garbage collection with Objective-C is fully supported on Mac OS X it isn't on iOS although they now have ARC I believe.

Edited by daviangel, 17 January 2012 - 06:25 PM.

Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

#8 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 17 January 2012 - 06:28 PM

This question in such general form doesn't make much sense.

Yes it does, you just chose to interpret it differently so you could go off on an arrogant rant.


Ok...

SQLite has a Java version. It's pure Java. SQLite is first compiled into ARM assembly, which is then converted into Java bytecode, which then runs inside JVM as Java class. SQLite has precisely zero knowledge of JVM.

C and C++ compilers that generate Intel or ARM assembly are widely available. This step is a non-issue, so they support just about any platform out there. In embedded development there is a whole set of different instruction sets which are technically called ARM, but they are incompatible with each other, yet none of that affects source code.

But in order for them to be of any use, it comes down to the APIs we use, from OS to third party libraries. Hence my claim that question doesn't make sense. C and C++ are not a problem, GCC will compile either of those into your chosen assembly.

But code that is written for POSIX will require POSIX run-time. Something using OGL will require OGL support.

When a new platform comes out, you're usually stuck with the toolchain that's given to you in the official SDK for that platform. If you're a licensed developer for that platform, you may not even have a (legal) choice about using a 3rd party SDK vs the official SDK.
If the official SDK has a buggy C/C++ compiler, or a buggy Java VM, or is missing a C# compiler -- then those are your limitations when it comes to portability.

If asking purely about out-of-box experience, then C or C++ is again a non-question. On Windows, portability is defined by Visual Studio. On other platforms by something else, perhaps a proprietary toolchain.

In an ideal world, every C++ compiler would follow the spec to the letter, and your C++ code would Just Work™ on any toolchain.

If my code uses DX, it's not portable, even if perfectly standard compliant.

A compiler does absolutely zero for portability. Compiler standard compliance tends to be the least important problem for portability.

Availability of compiler for given platform is just a pre-requisite and with cross-compilation and common targets widely supported not a big one. To do anything interesting, one needs to determine the OS functoinality application makes use of. Again, less of a problem these days as mainstream platforms gravitate towards POSIX. But any library ends up with either assembly or kernel calls. Those determine portability of an application.

#9 bronxbomber92   Members   -  Reputation: 275

Like
0Likes
Like

Posted 17 January 2012 - 08:36 PM

This question in such general form doesn't make much sense.

I think what the OP meant to ask was closer along the lines to: "Across different platforms, how standard compliant are the C++ compilers for that platform compared to the standard compliancy of the C compilers for that platform?" If this is indeed what the OP meant, I think SiCrane provided an adequate answer.

#10 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 18 January 2012 - 12:22 AM


This question in such general form doesn't make much sense.

I think what the OP meant to ask was closer along the lines to: "Across different platforms, how standard compliant are the C++ compilers for that platform compared to the standard compliancy of the C compilers for that platform?" If this is indeed what the OP meant, I think SiCrane provided an adequate answer.

That's pretty close to what I was asking (in addition to the question of "if there are C compilers for a system, are there usually C++ compilers too?"). It was about compiler support (i.e. real world language implementations) rather than language specifications (i.e. the ideal, albeit imaginary, implementation).

@Antheus: Of course C++ as a language doesn't dictate any kind of "portability"-ness. I was asking more about the practical, real life implementations of C++ and C -- whether they (particularly C++) exist and if so, how much do they tend to deviate from the standard so I know how much of my code will become invalid. I'm not talking about using particular 3rd party libraries; just straight C and C++, and if there is typically good support for taking this code and producing a runnable executable. You actually talk about my concerns/questions in your post. The compilation, linking, and run-time environments are the concerns, as they limit me in what I can do with a language (i.e. if the compiler doesn't support a language feature, or if the linker has some kind of restriction (maybe how names are decorated, for example) which is critical to the system (can't imagine why, but again, I don't know, which is why I'm asking), or if the runtime environment doesn't have support for the language's standard libraries, etc).

Anyway, thanks for the input everyone! Looks like I'll be writing my renderer in C++ and not C (it's for a school project, FYI, as I wouldn't normally try to reinvent the wheel).
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#11 Slavik81   Members   -  Reputation: 360

Like
0Likes
Like

Posted 18 January 2012 - 04:14 AM

Portability goes out the window very quickly, as you almost always have to work with a platform-specific API to implement anything useful.

Choosing what platforms you want to support should come first. From there, you can propose a strategy to meet those requirements.

#12 Tachikoma   Members   -  Reputation: 552

Like
0Likes
Like

Posted 18 January 2012 - 09:43 AM

Neither C nor C++ has a standard ABI last time I checked.The C++ language standard strictly defines semantics of all language constructs. However, it does not specify how these constructs must be implemented at binary level (in object code). As there is no common application binary interface (ABI) standard, C++ compiler vendors have come up their own ABI variations. As a result, problems can arise when object files produced by different compilers are linked together. These problems are usually caused either by the differences in memory layout of compiler-generated data structures, different calling conventions, or differences in name mangling. The main differences of an EABI with respect to an ABI for general purpose operating systems are that privileged instructions are allowed in application code, dynamic linking is not required (sometimes it is completely disallowed), and a more compact stack frame organization is used to save memory.
I remember Stroustroup for example talking about how recursion wasn't allowed on development of JSF.
Another example iPhone. Although garbage collection with Objective-C is fully supported on Mac OS X it isn't on iOS although they now have ARC I believe.

ABI conventions come into play when we are starting to look at generated code, as opposed to pure source code. ABI rarely affect your ability to write higher level code, so I'm not sure why anyone would be too concerned about it. The most impact it had in my experience is passing function pointers around and declaring functions with variadic argument lists. In those cases you are forced to disable any language/platform extensions for those particular functions, such as fastcall, etc.
Latest project: Sideways Racing on the iPad

#13 jonbonazza   Members   -  Reputation: 121

Like
0Likes
Like

Posted 19 January 2012 - 03:10 PM

Just to throw this out there, Android only recently received STL, RTTI, and Exception support. Many embedded systems don't include support for these 3 things off the bat, and many never get them at all.
Co-founder/Lead Programmer
Bonafide Software, L.L.C.
Fairmont, WV 26554 US

#14 LorenzoGatti   Crossbones+   -  Reputation: 2762

Like
0Likes
Like

Posted 20 January 2012 - 03:17 AM

Just to throw this out there, Android only recently received STL, RTTI, and Exception support.

You don't mean Android, you mean a C++ compiler for Android. Which one are you referring to?
Produci, consuma, crepa




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS