在 Linux 上链接 c++ 库
Posted
技术标签:
【中文标题】在 Linux 上链接 c++ 库【英文标题】:linking c++ libraries on Linux 【发布时间】:2015-01-10 13:18:00 【问题描述】:我正在运行以下命令:
g++ -m32 testLogin.cpp -L/root/c++/libs -ldvrnetsdk -o testLoginO -lpthread -lasound
结果:
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_channels'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutex_trylock'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_readi'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_access'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_strerror'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutexattr_settype'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_rate'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_close'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_malloc'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_period_size'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutex_timedlock'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutexattr_destroy'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_drain'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_free'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_create'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_open'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_format'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_writei'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutexattr_init'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_any'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_prepare'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
出现此错误后我做的第一件事是包含库 -lpthread -lasound,我还处理了参数顺序但没有工作。我感谢任何帮助。
【问题讨论】:
代码和对象之后的库。-pthread
不是 -lpthread
。
【参考方案1】:
您是否尝试仅使用 -pthread 作为链接器标志...有时它无法与 -lpthread 配合使用...
这应该可行
g++ -m32 testLogin.cpp -L/root/c++/libs -ldvrnetsdk -pthread -lasound
【讨论】:
【参考方案2】:使用 gcc,链接的顺序很重要。
所以,尝试不同的顺序。
g++ -m32 testLogin.cpp -L/root/c++/libs -lpthread -lasound -ldvrnetsdk -o testLoginO
下单请看这个问题:Why does the order in which libraries are linked sometimes cause errors in GCC?
或者,您可以使用 start-group 选项。
gcc -m32 testLogin.cpp -L/root/c++/libs -Wl,--start-group -lpthread -lasound -ldvrnetsdk -Wl,--end-group -o testLoginO
编辑:当您仍然收到错误时,请在库中的字符串上使用nm
,并检查给出链接器错误的符号是否在库中。检查库版本。您可能使用的是 64 位内核。
【讨论】:
tks!我使用了您的第二个建议,并进行了一些修改: gcc -m32 -fuse-ld=bfd testLogin.cpp -L/root/c++/libs -Wl,--start-group -pthread -lasound -ldvrnetsdk -lstdc++ -Wl,- -end-group -o testLoginO。它解决了 pthread 问题,但我仍然对“snd...”有未定义的引用,我认为这与 lasound 库有关。有什么线索吗?以上是关于在 Linux 上链接 c++ 库的主要内容,如果未能解决你的问题,请参考以下文章