在Linux上链接c ++库

时间:2015-01-10 13:18:00

标签: c++ linux linker

我正在运行以下命令:

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,我也参与了参数顺序但是没有用。我赞不绝口。

2 个答案:

答案 0 :(得分:1)

您是否尝试使用-pthread作为链接器标志...有时它不会与-lpthread一起使用...

这应该有效

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

答案 1 :(得分:0)

使用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位内核上。