views:

105

answers:

1

Hi, Although having a little experience, I am still confused with the question of whether it is necessary to install an application and/or build a C++ program or C++ library under Network File System shared by machines, which have different Linux version (e.g. CentOS release 4.7, Ubuntu 8.10), possibly different bit (e.g. 32-bit, 64-bit) and different versions of compiler (e.g. gcc/g++ 3.4.6 20060404 (Red Hat 3.4.6-10), gcc/g++ (Ubuntu 4.3.2-1ubuntu12) 4.3.2.), so that the executable or library files can be used under those different machines? What's the principles here?

For example, on a Network File System, I have an executable built from my C++ program under a machine with CentOS release 4.7, x86_64 ad gcc/g++ 3.4.6 20060404 (Red Hat 3.4.6-10). I surprisedly find that the executable can be used under another machine with Ubuntu 8.10, x86_64 and gcc/g++ (Ubuntu 4.3.2-1ubuntu12) 4.3.2. Also all the shared libraries I built on the first machine and used by my program do not complain error when the executable is running on the second machine. I am so worried if the running of the executable built on a different machine will give reliable results?

Another example, I remember a executable built on a 64-bit machine cannot run on a 32-bit machine. But how about the other way around, running on a 64-bit machine an executable built on 32-bit machine? How about C++ library files built and used across different bit machines?

If possible, could you point me to some reference, like some webpage, book,... that are related to my questions?

Thanks and regards!

A: 

You really need to test your program on each different configuration to be absolutely sure.

You can usually run 32 bit programs on 64 bit machines so you can probably get away with only a 32 bit version if you want.

You need to be a little careful about which version of gcc was used on each platform. Running a program built with gcc 4 on a machine whose OS and glibc were built with gcc 3 could cause you problems. So make sure that at least the major version numbers match.

Obviously, if you have different machine architectures, Sparc versus AMD64 for instance, you will need different versions. There are even some subtle differences between Intel and AMD so don't get over aggressive with the compiler optimizations and if you program uses SSE instructions (if you don't know what this is then you aren't using it), make sure the SSE set you use is supported by all the machines.

The key issues here are the machine architecture, the availability of the right libraries, and the "ABI" or Application Binary Interface. The ABI specifies how the OS should use machine registers and memory to pass information between different parts of the program, for example, how data is pushed onto the stack to pass arguments to a function. The ABI used by GCC has been known to change from time to time which is why using compatible versions is necessary.

I'm not aware of a book or web page that covers all these things. Start with the documentation for GCC and Binutils. And there is no substitute for good testing.

Steve K
Am I right that this compatibility issue is almost never guaranteed, the safe way is to build a version for each machine? For example, if I have a C++ library and two machines for the NFS, I will have to build it into ~/libraries/host1/mylibrary/ and ~/libraries/host2/mylibrary/. When my C++ program uses this library, how to write a Makefile that detects which the current machine is and assign the right path to the library for -L and -I ?
Tim
Well, to get the guarantee you have to do your homework and test.If you want to build for every machine, consider using GNU autoconf and automake. Autoconf was designed for this problem. You end up creating and using a configure script, just like most of the Linux source packages you download. If you have a lot of different machines to build or or you plan on distributing this program to others, this is he way to go. But, using autoconf is non-trivial if you've never done it before. If you just want to write the Makefiles manually, you can use shell commnands to get system info.
Steve K