I wrote some wrapper code for an existing library (wiringPi) to read a temperature sensor but ended up with an error while consuming this library.
My wrapper lib looks like:
mylib.h
#ifndef mylib_h__
#define mylib_h__
extern void read_sensor();
#endif
mylib.c
#include "mylib.h"
#include <wiringPi.h>
void read_sensor() {
//here is the first call on the wiringPi lib
if (wiringPiSetup() == -1)
exit(1);
...
}
then i use gcc to compile my library:
gcc -Wall -Werror -fPIC -c mylib.c
gcc -shared -o libmylib.so mylib.o -lwiringPi
cp libmylib.so /usr/lib/
Hint: In case of a normal C program consumption of this library everything works fine.
Now there‘s my C# program which use PInvoke
to call read_sensor()
from this library:
Program.cs
class Program
{
[DllImport("wiringPi")]
static extern int wiringPiSetup();
[DllImport("mylib")]
static extern void read_sensor();
static void Main(string[] args)
{
wiringPiSetup();
read_sensor();
}
}
This program is compiled with the following arguments:
dontet publish -r linux-arm
and copied to my Raspberry-Pi.
Now i execute this C# program and the following error is thrown:
./my-program-name: symbol lookup error: /usr/lib/libmylib.so: undefined symbol: wiringPiSetup
What‘s going wrong here?
My first thought was, my program didn‘t know the wiringPi library. So i added an export for this dll and called wiringPiSetup()
for testing. Same result with or without this statement.
I added also a test function without the wiringPi dependency into my custom library. This is called fine by C#.
Did i mess something up at linking time?
Edit:
The command ldd /usr/lib/libmylib.so
gives this output:
linux-vdso.so.1 (0x7efad000)
/usr/lib/arm-linux-gnueabihf/libarmmem.so (0x76f73000)
libwiringPi.so => /usr/local/lib/libwiringPi.so (0x76f40000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x76dff000)
libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0x76d84000)
libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0x76d5c000)
librt.so.1 => /lib/arm-linux-gnueabihf/librt.so.1 (0x76d45000)
libcrypt.so.1 => /lib/arm-linux-gnueabihf/libcrypt.so.1 (0x76d05000)
/lib/ld-linux-armhf.so.3 (0x54abc000)
ldd /usr/lib/libmylib.so
say? Does it include alibwiringPi.so
? – Copter