C Programming – Writing Multiple Flavors of C/C++ Code for Each Platform

ccompilernative

I have designed what I think is a useful, reusable functionality that I'd like to:

  1. Implement in C/C++ as an open source library; and then
  2. Write different "native binding libraries" for it in various higher-level languages, such that end users writing applications in these languages (Java, Ruby, Python, C#, Haskell, etc.) can all call the same C/C++ code under the hood, but drive that code from inside these libraries.

For example, I might have the following C code:

// Pseudo-code for simple example only, don't read into this too much!
float square(float x) {
    float p;
    p = x * x;

    return p;
}

And then write a "Java binding library" that includes the compiled C code as a native library inside of the JAR, and that exposes a Java API for invoking it:

// Pseudo-code for simple example only, don't read into this too much!
public class SquareManager {
    public SquareManager() {
        super();

        System.loadLibrary("Square");
    }

    public native float square(float x);

    public float calcSquare(float x) {
        return square(x);
    }
}

Ditto for other high-level languages (again, Ruby, Python, etc.).

When I hear people talking about writing C/C++ code, I often hear them talk about targeting various platforms. By this, I assume they mean that, with C/C++, you have to compile the code into binaries that can run on various OS/instruction set combos. For instance, you might have one binary distribution for running on Windows/x86. You might have another one for running on Linux/x86. You might also have a distribution for running on Linux/ARM. So to begin with, if the above statement is inaccurate, please begin by correcting me!

Assuming I'm more or less correct there, then it seems to me that I should be able to:

  • Just write this C/C++ code once; and then
  • Just make sure, for each platform (OS/instruction set combo) that I want to target, that I have a compiler running on my machine that can compile that C/C++ code into a binary that can run natively on the targeted platform

This would be opposed to what I'm concerned about, which would be a situation where:

  • For some reason, I need to write a different version/flavor of the C/C++ code for each targeted platform; and then
  • Compile each version of the source code into a binary that can run on the intended platform

So I ask: Am I correct here, thinking that I can write the C/C++ code one time, and then simply compile it (probably using different compilers, or different compiler configs) multiple times, one time for each targeted platform I want to support? And if I'm incorrect or misled, here, then how?

Best Answer

If you are careful about writing platform-neutral code, you might be able to write one C++ version and just compile it several times. I would not take any bets that this would be 100% successful.

What is more common is that 90% of the code is platform neutral and the rest ends up in sections that are controlled by conditional compilation directives, set up to include or exclude different versions of source code depending on the platform. Sometimes these sections are very short.

Why would they be needed? Windows APIs don't exist in Linux and Linux APIs don't exist in Windows, for example. There are differences in the way the O/Ss schedule threads and manage memory, so there may be subtle differences in the way you manage your locks or your handles. If you have any union structures, they may need to be re-arranged depending on endianness. Etc.

I have seen product teams eventually decide to split their code bases just because there were so many exceptions. And I have seen other teams try to merge them again. I don't think there is any perfect solution. But there is no harm in starting out your effort with a single code base, just to see if it is possible.

Related Topic