MK16
MK16

Reputation: 23

C++ linking custom .so libraries and then with JNI

I have a problem linking C++ libraries and I could really use some advice. I have two custom .so libraries, which I am using in my cpp project, which I am then linking to Java using JNI.

In my cpp project I am including the header files for the two libraries and JNI. Then I am creating an object file, like this:

g++ -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/lib/jvm/java-7-openjdk-amd64/include/linux -std=c++11 -fPIC -c my_file.cpp

The two includes specify the path to JNI libraries. I am using cpp11 and then a switch -fpic I found that may be necessary during creation of shared libraries.

Next I tried to link my object file: my_file.o with the two shared libraries like this:

g++ -fpic -L/<absolute path>/library1.so -L/<absolute path>/library2.so my_file.o -shared -o my_file.so

I am building a shared library which will be called from Java using JNI. I tried several different variations of assembling the linking parameters but none worked. I am further calling System.load() in Java on an absolute paths to my_file.so and library1.so Currently I am receiving an error:

/usr/lib/jvm/java-7-openjdk-amd64/bin/java: symbol lookup error: /<absoulte path>/my_file.so: undefined symbol: <method from library 1>

I have read several post here about linking and required parameter order of libraries in order to ensure the correct linking and I have gone through much of g++ documentation, yet no matter how I order them and no matter what parameters I use, it does not seem to work. I will be greatfull for any advice as to how to link these files.

Upvotes: 0

Views: 1364

Answers (1)

MK16
MK16

Reputation: 23

Figured it out with some help.

Firstly, for those who do not know about the command "ldd" on linux, it shows the linked symbols (linked library dependencies). When I saw that my libraries weren't there I tried to rearrange the linking command. I had to place the object file my_file.o before the other two libraries. Then I saw that ldd pointed out that the libraries were not found. I moved them to a system library and with some more symlinks everything worked. Thanks to everyone for their effort

Upvotes: 2

Related Questions