c++static-linkingportaudio

How to statically link portaudio?


I wanted to write some program using portaudio library. I downloaded the source, opened the cmake project and builded both static(libportaudio_static.a) and dynamic(libportaudio.dll and libportaudio.dll.a) files.

Then, I created c++ project with just main file, where I tried to print out the version of portaudio library. When I used dynamic linking, it worked, but I can't seem to use the static one. It gives me undefined reference to the Pa_GetVersion() method.

What am I doing wrong?

I am using Clion and building it with CMake. I have Windows 10.

This is my 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)

This is my main.cpp:

#include <iostream>

#include "portaudio.h"

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

For the sake of simplicity, I put all the files in the same directory.

Trying to use ${PROJECT_SOURCE_DIR}/libportaudio_static.a in add_executable, or set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-Lportaudio_static") gives me this error:

C:/Users/duso/ClionProjects/snoozeRec/main.cpp:7: undefined reference to `Pa_GetVersion'

If i try @alex-zywicki 's suggestion, using target_link_libraries(snoozeRec ${CMAKE_SOURCE_DIR}/libportaudio_static.a), it gives me bunch of undefined references:

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

Solution

  • So, i managed to resolve the issue. I asked my supervisor at school and he advised me to add the whole portaudio source to my project and add its cmakelists into mine. The resulting CMakeLists.txt looks like this:

    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)