create share library with WiringPI

243 views Asked by At

I want create a shared library, because I need this for JNI.

I have two files

PiezoSpeaker.c

#include "PiezoSpeaker.h"
#include <stdio.h>

#include <string.h>
#include <wiringPi.h>
#include <softTone.h>

JNIEXPORT void JNICALL Java_PiezoSpeaker_tone__I
(JNIEnv *env, jobject jobject, jint khz) {
wiringPiSetup();
softToneCreate(29);
softToneWrite (29, khz);
}

JNIEXPORT void JNICALL Java_PiezoSpeaker_tone__II
(JNIEnv *env, jobject jobject, jint khz, jint duration) {

}

and

PiezoSpeaker.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class PiezoSpeaker */

#ifndef _Included_PiezoSpeaker
#define _Included_PiezoSpeaker
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     PiezoSpeaker
 * Method:    tone
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_PiezoSpeaker_tone__I
  (JNIEnv *, jobject, jint);

/*
 * Class:     PiezoSpeaker
 * Method:    tone
 * Signature: (II)V
 */
JNIEXPORT void JNICALL Java_PiezoSpeaker_tone__II
  (JNIEnv *, jobject, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

I compile this with gcc

gcc -c -fPIC -I"/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/" -I"/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/linux" -IwiringPi -Ipthread PiezoSpeaker.c

gcc -shared -o share.so PiezoSpeaker.o

I become the error, that the symbol is undefined. The gcc command is wrong, because it does not include the wiringPi Library. How can I link the library, that the WiringPi Lib is included?

1

There are 1 answers

0
wordtronix On

Depending on the version of WiringPi you are using and how you built and installed it, the call could change. Assuming you are on a Rasberry Pi with WiringPi - github (not the deprecated version but the unofficial mirror) and you have cloned the repo locally into your Rasberry Pi.

cd WiringPi
./build 
# add /usr/local/lib to /etc/ld.so.conf
sudo ldconfig

Above is take from WiringPi INSTALL

Then for you compilation line you would need

gcc -c -fPIC -I"/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/" -I"/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/linux" -lwiringPi -pthread -lrt -lm PiezoSpeaker.c
  1. -lwiringPi -- gets you wiringPi
  2. -lrt and -lm -- take care of linking to dependencies within wiringPi itself