3

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.

Rahul
  • 95
  • 1
  • 2
  • 9
  • 1
    link it statically, so any external libraries necessary become part of the binary. – Marc B Mar 12 '14 at 20:24
  • You have to make sure the libraries are on the other computer - from your question I would say that is the issue not the OSX version – mmmmmm Mar 12 '14 at 20:25
  • @Marc B: Would you be able to give me an example of how I might do that (link statically that is)? Thanks – Rahul Mar 12 '14 at 20:25
  • gcc -static to create a static binary. It might not work, sometimes you don't actually get the static versions of libraries. – user3303729 Mar 12 '14 at 20:28

1 Answers1

1

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>
Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153
  • Could you please point me to a link that indicates how I might be able to include a copy of the libraries in my final application? My goal is to create an executable (or multiple executables if need be) on my computer that are ready to run on other computers that may be running an older version of a Mac OS and don't have libfftw3 and libaiff (the two other libraries I'm using) installed. Thanks. – Rahul Mar 13 '14 at 20:46
  • How are you building your application? Is it strictly from the command line, or with a makefile (if so, GNU make, AutoMake, CMake), or in an IDE (Eclipse, XCode, etc)? – Cloud Mar 13 '14 at 22:25
  • Please view this alternate question I have posted. I have the answer you are looking for there. http://stackoverflow.com/questions/22391848/compiling-a-c-program-for-previous-os-x-version – Rahul Mar 14 '14 at 05:39
  • I appreciate your response. I will give this a try. Thanks! – Rahul Mar 17 '14 at 10:27