Re: JNI Programming
On Nov 9, 2:18 am, Gordon Beaton <n....@for.email> wrote:
On Thu, 08 Nov 2007 14:31:45 -0800, Bala wrote:
This is the command that i use to build both the shared libraries.
libOpenApiImp.so
----------------
g++ -fpic -Wl,-G -Wl,-dy -Wno-deprecated -D_REENTRANT -DSOLARIS -DSUN4
-DSUN -DSVR4 -D_POSIX_PTHREAD_SEMANTICS -I/usr/java/include -I/usr/
java/include/solaris
-I/home1/users/biyer/Bala/OAPI/include -I/home1/users/biyer/Bala/OAPI/
src JavaC.C /home1/users/biyer/Bala/OAPI/lib/libAdpt.a /home1/users/
biyer/Bala/OAPI/lib/libDef.a
/home1/users/biyer/Bala/OAPI/lib/libxAdpt.a -o libOpenApiImp.so
libSubscribe.so
---------------
g++ -fpic -Wl,-G -Wl,-dy -Wno-deprecated -D_REENTRANT -DSOLARIS -DSUN4
-DSUN -DSVR4
-D_POSIX_PTHREAD_SEMANTICS -I/usr/java/include -I/usr/java/include/
solaris
-I/home1/users/biyer/Bala/OAPI/include -I/home1/users/biyer/Bala/OAPI/
src Common.C /home1/users/biyer/Bala/OAPI/lib/libAdpt.a /home1/users/
biyer/Bala/OAPI/lib/libDef.a
/home1/users/biyer/Bala/OAPI/lib/libxAdpt.a -o libSubscribe.so
Joshua has already explained about the name mangling.
I just thought I'd mention that it's rather unconventional to specify
static libraries as though they were unadorned object files, even
though it might work for you. The "normal" way is to specify one or
more library paths with -L, the each library with -l, like this:
-L /home1/users/biyer/Bala/OAPI/lib -lAdpt -lDef -lxAdpt
IMO this is both clearer and easier to maintain (lookup "DRY" on
wikipedia). There may also be subtle differences in the way the linker
treats the files as well, but that's just speculation.
/gordon
--- Hide quoted text -
- Show quoted text -
Hello,
Thanx Joshua,
I was able to run the application when i put OAPI_main in extern "C".
Was a silly mistake. But the static libraries that i am using are
compiled with CC 5.8. So g++ compiled code doesnt work for specific
functions when it tries to call them. Hence i migrated my source
compilation to CC, the same code just that i used CC instead of g++ to
create the shared libraries.
I am facing a different issue here.
Java program loads the first libOpenApiImp.so file properly, but this
in turn fails to load libSubscribe.so. This works perfect with g++
compiled versions.
I created a shared library using CC with the following command
CC -G -D_REENTRANT -DSOLARIS -DSUN4 -DSUN -DSVR4 -
D_POSIX_PTHREAD_SEMANTICS -I/usr/java/include -I/usr/java/include/
solaris -I/home1/users/biyer/Bala/OAPI/include -I/home1/users/biyer/
Bala/OAPI/src Common.cpp liba.a libb.b -o libSubscribe.so
Then i compiled my source as
CC -DSOLARIS -DSUN4 -DSUN -DSVR4 -D_POSIX_PTHREAD_SEMANTICS -I/home1/
users/biyer/Bala/OAPI/include -I/home1/users/biyer/Bala/OAPI/src
Main.cpp -o Main.o
Now when i run Main.o it says its not able to open the shared
library. It prints an error message which is part of my code. "Error
Loading the Dynamic Library"
I am also attaching the sample code in here.. The path of the library
that i have mentioned is correct since the same code works fine with g+
+ created shared library.
Please let me know if i need to do something differently if i am using
CC.
Main.cpp
--------
#include <iostream>
#include <Common.h>
#include <dlfcn.h>
#include <link.h>
#include <stdio.h>
using namespace std;
int main ()
{
void *handle;
void (*fptr)();
handle = dlopen ("./libSubscribe.so", RTLD_GLOBAL | RTLD_LAZY);
if (handle == NULL)
printf ("\nError Loading the Dynamic Library\n");
else
{
printf ("\n Library Loaded Successfully\n");
fptr = (void (*) ()) dlsym (handle, "OAPI_main");
if (fptr)
{
(*fptr)();
printf ("\n OAPI_Main Called\n");
}
else
{
printf ("\nCould not find method\n");
}
}
return (0);
}