我正在尝试在Linux上使用PortAudio,但是编译器抱怨对 ALSA 和 Jack 函数的未定义引用(确切的问题在本文末尾有描述) )。我已经设置了两个CMake项目:项目A和项目B,每个项目一个CMakeLists.txt
。
构建可执行文件并依赖于 the_library :
add_subdirectory(${projectB_dir})
add_executable(the_executable ${SOURCE_FILES})
target_link_libraries(the_executable LINK_PUBLIC the_library)
构建一个库 the_library ,这取决于PortAudio
,ExternalProject
:
PortAudio ExternalProject
ExternalProject_Add(project_portaudio
GIT_REPOSITORY https://git.assembla.com/portaudio.git
PREFIX ${CMAKE_SOURCE_DIR}/lib/portaudio
CONFIGURE_COMMAND <SOURCE_DIR>/configure
BUILD_IN_SOURCE 1
BUILD_COMMAND make
INSTALL_COMMAND ""
UPDATE_COMMAND "")
ExternalProject_Get_Property(project_portaudio BINARY_DIR)
ExternalProject_Get_Property(project_portaudio SOURCE_DIR)
set(portaudio_lib_dir "${BINARY_DIR}/lib/.libs")
set(portaudio_inc_dir "${SOURCE_DIR}/include")
add_library(portaudio STATIC IMPORTED)
set_property(TARGET portaudio PROPERTY IMPORTED_LOCATION "${portaudio_lib_dir}/libportaudio.a")
原生音频API ,以便 PortAudio 正常工作。
if(APPLE)
find_library(CORE_AUDIO_LIBRARY CoreAudio)
find_library(AUDIO_TOOLBOX_LIBRARY AudioToolbox)
find_library(AUDIO_UNIT_LIBRARY AudioUnit)
find_library(CARBON_LIBRARY Carbon)
SET(NATIVE_AUDIO_LIBS ${CORE_AUDIO_LIBRARY} ${AUDIO_TOOLBOX_LIBRARY} ${AUDIO_UNIT_LIBRARY} ${CARBON_LIBRARY})
elseif(UNIX)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(ALSA REQUIRED)
find_package(Threads REQUIRED)
SET(NATIVE_AUDIO_LIBS rt ${ALSA_LIBRARIES} jack Threads::Threads)
endif()
图书馆
add_library(the_library ${SOURCE_FILES} ${HEADER_FILES})
add_dependencies(the_library project_portaudio)
target_include_directories(the_library PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${portaudio_inc_dir})
target_link_libraries(the_library ${NATIVE_AUDIO_LIBS} portaudio)
当我构建 Project A 时,编译器会出现以下错误:
# A bunch of undefined references to ALSA functions...
[ 93%] Linking CXX executable the_executable
../lib/portaudio/src/project_portaudio/lib/.libs/libportaudio.a(pa_linux_alsa.o): In function `PaAlsa_LoadLibrary':
/home/tomasito/Development/project-a/lib/portaudio/src/project_portaudio/src/hostapi/alsa/pa_linux_alsa.c:380: undefined reference to `snd_pcm_open'
/home/tomasito/Development/project-a/lib/portaudio/src/project_portaudio/src/hostapi/alsa/pa_linux_alsa.c:381: undefined reference to `snd_pcm_close'
/home/tomasito/Development/project-a/lib/portaudio/src/project_portaudio/src/hostapi/alsa/pa_linux_alsa.c:382: undefined reference to `snd_pcm_nonblock'
/home/tomasito/Development/project-a/lib/portaudio/src/project_portaudio/src/hostapi/alsa/pa_linux_alsa.c:384: undefined reference to `snd_pcm_prepare'
...
# Followed by a bunch of undefined references to Jack functions...
../lib/portaudio/src/project_portaudio/lib/.libs/libportaudio.a(pa_jack.o): In function `GetStreamTime':
/home/tomasito/Development/project-a/lib/portaudio/src/project_portaudio/src/hostapi/jack/pa_jack.c:1738: undefined reference to `jack_frame_time'
/home/tomasito/Development/project-a/lib/portaudio/src/project_portaudio/src/hostapi/jack/pa_jack.c:1738: undefined reference to `jack_get_sample_rate'
...
我做错了什么?我将ALSA再次链接到我的库 the_library ,但它仍然抱怨。我还尝试将ALSA直接链接到项目A中的可执行文件,但这并没有任何区别。
PS:我得到的这些错误是在Linux上。它就像OSX上的魅力一样。