“ld:未知选项:-soname”在 OS X 上

发布于 2024-10-09 17:30:24 字数 3886 浏览 0 评论 0原文

我尝试在 Mac OS X 上使用 CMake 构建应用程序,但收到以下错误:

Linking CXX shared library libsml.so
ld: unknown option: -soname
collect2: ld returned 1 exit status
make[2]: *** [libsml.so] Error 1
make[1]: *** [CMakeFiles/sml.dir/all] Error 2
make: *** [all] Error 2

这很奇怪,因为 Mac 的扩展名为 .dylib 而不是 .so。

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

PROJECT (SilentMedia)

SET(SourcePath src/libsml)

IF (DEFINED OSS)
SET(OSS_src
    ${SourcePath}/Media/Audio/SoundSystem/OSS/DSP/DSP.cpp
    ${SourcePath}/Media/Audio/SoundSystem/OSS/Mixer/Mixer.cpp
)
ENDIF(DEFINED OSS)

IF (DEFINED ALSA)
SET(ALSA_src
    ${SourcePath}/Media/Audio/SoundSystem/ALSA/DSP/DSP.cpp
    ${SourcePath}/Media/Audio/SoundSystem/ALSA/Mixer/Mixer.cpp
)
ENDIF(DEFINED ALSA)

SET(SilentMedia_src

    ${SourcePath}/Utils/Base64/Base64.cpp
    ${SourcePath}/Utils/String/String.cpp
    ${SourcePath}/Utils/Random/Random.cpp

    ${SourcePath}/Media/Container/FileLoader.cpp

    ${SourcePath}/Media/Container/OGG/OGG.cpp

    ${SourcePath}/Media/PlayList/XSPF/XSPF.cpp
    ${SourcePath}/Media/PlayList/XSPF/libXSPF.cpp
    ${SourcePath}/Media/PlayList/PlayList.cpp

    ${OSS_src}
    ${ALSA_src}

    ${SourcePath}/Media/Audio/Audio.cpp
    ${SourcePath}/Media/Audio/AudioInfo.cpp
    ${SourcePath}/Media/Audio/AudioProxy.cpp

    ${SourcePath}/Media/Audio/SoundSystem/SoundSystem.cpp
    ${SourcePath}/Media/Audio/SoundSystem/libao/AO.cpp

    ${SourcePath}/Media/Audio/Codec/WAV/WAV.cpp
    ${SourcePath}/Media/Audio/Codec/Vorbis/Vorbis.cpp
    ${SourcePath}/Media/Audio/Codec/WavPack/WavPack.cpp
    ${SourcePath}/Media/Audio/Codec/FLAC/FLAC.cpp
)

SET(SilentMedia_LINKED_LIBRARY
    sml
    vorbisfile
    FLAC++
    wavpack
    ao
    #asound
    boost_thread-mt
    boost_filesystem-mt
    xspf
    gtest
)

INCLUDE_DIRECTORIES(
    /usr/include
    /usr/local/include
    /usr/include/c++/4.4
    /Users/alex/Downloads/boost_1_45_0
    ${SilentMedia_SOURCE_DIR}/src
    ${SilentMedia_SOURCE_DIR}/${SourcePath}
)

#link_directories(
#   /usr/lib
#   /usr/local/lib
#   /Users/alex/Downloads/boost_1_45_0/stage/lib
#)

