如何静态link portaudio?

How to statically link portaudio?

我想使用 portaudio 库编写一些程序。我下载了源代码,打开了 cmake 项目并构建了静态(libportaudio_static.a)和动态(libportaudio.dll 和 libportaudio.dll.a)文件。

然后,我用主文件创建了 c++ 项目,我试图在其中打印出 portaudio 库的版本。当我使用动态链接时,它起作用了,但我似乎无法使用静态链接。它给了我对 Pa_GetVersion() 方法的未定义引用。

我做错了什么?

我正在使用 Clion 并使用 CMake 构建它。我有 Windows 10.

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(snoozeRec)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
# first way i tried to use static library
#set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-Lportaudio_static")

add_executable(
    snoozeRec
    ${SOURCE_FILES}
    #portaudio.h
    # second way i tried to use static library
    #${PROJECT_SOURCE_DIR}/libportaudio_static.a
)

target_link_libraries(snoozeRec ${CMAKE_SOURCE_DIR}/libportaudio.dll)

这是我的 main.cpp:

#include <iostream>

#include "portaudio.h"

int main()
{
    std::cout << "Version of portAudio: " << Pa_GetVersion() << std::endl;
    return 0;
}

为了简单起见,我把所有文件放在同一个目录下。

尝试在 add_executableset(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-Lportaudio_static") 中使用 ${PROJECT_SOURCE_DIR}/libportaudio_static.a 时出现此错误:

C:/Users/duso/ClionProjects/snoozeRec/main.cpp:7: 对 `Pa_GetVersion'

的未定义引用

如果我尝试@alex-zywicki 的建议,使用 target_link_libraries(snoozeRec ${CMAKE_SOURCE_DIR}/libportaudio_static.a),它会给我一堆未定义的引用:

../libportaudio_static.a(pa_win_util.c.obj): In function `PaUtil_GetTime':
C:/portaudio/src/os/win/pa_win_util.c:154: undefined reference to `_imp__timeGetTime@0'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `QueryInputWaveFormatEx':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:539: undefined reference to `_imp__waveInOpen@24'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:555: undefined reference to `_imp__waveInGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `QueryOutputWaveFormatEx':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:565: undefined reference to `_imp__waveOutOpen@24'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:581: undefined reference to `_imp__waveOutGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `QueryWaveInKSFilterMaxChannels':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:676: undefined reference to `_imp__waveInMessage@16'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:685: undefined reference to `_imp__waveInMessage@16'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `InitializeInputDeviceInfo':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:715: undefined reference to `_imp__waveInGetDevCapsW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `QueryWaveOutKSFilterMaxChannels':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:806: undefined reference to `_imp__waveOutMessage@16'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:815: undefined reference to `_imp__waveOutMessage@16'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `InitializeOutputDeviceInfo':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:848: undefined reference to `_imp__waveOutGetDevCapsW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `PaWinMme_Initialize':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1020: undefined reference to `_imp__waveInMessage@16'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1024: undefined reference to `_imp__waveOutMessage@16'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1028: undefined reference to `_imp__waveInGetNumDevs@0'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1032: undefined reference to `_imp__waveOutGetNumDevs@0'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `InitializeWaveHandles':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1904: undefined reference to `_imp__waveInOpen@24'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1910: undefined reference to `_imp__waveOutOpen@24'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1950: undefined reference to `_imp__waveInGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1954: undefined reference to `_imp__waveOutGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `TerminateWaveHandles':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1984: undefined reference to `_imp__waveInClose@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1991: undefined reference to `_imp__waveOutClose@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2002: undefined reference to `_imp__waveInGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2006: undefined reference to `_imp__waveOutGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `InitializeWaveHeaders':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2093: undefined reference to `_imp__waveInPrepareHeader@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2097: undefined reference to `_imp__waveInGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2103: undefined reference to `_imp__waveOutPrepareHeader@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2107: undefined reference to `_imp__waveInGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `TerminateWaveHeaders':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2143: undefined reference to `_imp__waveInUnprepareHeader@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2145: undefined reference to `_imp__waveOutUnprepareHeader@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `AdvanceToNextInputBuffer':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2750: undefined reference to `_imp__waveInAddBuffer@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2756: undefined reference to `_imp__waveInGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `AdvanceToNextOutputBuffer':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2777: undefined reference to `_imp__waveOutWrite@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2783: undefined reference to `_imp__waveOutGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `ProcessingThreadProc':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3012: undefined reference to `_imp__waveOutGetPosition@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `StartStream':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3279: undefined reference to `_imp__waveInAddBuffer@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3283: undefined reference to `_imp__waveInGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3296: undefined reference to `_imp__waveOutPause@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3299: undefined reference to `_imp__waveOutGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3367: undefined reference to `_imp__waveOutWrite@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3371: undefined reference to `_imp__waveOutGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3432: undefined reference to `_imp__waveInStart@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3437: undefined reference to `_imp__waveInGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3447: undefined reference to `_imp__waveOutRestart@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3450: undefined reference to `_imp__waveOutGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `StopStream':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3582: undefined reference to `_imp__waveOutReset@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3586: undefined reference to `_imp__waveOutGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3595: undefined reference to `_imp__waveInReset@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3599: undefined reference to `_imp__waveInGetErrorTextW@12'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `AbortStream':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3641: undefined reference to `_imp__waveOutReset@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3644: undefined reference to `_imp__waveOutGetErrorTextW@12'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3654: undefined reference to `_imp__waveInReset@4'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3657: undefined reference to `_imp__waveInGetErrorTextW@12'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `BuildFilterList':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3089: undefined reference to `_imp__SetupDiGetClassDevsW@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3105: undefined reference to `_imp__SetupDiEnumDeviceInterfaces@20'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3112: undefined reference to `_imp__SetupDiGetDeviceInterfaceAlias@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3126: undefined reference to `_imp__SetupDiGetDeviceInterfaceAlias@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3152: undefined reference to `_imp__SetupDiDestroyDeviceInfoList@4'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3169: undefined reference to `_imp__SetupDiEnumDeviceInterfaces@20'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3175: undefined reference to `_imp__SetupDiGetDeviceInterfaceAlias@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3184: undefined reference to `_imp__SetupDiGetDeviceInterfaceAlias@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3200: undefined reference to `_imp__SetupDiGetDeviceInterfaceAlias@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3209: undefined reference to `_imp__SetupDiGetDeviceInterfaceDetailW@24'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3225: undefined reference to `_imp__SetupDiGetDeviceRegistryPropertyW@28'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3243: undefined reference to `_imp__SetupDiOpenDeviceInterfaceRegKey@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3304: undefined reference to `_imp__SetupDiDestroyDeviceInfoList@4'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `ScanDeviceInfos':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3468: undefined reference to `_imp__waveInMessage@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3471: undefined reference to `_imp__waveInMessage@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3473: undefined reference to `_imp__waveOutMessage@16'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3476: undefined reference to `_imp__waveOutMessage@16'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `BumpThreadPriority':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:5535: undefined reference to `_imp__timeBeginPeriod@4'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `DropThreadPriority':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:5561: undefined reference to `_imp__timeEndPeriod@4'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `GetCurrentTimeInMillisecs':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:5866: undefined reference to `_imp__timeGetTime@0

您是否尝试过替换:

target_link_libraries(snoozeRec${CMAKE_SOURCE_DIR}/libportaudio.dll)

与:

target_link_libraries(snoozeRec${CMAKE_SOURCE_DIR}/libportaudio_static.a)

所以,我设法解决了这个问题。我问过我在学校的主管,他建议我将整个 portaudio 源添加到我的项目中,并将其 cmakelists 添加到我的项目中。结果 CMakeLists.txt 看起来像这样:

cmake_minimum_required(VERSION 3.6)
project(snoozeRec)

set(CMAKE_CXX_STANDARD 11)

add_subdirectory("C:\portaudio" asdf)

add_executable(
        snoozeRec
        main.cpp
)

target_link_libraries(snoozeRec portaudio_static)

timeXXXwaveXXX符号来自WinMM library and the SetupXXX symbols come from the Setup API。这是一个极简主义的 CMakeLists.txt,可以很好地与 MinGW 和 PortAudio v19 编译:

cmake_minimum_required(VERSION 3.13)
project(test-portaudio-windows)

set(PORTAUDIO_ROOT C:/CPP/portaudio)

add_executable(${PROJECT_NAME} main.cpp)
target_compile_features(${PROJECT_NAME PRIVATE cxx_std_11)
target_include_directories(${PROJECT_NAME} PRIVATE ${PORTAUDIO_ROOT}/include)
target_link_directories(${PROJECT_NAME} PRIVATE ${PORTAUDIO_ROOT}/lib)
target_link_options(${PROJECT_NAME} PRIVATE -static)
target_link_libraries(${PROJECT_NAME} PRIVATE portaudio_static winmm setupapi)