简体   繁体   English

在Linux上链接c ++库

[英]linking c++ libraries on Linux

I'm running the following command: 我正在运行以下命令:

g++ -m32 testLogin.cpp  -L/root/c++/libs  -ldvrnetsdk -o testLoginO -lpthread  -lasound 

the result: 结果:

/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

The first thing I did after this error was to include the libraries -lpthread -lasound, I also worked on the parameters order but did not work. 发生此错误后,我要做的第一件事是包括库-lpthread -lasound,我也处理了参数order,但没有工作。 I appriciate any help. 我寻求任何帮助。

did you try with just -pthread as the linker flag...sometimes it does notw ork with -lpthread... 您是否尝试仅使用-pthread作为链接器标志...有时不使用-lpthread ...

This should work 这应该工作

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

With gcc, ordering of linking does matter. 使用gcc,链接的顺序很重要。

So, try with different order. 因此,尝试使用不同的顺序。

 g++ -m32 testLogin.cpp  -L/root/c++/libs -lpthread -lasound -ldvrnetsdk -o testLoginO

See this question for order: Why does the order in which libraries are linked sometimes cause errors in GCC? 有关顺序的信息,请参见以下问题: 为什么链接库的顺序有时会导致GCC错误?

Alternatively, you can use start-group option. 或者,您可以使用开始组选项。

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

Edit: As you still get the error, use nm on strings on your library and check if the symbols for which linker error is given are in the library or not. 编辑:当您仍然收到错误时,请对库上的字符串使用nm并检查库中是否给出了给出链接器错误的符号。 Check for library version. 检查库版本。 You might be on 64-bit kernel. 您可能在64位内核上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM