Compiling C libraries on OS X

If you have compiled or built shared C libraries before you would have come across the following code:

  
     gcc -c -fPIC some_code.c -o some_code.o
gcc -shared -Wl,-soname,mylib.so.1 -o mylib.so.1.0.1 some_code.o

The above essentially compiles native C code into object files using gcc compiler and then linking it into a library called ‘mylib.so’.

If you issue the command above on a MAC, you will get the following errors:

   
      /usr/bin/ld: unknown flag: -soname
      collect2: ld returned 1 exit status
    

Within OS X, the second command will not work as OS X treats shared libraries as dynamic libraries. The second line needs to be changed to:

   
      gcc -Wall -D_FILE_OFFSET_BITS=64 -fPIC -arch ppc -arch i386 -arch x86_64 -dynamiclib -o mylib.dylib calc_mean.o
   

Then move the shared library mylib.dylib into ‘/usr/lib/’:

  
    sudo cp -v mylib.dylib /usr/lib/
  
Tagged with: