.o files in Linux and Mac OS X - linux

I'm not sure where I'm supposed to ask this, so excuse me if stack overflow isn't the best place.
My question is if there are any differences between a .o file generated on Linux using gcc and a .o file on Mac OS X. If so, what are the differences exactly and why couldn't I use one from Linux on Mac or vice-versa?

They are completely different. OS X uses the Mach-O object file format. Linux uses the ELF object file format. They perform the same general purpose but in completely different ways.

A file ending in .o is an object file. The compiler creates an object file for each source file, before linking them together, into the final executable. Mac OS is based on a BSD code base, while Linux is an independent development of a unix-like system. This means that these systems are similar, but not binary compatible. To get it working compile the code on the OS that you will be using.
Read more on binary code compatibility:
https://en.wikipedia.org/wiki/Binary_code_compatibility

Related

How to tell if current OS uses Linux-like or MacOSX-like shared libraries?

I am aware that there are (at least) two radically different kinds of shared-library files on Unix-type systems. One is the kind used on GNU/Linux systems and probably other systems as well (with the filename ending in ".so") and the other used in Mac OS X, and also possibly other systems as well (with the filename ending in ".dylib").
My question is this --- is there any type of test I could do from a shell-script that would easily detect which of these two paradigms the current OS uses for shared libraries?
I'm sure I could find some way to easily deal with this variance --- if only I knew of a simple test I could run from a shell-script that would tell me which type of shared library is used on the current system.
Well, I guess you need to check filetypes of executables on a target platform. You may use file for that (check its output for, say, /bin/ls ). ELF is a most widely used executable type on Linux, while Mach-O is "natively" used in MacOS X.
A note: technically there're other executable types on these systems, say a.out and PEF, and, you guess, those formats have their own dynamic libraries. Frankly speaking Linux has a pluggable support for executable formats and even Win32 .EXEs may be executed "quasi-natively" in Linux (of course, they need an implementation of Win32 API working above a given kernel API, WINE is a such implemetation).
Also if you need to create a dynamically loaded library, then you should use one of those portable build systems (to name a few: GNU autotools, CMake, QMake...). Thus you'll get not only ordinary DLL extension but also linker flags, portable methods of installation/uninstallation and so on...

Compiling a fortran program on linux and moving the executable to another linux machine

I have a code that I have written in Fortran during my PhD, and now I am collaborating with some researcher that uses Linux, and they need my model, that is basically a single executable file. In the future I will probably make it open source, but up to know they just want the executable, also because they are not programmers and they have never compiled a program in their life. So the question is: is it possible to compile it on my linux machine and then send it to them in order to use it in another linux machine?Or does the linux version and distribution matter?
thank you very much
A.
If you do not use many libraries you can do that. One option is statically linking the executable (-static or similar compiler option). You need to have the static versions of all needed libraries for that. The have .a suffix. They are often not installed by default in Linux distributions and often they are not supplied in the repositories at all.
In my distrbution (OpenSuSE) they are in packages like glibc-devel-static, lapack-devel-static and similar.
The other option would be to compile the executable on a compatible distribution the users will have (GLIBC version is important) and supply all .so dynamically linked libraries they will need with your executable.
All of this assumes you use the same platform, like i586 or amd64 or arm like wallyk comments. I mostly assumed you are on a PC. You can force most compilers to produce a 32-bit or 64-bit executable by -m32 or -m64 option. You need the right version of the development libraries for that.

MATLAB compiler for LINUX

I am looking to compile a .m file (program) from MATLAB to Linux. I have done it on Windows operating system using
mcc -mv FILENAME.m
I see on the MATLAB website that I can use GNU g++.
Does this work in a similar way to the MATLAB compiler by just writing one line of code in MATLAB or do I have to run it in the Linux terminal?
Also, does this compiler tend to have issues regarding getting the desired output?
What you want to do, is called crosscompiling. Here you want from a Windows computer cross compile a Matlab program to a native Linux executable. As of 2009, this was not possible and most likely isn't now either.
Perhaps you might try using Octave for Linux.
Download GNU Octave

Compiling a fat executable with gcc

This is not essential for my programs, but merely out of curiosity. Is it possible to, preferably using gcc, compile a 'fat' binary for Linux including multiple architectures such as combinations of amd64, i386, lpia and powerpc?
The ELF format for executables that linux uses does not support fat binaries, so there's currently not a reasonable way to do this.
There's an extension to ELF available at http://icculus.org/fatelf/ , to use it you need to patch various parts (linux kernel,binutils,glibc etc.) as these changes are not integrated in the mainline yet.
You don't need a fat executable in Linux. Instead, you can write a bash script that will choose and execute the correct binary.

sending a.out to someone [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is a Linux executable “compatible” with OS X?
I have some c++ code compiled using GLUT and OpenGL on os x.
It all works fine. I have it as a.out
Is there a way to send this to someone using os x? Will there be some weird dependencies?
Can I send it to someone using linux?
I doubt I can do either which is why I'm asking on here. I looked around on google but couldn't find anything useful, I'm probably not searching for the right stuff
thanks
OS X and Linux have completely different binary formats (Mach-O/Universal Binary and ELF respectively), so no, a program compiled on OS X won't run on Linux unless you cross-compiled it.
If you compiled your code using static linking, then another OS X user will be able to use it just fine. However, bearing in mind that I know very little about GLUT or OpenGL, I'd bet good money that those are dynamic libraries, and thus whoever you send it to will need to have the same libraries installed in the same place in order to be able to use your compiled code.
otool -L will display the linkage dependencies of your executable, FYI.
It should work to send it to someone using the same version of OS X and the same computer chip as you. It may even be possible on OS X to cross-compile to a universal binary that works on Intel and PowerPC chips, but you'll still be limited to OS X.
To get a working executable for Linux, you'll need to compile it on Linux (or use a cross-compiler, but that could be more difficult than it's worth, especially if you have to do it repeatedly).
Or, as has been said already, you could just distribute the source code and let people compile it themselves. This may be the best approach, because C++ has no common binary format, so if different people use different compilers that have different forms of name-mangling, they'll all still get a program that works (if they compiled OpenGL or GLUT with one C++ compiler and then you compiled your program with another, they might not work together (I think)).
What about just sharing the source?
Are you afraid somebody's going to steal your precious work?

Resources