IF(LibraryType STREQUAL "static")
  ADD_LIBRARY(sml-static STATIC ${SilentMedia_src})
  # rename library from libsml-static.a => libsml.a
  SET_TARGET_PROPERTIES(sml-static PROPERTIES OUTPUT_NAME "sml")
  SET_TARGET_PROPERTIES(sml-static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
ELSEIF(LibraryType STREQUAL "shared")
  ADD_LIBRARY(sml SHARED ${SilentMedia_src})

  # change compile optimization/debug flags # -Werror -pedantic
  IF(BuildType STREQUAL "Debug")
    SET_TARGET_PROPERTIES(sml PROPERTIES COMPILE_FLAGS "-pipe -Wall -W -ggdb")
  ELSEIF(BuildType STREQUAL "Release")
    SET_TARGET_PROPERTIES(sml PROPERTIES COMPILE_FLAGS "-pipe -Wall -W -O3 -fomit-frame-pointer")
  ENDIF()

  SET_TARGET_PROPERTIES(sml PROPERTIES CLEAN_DIRECT_OUTPUT 1)
ENDIF()

### TEST ###

IF(Test STREQUAL "true")
  ADD_EXECUTABLE (bin/TestXSPF ${SourcePath}/Test/Media/PlayLists/XSPF/TestXSPF.cpp)
  TARGET_LINK_LIBRARIES (bin/TestXSPF ${SilentMedia_LINKED_LIBRARY})

  ADD_EXECUTABLE (bin/test1 ${SourcePath}/Test/test.cpp)
  TARGET_LINK_LIBRARIES (bin/test1 ${SilentMedia_LINKED_LIBRARY})

  ADD_EXECUTABLE (bin/TestFileLoader ${SourcePath}/Test/Media/Container/FileLoader/TestFileLoader.cpp)
  TARGET_LINK_LIBRARIES (bin/TestFileLoader ${SilentMedia_LINKED_LIBRARY})

  ADD_EXECUTABLE (bin/testMixer ${SourcePath}/Test/testMixer.cpp)
  TARGET_LINK_LIBRARIES (bin/testMixer ${SilentMedia_LINKED_LIBRARY})
ENDIF (Test STREQUAL "true")

### TEST ###

ADD_CUSTOM_TARGET(doc COMMAND doxygen ${SilentMedia_SOURCE_DIR}/doc/Doxyfile)

Linux 上没有错误。

构建过程:

cmake -D BuildType=Debug -D LibraryType=shared .
make

我发现,在 CMakeFiles/sml.dir/link.txt 中生成了错误的命令。但是为什么,因为 CMake 的目标是跨平台..

如何解决它?

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 returned 1 exit status
make[2]: *** [libsml.so] Error 1
make[1]: *** [CMakeFiles/sml.dir/all] Error 2
make: *** [all] Error 2

This is strange, as Mac has .dylib extension instead of .so.

There's my CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)

PROJECT (SilentMedia)

SET(SourcePath src/libsml)

IF (DEFINED OSS)
SET(OSS_src
    ${SourcePath}/Media/Audio/SoundSystem/OSS/DSP/DSP.cpp
    ${SourcePath}/Media/Audio/SoundSystem/OSS/Mixer/Mixer.cpp
)
ENDIF(DEFINED OSS)

IF (DEFINED ALSA)
SET(ALSA_src
    ${SourcePath}/Media/Audio/SoundSystem/ALSA/DSP/DSP.cpp
    ${SourcePath}/Media/Audio/SoundSystem/ALSA/Mixer/Mixer.cpp
)
ENDIF(DEFINED ALSA)

SET(SilentMedia_src

    ${SourcePath}/Utils/Base64/Base64.cpp
    ${SourcePath}/Utils/String/String.cpp
    ${SourcePath}/Utils/Random/Random.cpp

    ${SourcePath}/Media/Container/FileLoader.cpp

    ${SourcePath}/Media/Container/OGG/OGG.cpp

    ${SourcePath}/Media/PlayList/XSPF/XSPF.cpp
    ${SourcePath}/Media/PlayList/XSPF/libXSPF.cpp
    ${SourcePath}/Media/PlayList/PlayList.cpp

    ${OSS_src}
    ${ALSA_src}

    ${SourcePath}/Media/Audio/Audio.cpp
    ${SourcePath}/Media/Audio/AudioInfo.cpp
    ${SourcePath}/Media/Audio/AudioProxy.cpp

    ${SourcePath}/Media/Audio/SoundSystem/SoundSystem.cpp
    ${SourcePath}/Media/Audio/SoundSystem/libao/AO.cpp

    ${SourcePath}/Media/Audio/Codec/WAV/WAV.cpp
    ${SourcePath}/Media/Audio/Codec/Vorbis/Vorbis.cpp
    ${SourcePath}/Media/Audio/Codec/WavPack/WavPack.cpp
    ${SourcePath}/Media/Audio/Codec/FLAC/FLAC.cpp
)

SET(SilentMedia_LINKED_LIBRARY
    sml
    vorbisfile
    FLAC++
    wavpack
    ao
    #asound
    boost_thread-mt
    boost_filesystem-mt
    xspf
    gtest
)

INCLUDE_DIRECTORIES(
    /usr/include
    /usr/local/include
    /usr/include/c++/4.4
    /Users/alex/Downloads/boost_1_45_0
    ${SilentMedia_SOURCE_DIR}/src
    ${SilentMedia_SOURCE_DIR}/${SourcePath}
)

#link_directories(
#   /usr/lib
#   /usr/local/lib
#   /Users/alex/Downloads/boost_1_45_0/stage/lib
#)

IF(LibraryType STREQUAL "static")
  ADD_LIBRARY(sml-static STATIC ${SilentMedia_src})
  # rename library from libsml-static.a => libsml.a
  SET_TARGET_PROPERTIES(sml-static PROPERTIES OUTPUT_NAME "sml")
  SET_TARGET_PROPERTIES(sml-static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
ELSEIF(LibraryType STREQUAL "shared")
  ADD_LIBRARY(sml SHARED ${SilentMedia_src})

  # change compile optimization/debug flags # -Werror -pedantic
  IF(BuildType STREQUAL "Debug")
    SET_TARGET_PROPERTIES(sml PROPERTIES COMPILE_FLAGS "-pipe -Wall -W -ggdb")
  ELSEIF(BuildType STREQUAL "Release")
    SET_TARGET_PROPERTIES(sml PROPERTIES COMPILE_FLAGS "-pipe -Wall -W -O3 -fomit-frame-pointer")
  ENDIF()

  SET_TARGET_PROPERTIES(sml PROPERTIES CLEAN_DIRECT_OUTPUT 1)
ENDIF()

### TEST ###

IF(Test STREQUAL "true")
  ADD_EXECUTABLE (bin/TestXSPF ${SourcePath}/Test/Media/PlayLists/XSPF/TestXSPF.cpp)
  TARGET_LINK_LIBRARIES (bin/TestXSPF ${SilentMedia_LINKED_LIBRARY})

  ADD_EXECUTABLE (bin/test1 ${SourcePath}/Test/test.cpp)
  TARGET_LINK_LIBRARIES (bin/test1 ${SilentMedia_LINKED_LIBRARY})

  ADD_EXECUTABLE (bin/TestFileLoader ${SourcePath}/Test/Media/Container/FileLoader/TestFileLoader.cpp)
  TARGET_LINK_LIBRARIES (bin/TestFileLoader ${SilentMedia_LINKED_LIBRARY})

  ADD_EXECUTABLE (bin/testMixer ${SourcePath}/Test/testMixer.cpp)
  TARGET_LINK_LIBRARIES (bin/testMixer ${SilentMedia_LINKED_LIBRARY})
ENDIF (Test STREQUAL "true")

### TEST ###

ADD_CUSTOM_TARGET(doc COMMAND doxygen ${SilentMedia_SOURCE_DIR}/doc/Doxyfile)

There was no error on Linux.

Build process:

cmake -D BuildType=Debug -D LibraryType=shared .
make

I found, that incorrect command generate in CMakeFiles/sml.dir/link.txt. But why, as the goal of CMake is cross-platforming..

How to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瀟灑尐姊 2024-10-16 17:30:24

我在 OS X 上遇到了类似的问题,我使用 install_name 开关而不是 soname 解决了这个问题。

gcc -shared <files> -lc -Wl,-install_name,<libname>.so, -o <libname>.so.1

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
眼泪淡了忧伤 2024-10-16 17:30:24

您解决了您的问题,但我想将此作为未来访问者的参考,因为在 OS X 上创建动态库还有更多内容。另外,请参阅 在 Apple 开发者页面中创建动态库

OS X 不使用 libcoolstuff.so.XYZ 约定。相反,OS X 使用约定libcoolstuff.X.dylib。然后,要将 XYZ 嵌入到库中,请使用 -install_name-current_version-compatibility_version

我不知道 Cmake,但这是它在 Make 下的样子。构建 libcoolstuff 1.0.6 的方法将如下所示:

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)

并且您的 make install 规则将如下所示:

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

otool 下,它如下所示:

$ 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)

最后,您将按预期使用它:

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

You cleared your problem, but I wanted to provide this as 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. Instead, OS X uses the convention libcoolstuff.X.dylib. Then, 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
...
永言不败 2024-10-16 17:30:24

好的,我找到问题所在了。在构建之前,您必须删除所有 CMake 临时文件夹和文件,例如 CMakeFilesCMakeCache.txtMakefile。就我而言,问题是我在 Linux 上构建了该项目并且没有删除这些文件......
这就是为什么有 .so 扩展名......

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...

缱绻入梦 2024-10-16 17:30:24

我自己也有这个问题。对我来说解决这个问题的方法是删除逗号并将 -soname 替换为 -install_name

Had a problem with this myself. What fixed it for me was removing the commas and replacing -soname with -install_name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文