我想用portaudio庫編寫一些程序。我下載了源代碼,打開了cmake項目並構建了靜態(libportaudio_static.a)和動態(libportaudio.dll和libportaudio.dll.a)文件。如何靜態鏈接portaudio?
然後,我創建了只有主文件的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_executable
使用${PROJECT_SOURCE_DIR}/libportaudio_static.a
,或set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-Lportaudio_static")
給了我這個錯誤:
C:/Users/duso/ClionProjects/snoozeRec/main.cpp:7:未定義的引用`Pa_GetVersion」
如果我嘗試@亞歷克斯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 `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `QueryInputWaveFormatEx':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:539: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:555: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `QueryOutputWaveFormatEx':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:565: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:581: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `QueryWaveInKSFilterMaxChannels':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:676: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:685: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `InitializeInputDeviceInfo':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:715: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `QueryWaveOutKSFilterMaxChannels':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:806: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:815: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `InitializeOutputDeviceInfo':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:848: undefined reference to `[email protected]'
../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 `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1024: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1028: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1032: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `InitializeWaveHandles':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1904: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1910: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1950: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1954: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `TerminateWaveHandles':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1984: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:1991: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2002: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2006: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `InitializeWaveHeaders':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2093: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2097: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2103: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2107: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `TerminateWaveHeaders':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2143: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2145: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `AdvanceToNextInputBuffer':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2750: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2756: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `AdvanceToNextOutputBuffer':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2777: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:2783: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `ProcessingThreadProc':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3012: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `StartStream':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3279: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3283: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3296: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3299: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3367: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3371: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3432: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3437: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3447: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3450: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `StopStream':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3582: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3586: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3595: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3599: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wmme.c.obj): In function `AbortStream':
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3641: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3644: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3654: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wmme/pa_win_wmme.c:3657: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `BuildFilterList':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3089: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3105: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3112: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3126: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3152: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3169: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3175: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3184: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3200: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3209: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3225: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3243: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3304: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `ScanDeviceInfos':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3468: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3471: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3473: undefined reference to `[email protected]'
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:3476: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `BumpThreadPriority':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:5535: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `DropThreadPriority':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:5561: undefined reference to `[email protected]'
../libportaudio_static.a(pa_win_wdmks.c.obj): In function `GetCurrentTimeInMillisecs':
C:/portaudio/src/hostapi/wdmks/pa_win_wdmks.c:5866: undefined reference to `[email protected]
一個dll文件是一個動態庫。您需要從源代碼構建portAudio作爲靜態庫('.a'或'.lib'文件)。因此你必須使用'--enable-static --disable-shared'運行'configure'(最後一個是可選的) – Nidhoegger
我確實建立了靜態和動態版本(你看,我有dll和.a文件), portAudio的cmakelists有兩種配置。 – Leitos
然後,當我嘗試使用第一個或第二個方法鏈接到.a文件(並註釋掉動態鏈接)時,會出現與'.a'文件相關的鏈接,而不是'.dll' – Nidhoegger