What are the practical differences in C and C++ code portability?

Asked

Viewed 736 times

6

I have been reading some open-source C code lately and have noticed that many of them become quite complex and with enough #ifdef in order to make the code as portable as possible between different platforms and *NIX. Some even support different libraries in the same program.

I would like to ask if in practice C++ can override this type of portability problem. An example of code to have the command mkdir portable:

// Try to bring in unistd.h if possible
#if !defined(__TURBOC__) && !defined(_MSC_VER)
#include <unistd.h>
#endif

// Bring in direct.h if we need to; sometimes mkdir/rmdir is defined here
#if defined(__WATCOMC__) || defined(_MSC_VER)
#include <direct.h>
#endif

// Fill in S_ISDIR
#if !defined(_POSIX_VERSION) && !defined(S_ISDIR)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif

#if defined(__TURBOC__) || defined(__WATCOMC__) || defined(__MINGW32__) || defined(_MSC_VER)
//
// Already have a single-argument mkdir()
//
#else
//
// Provide a single-argument mkdir()
//
#define mkdir(a) mkdir(a, S_IRWXU | S_IRWXG | S_IRWXO)
#endif

Code taken from common. h of the cmdpack package (apt-get install cmdpack on Debian-like systems)

In practice, can C++ be more portable? I understand that the standard C++ library should offer all the necessary resources (considering C++11) to program on any platform, and this type of code is not necessary. What is the real intention of the programmer in making this type of code and mainly, how C++ solves (or tries to solve) this type of situation?

  • And have you been reading C++ codes and not finding the same structure? Have you ever wondered if this is just a matter of style?

  • I actually read a few codes in C++, but I was quite intrigued by why they did this kind of #if since the early, unproporting versions of the code had none of that.

1 answer

6


Preprocessor

This is how you solve the different parts for each platform. I don’t know how well you know the languages, but this is part of the preprocessor.

These preprocessor directives help translate the code even before the program builds, so everything that is in one #if (which is not part of the program construction) of that platform is simply ignored even by the compiler. This translation is a simple form of code modification. Although it is part of the build process of the program and is done by the compiler it is not exactly the build process itself that deals with it. Hence there are even some criticisms that its use can bring problems since this translation is "dumb", does not understand much what you are doing. But it works well when the programmer knows how to use it correctly and there is no universally better solution.

This makes the build faster (has less code to compile effectively), and evidently the generated machine code gets smaller and faster, after all it does not need to decide at runtime whether it is relevant to that platform or not.

Probably the alternative would be to solve this at runtime somehow. It won’t look good for most programs. At least the program will be bigger without need, after all there are things in it that will never be used on that platform where it is running (remember that a program generated for one platform does not run on another). Depending on the technique used to select which code to execute can even get slower (theoretically, because no one will get to this point).

This goes for C and C++. C++ is essentially C with a few more features (I’ll leave out some irrelevant details for this question). These features can greatly decrease the need for preprocessor use but in this case selecting the platform specific code has nothing better.

Of course, you don’t know this, but your question doesn’t seem to make much sense. One of the reasons you might think this is that you don’t have much experience with both so understand:

  • Codes can be more or less portable.

    C++ has more features. Therefore, it has more chances of them being misused, making portability difficult. But this is the problem of code, of programmer, not of language.

  • Languages at most can have more or less facilities to separate codes for different platforms (not the case of C++ over C).

    In terms of having more features that help portability, even those that go beyond the pre-processor, if you have something else, it’s very little. It would have to make creative use of a few things. It will often bring more problems than benefits.

    It is possible to use templates, for example, but it will hardly bring any real advantage. I don’t see this being used in the codes I usually read (which is different than not existing).

  • Language implementations can meet more or less platforms.

    In practice it is often said that C++ is slightly less portable because there are no good implementations for all platforms. But there are controversies about it being an absolute truth.

  • Library is something different from language

    What surely C++ has the most by default, mainly C++11, C++14, C++17, etc., are libraries that already handle the different platforms for you. But it is not a language resource. Then you will see how these libraries were written and you will probably find a lot of #if there. That is, the advantage is that someone has already done for you.

    Of course C has libraries that deal with platform differences and facilitate the life of the programmer, but little is in the library that is considered as standard language.

Someone might come here and reply that it makes a lot of difference for portability to use C or C++. This can happen because I don’t know any little technique or because some people like to use a certain resource so much that they prefer it more than any other. There are people who have so much hatred to the use of the pre-processor that they may prefer all the difficulties of the template, for it will be easier (the best tool is the one you know and get along with).

Both have another problem that hinders portability between language versions or libraries or even implementations (compilers). Probably C++ suffers more from it, but I believe that’s not quite what you’re asking.

  • Perfect, it was exactly what I intended to ask. Feel free to edit the question if you feel it necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.