创建共享库链接器未定义引用

creating shared library linker undefined reference to

我使用

构建共享库
 arm-linux-gnueabi-g++ -Wall -fPIC -c -I../../../onvifgen -I. *.cpp -include openssl/all.h -fpermissive -DWITH_OPENSSL
 arm-linux-gnueabi-g++ -shared -fPIC -Wl,-soname,libonvif.so.1 -o libonvif.so.1.0   *.o
 ln -sf libonvif.so.1.0 libonvif.so.1
 ln -sf libonvif.so.1.0 libonvif.so

当我尝试使用该库时,我得到了很多

 ../../../../binaries/lib/libonvif.so: undefined reference to 

这是我使用的命令

 arm-linux-gnueabi-g++ -I../../../../binaries/include -I../../../include -fpermissive -include ../../../../binaries/include/openssl/all.h -w -o test1 onvifclient.cpp -L../../../../binaries/lib -lonvif -lssl -lcrypto -lpthread -DWITH_OPENSSL

如命令 -lonvif 所示,它位于输入文件 onvifclient.cpp 之后,因此链接器应该可以找到它。

使用来自相同目标文件的静态库 .o 有效并且链接器生成可执行文件。

我在 SO 上找到的所有答案都与排序有关。

有关错误的更多信息:

../../../../binaries/lib/libonvif.so: undefined reference to `ReceiverBindingService::CreateReceiver(_trv__CreateReceiver*, _trv__CreateReceiverResponse*)'
../../../../binaries/lib/libonvif.so: undefined reference to `PullPointSubscriptionBindingService::PullMessages(_tev__PullMessages*, _tev__PullMessagesResponse*)'
../../../../binaries/lib/libonvif.so: undefined reference to `ReceiverBindingService::GetReceiver(_trv__GetReceiver*, _trv__GetReceiverResponse*)'
../../../../binaries/lib/libonvif.so: undefined reference to `RecordingBindingService::GetRecordingJobState(_trc__GetRecordingJobState*, _trc__GetRecordingJobStateResponse*)'
../../../../binaries/lib/libonvif.so: undefined reference to `PullPointSubscriptionBindingService::Renew_(_wsnt__Renew*, _wsnt__RenewResponse*)'
../../../../binaries/lib/libonvif.so: undefined reference to `MediaBindingService::GetVideoEncoderConfigurations(_trt__GetVideoEncoderConfigurations*, _trt__GetVideoEncoderConfigurationsResponse*)'
../../../../binaries/lib/libonvif.so: undefined reference to `wsddService::Resolve(wsdd__ResolveType*)'
../../../../binaries/lib/libonvif.so: undefined reference to `MediaBindingService::GetAudioDecoderConfiguration_(_trt__GetAudioDecoderConfiguration*, _trt__GetAudioDecoderConfigurationResponse*)'
+ 100+ more errors but other functions.

所有这些函数都在 cpp 文件中找到并正确定义。

使用nm libonvif.so -C| grep MediaBindingService::GetVideoEncoderConfigurations

我得到以下结果。

     U MediaBindingService::GetVideoEncoderConfigurations(_trt__GetVideoEncoderConfigurations*, _trt__GetVideoEncoderConfigurationsResponse*)
     U MediaBindingService::GetVideoEncoderConfigurations_(_trt__GetVideoEncoderConfigurations*, _trt__GetVideoEncoderConfigurationsResponse*)

MediaBindingService::GetVideoEncoderConfigurations 是一个纯虚函数,在 class 中定义为:

virtual int GetVideoEncoderConfigurations(_trt__GetVideoEncoderConfigurations *trt__GetVideoEncoderConfigurations, _trt__GetVideoEncoderConfigurationsResponse *trt__GetVideoEncoderConfigurationsResponse) =0;

制作共享库的过程是对的。 问题是我的函数没有 body。 解决方法是将虚函数设为纯虚函数。

 virtual void function_name() = 0;

而不是

 virtual void function_name();

静态库允许没有body的函数;共享库没有。