built protocol buffer 3.5 libraries below commands.
./configure "CFLAGS=-fPIC" "CXXFLAGS=-fPIC"
create libtest.so sample library which statically linked by libproto.a
sensor.proto file
syntax = 'proto3';
message Sensor {
string name = 1;
double temperature = 2;
int32 humidity = 3;
enum SwitchLevel {
CLOSED = 0;
OPEN = 1;
}
SwitchLevel door = 5;
}
test.h file
extern "C" void testprotocolbuffer();
test.cpp file
#include <iostream>
#include <fstream>
#include "sensor.pb.h"
#include "test.h"
void testprotocolbuffer()
{
Sensor sensor;
sensor.set_name("Laboratory");
sensor.set_temperature(23.4);
sensor.set_humidity(68);
sensor.set_door(Sensor_SwitchLevel_OPEN);
std::string s;
sensor.SerializeToString(&s);
std::cout << s.length() << std::endl;
}
main.cpp
#include "test.h"
#include <stdio.h>
int main()
{
testprotocolbuffer();
return 0;
}
Building Steps:
Generate sensor .h & .cc file using protocol buffer compiler
protobuf-3.5.0/src/protoc --cpp_out=. sensor.proto
Compile .cpp files ( sensor.pb.cc & test.cpp )
g++ -std=c++11 -fPIC -c test.cpp sensor.pb.cc -I protobuf-3.5.0/src
create a shared library using statically linked lib protocol buffer
g++ -shared -pthread -o libtest.so test.o sensor.pb.o -Lprotobuf-3.5.0/src/.libs -lprotobuf
Linking libtest.so file to main.cpp
g++ -Wall -o main main.cpp -L . -ltest
Throws error while executing ./main executable
./main
./main: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
Please find the readelf structure
readelf -d libtest.so
Dynamic section at offset 0x3f70a8 contains 28 entries:
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x0000000000000001 (NEEDED) Shared library: [ld-linux-x86-64.so.2]
0x000000000000000c (INIT) 0x1e84b0
0x000000000000000d (FINI) 0x365484
0x0000000000000019 (INIT_ARRAY) 0x5f2540
0x000000000000001b (INIT_ARRAYSZ) 304 (bytes)
0x000000000000001a (FINI_ARRAY) 0x5f2670
0x000000000000001c (FINI_ARRAYSZ) 8 (bytes)
0x000000006ffffef5 (GNU_HASH) 0x228
0x0000000000000005 (STRTAB) 0x64910
0x0000000000000006 (SYMTAB) 0x18a40
0x000000000000000a (STRSZ) 1283290 (bytes)
0x000000000000000b (SYMENT) 24 (bytes)
0x0000000000000003 (PLTGOT) 0x5f8000
0x0000000000000002 (PLTRELSZ) 214368 (bytes)
0x0000000000000014 (PLTREL) RELA
0x0000000000000017 (JMPREL) 0x1b3f50
0x0000000000000007 (RELA) 0x1a4428
0x0000000000000008 (RELASZ) 64296 (bytes)
0x0000000000000009 (RELAENT) 24 (bytes)
0x000000006ffffffe (VERNEED) 0x1a4328
0x000000006fffffff (VERNEEDNUM) 5
0x000000006ffffff0 (VERSYM) 0x19ddea
0x000000006ffffff9 (RELACOUNT) 135
0x0000000000000000 (NULL) 0x0
Able to find the exported function on libtest.so
nm -gDC libtest.so | grep "testprotocolbuffer"
000000000020b4a0 T testprotocolbuffer
Can you please help me on this why library loading failed even if we statically linked to a shared library?
Thanks in advance
Aucun commentaire:
Enregistrer un commentaire