Question

I wrote a piece of C code on OS X 10.9.2 (Mavericks) and compiled it using the gcc compiler from terminal by linking the required libraries (like -lm -lfftw3 etc.). It works fine on my computer and another running the same OS (that also has the required libraries) but when I tried running the executable on another computer running OS X 10.8 (Mountain Lion), it doesn't work. I didn't try recompiling on the computer running OS X 10.8 (Mountain Lion) because it doesn't have the required libraries installed. How can I create a new executable using only terminal on my computer (running OS X 10.9.2 (Mavericks)) so that it works on any previous version of the OS that may never have the required libraries installed?

Thanks.

Was it helpful?

Solution

There are three approaches to this problem:

  • Statically link all dependencies into your binary.
    • This will include a copy of the libraries in your final application, making it bigger, but self-contained.
  • Create a dynamic build of the dependencies and deploy them on the target system.
    • libm (math library) exists on all systems typically, so it's like just libfftw3 and other libraries that need to be rebuilt.
    • This can be accomplished by either building the source, or installing the needed libraries using macports <www.macports.org>
  • Provide an alternate environment for the remote side to run the application in (ie: a Virtual Machine running Mavericks inside it).

Edit:

In response to your ocmment, you can force static linking of dependencies for your XCode target, in the case of libfftw3, by replacing the line:

-lfftw3

With:

-l/full/path/to/libfftw3.a

Repeat this for all of the libraries against which your target links, and you're set. The final application binary will likely be much greater in file size, but completely self contained.

References:


  1. Force static linking of library linked to Xcode target?, Accessed 2014-03-14, <https://stackoverflow.com/questions/458805/force-static-linking-of-library-linked-to-xcode-target>
  2. Compiling a C program for previous OS X Version, Accessed 2014-03-14, <https://stackoverflow.com/questions/22391848/compiling-a-c-program-for-previous-os-x-version>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top