在 Linux 上链接 C++ 库

linking c++ libraries on Linux

我是运行以下命令:

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 一起使用...

这应该有效

g++ -m32 testLogin.cpp -L/root/c++/libs -ldvrnetsdk -pthread -lasound

对于 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?

或者,您可以使用启动组选项。

gcc -m32 testLogin.cpp  -L/root/c++/libs -Wl,--start-group -lpthread -lasound -ldvrnetsdk -Wl,--end-group -o testLoginO

编辑:由于您仍然收到错误,请对库中的字符串使用 nm 并检查给出链接器错误的符号是否在库中。检查库版本。您可能使用的是 64 位内核。