How to make a linux C++ app work on any linux system without need to recompile?

Red Squirrel

[H]F Junkie
Joined
Nov 29, 2009
Messages
9,211
One thing I've always hated in Linux is that you can't just copy a binary over to another machine and be able to run it, like in Windows. Sometimes I want to make a little utility or program that I just want to put on a USB stick and have it readily available without having to compile it. There HAS to be a way to do this. Anyone know how?
 
What, you think it's easy for Microsoft to give near-perfect binary backwards compatibility on every release and system? It's a pain in the ass, it involves a lot of time and tough design decisions, and it's something that Mac and Linux have decided isn't worth the effort. It's not magic, it's just a lot of appcompat testing and hard work to ensure libraries don't break old apps between versions. You're not going to get that on a Linux system.
 
What about programs like vmware? I have not checked, but they don't give all the source code with it, do they?
 
It's ridiculously hard to do this and the more stuff you link to the worse it gets.

At a minimum you want to link against oldest version of glibc you can. Cross compiling with different binutils will drive you nuts, I just keep a separate Debian Etch system for building apps w/glibc v2.3.6. It makes them portable enough for me.

Just realize every time you link to something, you're losing portability. What type of app are you talking about?

Vmware does need to ship some code to compile, pretty much anything that loads a kernel module needs to be build for that kernel. You can't have portable kernel modules with Linux, Solaris handles this.
 
No app in particular, just small utilities and stuff of that sort that I may code.

For example a backup management app. Rather then have to upload all the sources and stuff to the server, then having to install G++ and other requirements, I could just drop the binary and be done. But if it's that hard to do then guess it may not be worthwhile. I just figured maybe there was a flag in G++ I could add to tell it to just include everything needed to run the app in the binary.
 
I just figured maybe there was a flag in G++ I could add to tell it to just include everything needed to run the app in the binary.

You're looking for is called "static linking". Alternately, you can try to code against Perl/Python or write shell scripts.
 
One thing I've always hated in Linux is that you can't just copy a binary over to another machine and be able to run it, like in Windows.
You can do this. ELF has been fairly consistent over the last 10 years, and x86 even longer. You're going to run into issues with dependencies, but the same is true for Windows.

How many Windows apps can you just copy the .exe file and run? Most install a plethora of other files required for execution.
 
You can do this. ELF has been fairly consistent over the last 10 years, and x86 even longer. You're going to run into issues with dependencies, but the same is true for Windows.

How many Windows apps can you just copy the .exe file and run? Most install a plethora of other files required for execution.

Yeah lot of windows apps are coded in a way that makes it impossible to just transfer over. But I'm talking more about custom apps, where I have a bunch of cpp and h files and compile into a single binary. I try to avoid linking to 3rd party libraries for portability reasons as well.
 
How many Windows apps can you just copy the .exe file and run? Most install a plethora of other files required for execution.
Only the trivial ones, since they don't have any dependencies beyond what's built-in to the system. Even then, there are plenty of useful apps without installers. 7zip, CPU-Z, and Putty are the first three that I thought of.

If you're writing an application that does something interesting, it's going to depend on an API to help it. That API is a shared component, and its installation must be managed.
 
You're looking for is called "static linking". Alternately, you can try to code against Perl/Python or write shell scripts.
This. Pass -static to g++ and it'll disable dynamic linking. Your binary size will balloon, obviously, but you should be able to run it portably on most up-to-date architectures, especially if you're not doing anything too complex.
 
Back
Top