5

How to create a shared library (.so) file in Mac os X using terminal

Thanks in Advance.

4
  • Create it from what? And why can't you use Xcode? Jul 1, 2015 at 11:52
  • 1
    I have source file like .cpp file i need to create .o file by compiling that .cpp file and based on that .o file i want to create .so file. how to create .o file ? Jul 1, 2015 at 12:18
  • 1
    Is that shared library is .so file or .dylib on mac OSX ? which one i should use, can you please anyone guide me. Jul 1, 2015 at 12:21
  • 1
    possible duplicate of Creating shared libraries in C++ for OSX Jul 1, 2015 at 12:57

1 Answer 1

1

Assuming that you are in the directory with your .cpp/.c and .h files, in the terminal you must type:

  1. g++ -c file1.cpp file2.cpp (add as many cpp/c files you want in the so file)
  2. g++ *.o -dynamiclib -o lib_name.so

Then if you are using a cpp file that will run it:

  1. g++ -c test.cpp -o test.o (compiling the program to see if the shared library works)
  2. g++ -o test test.o -L. -l_name (create an executable to run)
  3. ./test (run the test)

For C code so files, it's the same but replace g++ with gcc.

1
  • Just to be sure, you have tested this procedure on MacOS, right? (I'm asking because OP was explicit about needing a MacOS answer, not a generic *nix answer.)
    – joanis
    Jun 25, 2022 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.