Abkarino Abkarino - 4 months ago 19
Linux Question

creating shared library linker undefined reference to

I build a shared library using

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


When I try to use the library I get a lot of

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


This is the command I use

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


As shown in the command -lonvif is after the input file onvifclient.cpp so the linker should find it.

Using a static library from the same object files .o works and the linker produce the executable.

All the answers I found on SO, are related to the ordering.

More about the errors:

../../../../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.


All this functions are found in the cpp files and defined right.

using
nm libonvif.so -C| grep MediaBindingService::GetVideoEncoderConfigurations


I get the folwing result.

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


MediaBindingService::GetVideoEncoderConfigurations
is a pure virtual function defined inside the class as:

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

Answer

The process of making the shared library was right. The problem is that I had functions without body. It was solved be making the virtual functions pure virtual.

 virtual void function_name() = 0;

instead of

 virtual void function_name();

Static libraries allow functions without body; shared libraries doesn't.