0

I have written some code in C which I want to call from Java. What I have done is I have a function in my c code which calls another c function from libspotify and I am trying to write a Java Wrapper using JNA to call my c function.

I have written a simple login function for spotify in c which is actually working. Here is a link to that c file which has the method

https://github.com/shroffrushabh/libspotify_java/blob/master/jukebox.c

The next step I followed is, instead of using the make file provided in the libspotify examples I used the following cmd to compile and create a .so file

gcc -o libspot.so -shared jukebox.c appkey.c

This is what my java file looks like

import com.sun.jna.Library;
import com.sun.jna.Native;

public class SpotifyTest {
    public interface JukeBox extends Library {
        public int login();
    }
    static public void main(String argv[]) {
        JukeBox jk = (JukeBox) Native.loadLibrary("spot", JukeBox.class);
        jk.login();
    }
}

The following are the commands to compile and run the java file

javac -classpath jna-4.0.0.jar SpotifyTest.java
java -classpath jna-4.0.0.jar:. SpotifyTest

Following is the exception I get:

java.lang.UnsatisfiedLinkError: /home/rushabh/libctest.so: undefined symbol: sp_session_create

I am not sure what is going wrong here, but here is what I am thinking, there is a sp_session_create function in libspotify(the C api which spotify has provided) which I am calling in my c code, so I think I need to somehow link the libspotify library when I am trying to generate my .so file. It would be great if you could give me some suggestions on how to solve this problem

user1386101
  • 1,945
  • 1
  • 14
  • 21

2 Answers2

2

You need to link in the spotify library.

This should be done when you're compiling libspot.so. This can be done with:

-L/path/to/spotify/library

This should be something/libspotify/lib, and is likely the directory where you built libspot.so. You may also need to specify:

-I/path/to/spotify/includes
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • gcc -o libspot.so -L ~/Downloads/libspotify-12.1.51-Linux-i686-release/lib/ -L /usr/local/lib/ -I ~/Downloads/libspotify-12.1.51-Linux-i686-release/include/libspotify/ -shared jukebox.c appkey.c – user1386101 Jan 14 '14 at 16:39
  • You need to pass in `-Wl,-rpath,$HOME/Downloads/libspotify-12.151-Linux-i686-releases/lib` as well, so the `libspot.so` knows to look there for the library. I tend not to use `~` in path names as evaluation rules are very restricted – Anya Shenanigans Jan 14 '14 at 16:55
  • Hey Petesh, thank you for your comment, is it possible for you tell me how the 2 flags you mentioned fit into the gcc compilation command, i.e. how would the gcc command look like? – user1386101 Jan 14 '14 at 20:01
-1

after 2 days of researching this is how I got it to work, following is the command I used to generate the .so file.

gcc -L~/Downloads/libspotify-12.1.51-Linux-i686-release/lib  -shared appkey.c jukebox.c  -lspotify -o libspot.so

Rest of the steps to execute to Java file are same as above.

Thank you Erick Robertson and Petesh for your help :)

user1386101
  • 1,945
  • 1
  • 14
  • 21