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
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
...
I had a similar issue on OS X which I resolved using the the install_name
switch instead of soname
.
gcc -shared <files> -lc -Wl,-install_name,<libname>.so, -o <libname>.so.1
OK, I found where problem was. Before build, you have to remove all CMake temp folders and files, e.g. CMakeFiles
, CMakeCache.txt
, Makefile
. As in my case, issue was that I built that project on Linux
and didn't delete these files...
That's why there's .so extension...