“ld: unknown option: -soname” on OS X

后端 未结 3 2027
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 02:03

I try to build my app with CMake on Mac OS X, I get the following error:

Linking CXX shared library libsml.so
ld: unknown option: -soname
collect2: ld return         


        
3条回答
  •  温柔的废话
    2020-12-14 02:15

    You cleared you're problem, but I wanted to provide this a s a reference for future visitors because there's a bit more to creating a dynamic library on OS X. Also see Creating Dynamic Libraries in the Apple Developer pages.

    OS X does not use the convention libcoolstuff.so.X.Y.Z. OS X uses the convention libcoolstuff.X.dylib. The, to embed X.Y.Z into the library, use -install_name, -current_version and -compatibility_version.

    I don't know Cmake, but here's how it looks under Make. Your recipe to build the libcoolstuff 1.0.6 will look like:

    libcoolstuff libcoolstuff.dylib:
        $(CC) $(CFLAGS) -dynamiclib -install_name "libcoolstuff.1.dylib" \
        -current_version 1.0.6 -compatibility_version 1.0 -o libcoolstuff.1.dylib $(OBJS)
    

    And your make install rule would look like:

    PREFIX?=/usr/local
    LIBDIR?=$(PREFIX)/lib
    ...
    
    install:
        cp -f libcoolstuff.1.dylib $(LIBDIR)/libcoolstuff.1.dylib
        rm -f $(LIBDIR)/libcoolstuff.dylib
        ln -s $(LIBDIR)/libcoolstuff.1.dylib $(LIBDIR)/libcoolstuff.dylib
        install_name_tool -change "libcoolstuff.1.dylib" "$(LIBDIR)/libcoolstuff.1.dylib" $(LIBDIR)/libcoolstuff.1.dylib
    

    Under otool, it looks like:

    $ otool -L libcoolstuff.dylib 
    libcoolstuff.dylib:
        libcoolstuff.1.dylib (compatibility version 1.0.0, current version 1.0.6)
        /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.7)
    

    Finally, you would use it as expected:

    export CFLAGS="-NDEBUG -g2 -O2 -Wall -arch ppc -arch ppc64"
    make
    ...
    

提交回复
热议问题