I have designed what I think is a useful, reusable functionality that I'd like to:
- Implement in C/C++ as an open source library; and then
- 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?