Linux:与Alsa的未定义引用链接

问题描述 投票:0回答:1

这里有经验的C ++开发人员(尽管对编译器/链接的基本细节没有很多深入的经验)。我在Ubuntu 16.04上,似乎无法链接到alsa库。我已经在gcc 9.2.1中安装了libasound2-dev。

我正在使用的代码来自这里https://gist.github.com/albanpeignier/104902

我的编译输出是:

↳ g++ -L/usr/lib/x86_64-linux-gnu/libasound.so main.cpp

/tmp/cc9z8wYv.o: In function `main':
main.cpp:(.text+0x50): undefined reference to `snd_pcm_open'
main.cpp:(.text+0x67): undefined reference to `snd_strerror'
main.cpp:(.text+0xc2): undefined reference to `snd_pcm_hw_params_malloc'
main.cpp:(.text+0xd9): undefined reference to `snd_strerror'
main.cpp:(.text+0x130): undefined reference to `snd_pcm_hw_params_any'
main.cpp:(.text+0x147): undefined reference to `snd_strerror'
main.cpp:(.text+0x1a3): undefined reference to `snd_pcm_hw_params_set_access'
main.cpp:(.text+0x1ba): undefined reference to `snd_strerror'
main.cpp:(.text+0x214): undefined reference to `snd_pcm_hw_params_set_format'
main.cpp:(.text+0x22b): undefined reference to `snd_strerror'
main.cpp:(.text+0x288): undefined reference to `snd_pcm_hw_params_set_rate_near'
main.cpp:(.text+0x29f): undefined reference to `snd_strerror'
main.cpp:(.text+0x2fb): undefined reference to `snd_pcm_hw_params_set_channels'
main.cpp:(.text+0x312): undefined reference to `snd_strerror'
main.cpp:(.text+0x369): undefined reference to `snd_pcm_hw_params'
main.cpp:(.text+0x380): undefined reference to `snd_strerror'
main.cpp:(.text+0x3d0): undefined reference to `snd_pcm_hw_params_free'
main.cpp:(.text+0x3fa): undefined reference to `snd_pcm_prepare'
main.cpp:(.text+0x411): undefined reference to `snd_strerror'
main.cpp:(.text+0x45f): undefined reference to `snd_pcm_format_width'
main.cpp:(.text+0x4b8): undefined reference to `snd_pcm_readi'
main.cpp:(.text+0x4d2): undefined reference to `snd_strerror'
main.cpp:(.text+0x556): undefined reference to `snd_pcm_close'
collect2: error: ld returned 1 exit status

((如果我只使用-lasound,也会出现相同的错误)

这是令人困惑的地方,这是libasound.so中的符号:

↳ nm -gD /usr/lib/x86_64-linux-gnu/libasound.so
                 U accept
                 U access
0000000000000000 A ALSA_0.9
0000000000000000 A ALSA_0.9.0
0000000000000000 A ALSA_0.9.0rc4
0000000000000000 A ALSA_0.9.0rc8
0000000000000000 A ALSA_0.9.3
0000000000000000 A ALSA_0.9.5
0000000000000000 A ALSA_0.9.7
00000000000ba0b0 T alsa_lisp
00000000000ba390 T alsa_lisp_default_cfg
00000000000ba460 T alsa_lisp_default_cfg_free
00000000000b9f00 T alsa_lisp_free
00000000000ba490 T alsa_lisp_function
000000000002aea0 T alsa_lisp_init_objects
00000000002ff7a0 B alsa_lisp_nil
00000000000ba970 T alsa_lisp_result_free
00000000000ba9e0 T alsa_lisp_seq_count
00000000000ba980 T alsa_lisp_seq_first
00000000000baa20 T alsa_lisp_seq_integer
00000000000ba9a0 T alsa_lisp_seq_next
00000000000baa60 T alsa_lisp_seq_pointer
00000000002ff7e0 B alsa_lisp_t
                 U __assert_fail
                 U bind
...
0000000000077190 T _snd_pcm_null_open
0000000000076f80 T snd_pcm_null_open
00000000002ff778 B __snd_pcm_null_open_dlsym_pcm_001
000000000004e240 T snd_pcm_open
000000000004e330 T snd_pcm_open_fallback
000000000004e2d0 T snd_pcm_open_lconf
...

在上面的(巨大)符号列表中,至少列出了'snd_pcm_open'-那么为什么我得到未定义的引用?

我想念的是什么?我还能尝试什么?

c++ ubuntu ld undefined-reference alsa
1个回答
0
投票

我自己找到了解决方案;但这并不能解决问题的核心。

正如我提到的,我正在使用GCC 9.2.1。据我所知,安装GCC 9.2.1会在工具链中某处引起某种问题。 alsa库不仅不能正确链接,而且X11库也不能正确链接。

解决方案是去核并重新安装Ubuntu。我打算将其从16.04完全升级到18.04已有一段时间。安装18.04后,一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.