Buck Ingham
Buck Ingham

Reputation: 77

JNI/C++ compiling problems

I'm new to C++ and try to compile a .so file to run it via JNI in Java.

The directory structure of the C++ folder is:

/
/lib/ - this contains a .a file = library.a below
/folder1 - this contains the .cpp .h and .o files

Scenario 1:

I'm running the following:

g++ -std=c++11 -I$JAVA_HOME/include -Ifolder1/ -I$JAVA_HOME/include/linux -o outputFileName.so inputFileName.cpp -Llib -llibrary.a -fPIC -shared

I get the following error:

relocation R_X86_64_32 against '.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC

All of the files and the .so object had the -fPIC flag. What's the issue?

Scenario 2:

I'm running the following:

g++ -std=c++11 -I$JAVA_HOME/include -Ifolder1/ -I$JAVA_HOME/include/linux -o outputFileName.so inputFileName.cpp -shared

So without the -L and -l flags.

I get the .so file and put it in /usr/lib with the relevant naming i.e. "lib" prefix. When I run Java program I get:

symbol lookup error: /usr/lib/outputFileName.so: undefined symbol: _ZN3...

I use c++filt to decompile the symbol and I get a reference to a class like this:

namespace::Class::Constructor

I checked the .cpp file of the class and the constructor exists.

Help please?

Upvotes: 1

Views: 483

Answers (1)

Oo.oO
Oo.oO

Reputation: 13405

Take a look here for a sample code where you can see how JNI can use other shared library.

https://github.com/mkowsiak/jnicookbook/blob/master/recipes/recipeNo023/Makefile

It looks like you refer to some sort of libs that are used by your .so file.

Make sure to have them at your LD_LIBRARY_PATH - not just your JNI library.

Upvotes: 1

Related Questions