如何静态链接portaudio?

时间:2017-02-14 12:57:46

标签: c++ static-linking 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;
}

为简单起见,我将所有文件放在同一目录中。

尝试在${PROJECT_SOURCE_DIR}/libportaudio_static.a中使用add_executable,或set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-Lportaudio_static") 给出了这个错误:

C:/Users/duso/ClionProjects/snoozeRec/main.cpp:7:对'Pa_GetVersion&#39;

的未定义引用

如果我尝试@ 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

3 个答案:

答案 0 :(得分:3)

所以,我设法解决了这个问题。我问我在学校的主管,他建议我将整个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)

答案 1 :(得分:0)

您是否尝试过更换:

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

使用:

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

答案 2 :(得分:0)

timeXXXwaveXXX 符号来自 WinMM 库,SetupXXX 符号来自 Setup API。这是一个简约的 CMakeLists.txt,可与 MinGW 和 PortAudio v19 一起编译:

cmake_minimum_required(VERSION 3.13)

set(APP test-portaudio-windows)
project(${APP})

set(PORTAUDIO_ROOT C:/CPP/portaudio)

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