andrewrk / libsoundio

C library for cross-platform real-time audio input and output
http://libsound.io/
MIT License
1.92k stars 229 forks source link

Static build of libsoundio #234

Open maximumspatium opened 4 years ago

maximumspatium commented 4 years ago

Hello,

first of all, thank you for bringing us this amazing sound library!

I'm planning to use libsoundio as sound server in our experimental Power Macintosh emulator DingusPPC (https://github.com/dingusdev/dingusppc).

While the initial integration has now been completed, I'd like to further optimize the build by using the static version of the library. Unfortunately, it doesn't work for me.

Instead of using a separate libsoundio installation, I compile and link libsoundio along with my application.

Our source tree is organized as follows:

- cpu
- debugger
- devices
- machines
- thirdparty
--- libsoundio

That's the corresponding excerpt from our CMakeLists.txt:

option(BUILD_EXAMPLE_PROGRAMS "Build libsoundio example programs" OFF)
option(BUILD_TESTS "Build libsoundio tests" OFF)
add_subdirectory("${PROJECT_SOURCE_DIR}/thirdparty/libsoundio")

[...]

include_directories( [...]
    ${LIBSOUNDIO_HEADERS})

[...]

target_link_libraries(dingusppc libsoundio_shared ${LIBSOUNDIO_LIBS})

The above build configuration only works with "libsoundio_shared". When I change it to "libsoundio_static" the build fails with the following error message:

Undefined symbols for architecture x86_64:
  "_AudioComponentFindNext", referenced from:
      _outstream_open_ca in libsoundio.a(coreaudio.c.o)
      _instream_open_ca in libsoundio.a(coreaudio.c.o)
  "_AudioComponentInstanceDispose", referenced from:
      _outstream_open_ca in libsoundio.a(coreaudio.c.o)
      _outstream_destroy_ca in libsoundio.a(coreaudio.c.o)
      _instream_open_ca in libsoundio.a(coreaudio.c.o)
      _instream_destroy_ca in libsoundio.a(coreaudio.c.o)
  "_AudioComponentInstanceNew", referenced from:
      _outstream_open_ca in libsoundio.a(coreaudio.c.o)
      _instream_open_ca in libsoundio.a(coreaudio.c.o)
  "_AudioObjectAddPropertyListener", referenced from:
[...]

What can I do to make it work?

I'm using: Mac OS X 10.13.6 clang 7.1.0 cmake 3.16.5

maximumspatium commented 4 years ago

Okay, I was able to statically link to libsoundio by adding the following command to my CMakeLists.txt:

get_directory_property(LIBSOUNDIO_LIBS DIRECTORY thirdparty/libsoundio DEFINITION LIBSOUNDIO_LIBS)

This solution is inspired by the cmake file of DeEsser project. Kudos to @Crataegus27

The updated CMakeLists.txt is located here

Unfortunately, linking to libsoundio_static VisualStudio 2019 doesn't work. I'm getting a bunch of errors like:

undefined symbol: __imp_soundio_outstream_create
undefined symbol: __imp_soundio_outstream_open
...

The above errors come from the LLVM/Clang toolchain. Switching to the MSVC toolchain produces similar errors (LNK2019 & LNK1120).

Linking to libsoundio_shared works though.

Help please!

shangjiaxuan commented 4 years ago

You need to define SOUNDIO_STATIC_LIBRARY for static linking. Use -DSOUNDIO_STATIC_LIBRARY