2017-02-14 135 views
0

我想用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] 
+1

一个dll文件是一个动态库。您需要从源代码构建portAudio作为静态库('.a'或'.lib'文件)。因此你必须使用'--enable-static --disable-shared'运行'configure'(最后一个是可选的) – Nidhoegger

+0

我确实建立了静态和动态版本(你看,我有dll和.a文件), portAudio的cmakelists有两种配置。 – Leitos

+0

然后,当我尝试使用第一个或第二个方法链接到.a文件(并注释掉动态链接)时,会出现与'.a'文件相关的链接,而不是'.dll' – Nidhoegger

回答

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) 
0

您是否尝试过更换:

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

有了:

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

我试过了,它说:***没有规则来制作目标'../libportaudio.a','snoozeRec.exe'需要。停止。 可能是因为该文件被称为libportaudio_static.a。试着用这个名字代替大量未定义的参考错误 – Leitos

+0

库相对于源代码的位置在哪里? –

+0

有关的一切都在同一个目录下; CMakeLists.txt,main.cpp,libportaudio_static.a,portaudio。h – Leitos