1 Reply
      Latest reply on Feb 26, 2019 6:40 PM by Ken Thomases
      a.bogong Level 1 Level 1 (0 points)

        Hello!

         

        How to create Shared Object file (*.so) from Objective-C and make it available from C or C++? I need to implement some functionality from Objective-C classes into C++ application? Is there any way to do it?

        • Re: Shared Object file from Objective-C. How?
          Ken Thomases Level 4 Level 4 (745 points)

          On macOS, what you call a shared object file is call a dynamic library, with a .dylib extension. You can create one using the "-dynamiclib" option to the compiler (if the compiler is driving the link step):

          cc -dynamiclib -o foo.dylib foo.m

           

          If you're invoking the linker (ld) directly, you can use the "-dylib" option:

          ld -dylib -o foo.dylib foo.o

           

          However, you don't necessarily need a dynamic library. You can just compile and link an Objective-C source file to your other files:

          cc -o your_program your_c_file.c your_c++_file.cpp your_objective_c_file.m

           

          Objective-C is an extension of C, so all C code is also Objective-C code with the same semantics.  Likewise, Objective-C++ is an extension of C++.  So, you can make the interface between your C/C++ code be a typical C/C++ interface (functions, variables, C++ classes, etc.) and use Objective-C(++) in the implementation.