十分感谢版主的帮助!
应该是刚才您说的System.loadLibrary("testdll.so")多了.so的原因。
我最初用的方法是System.load,这个用.so和去掉.so的库都试过。
但目前现在还存在下面这个问题,以前用System.load有这个问题,改用了loadlibrary后没有了,以为问题解决了。实际上是倒退了!
希望再麻烦看看是什么原因。
编译库时用了-fPIC.若不用会编不过有错误如下:
linux-z03057:~/workspace/CtoJavaTest # g++ -I/home/z03057/src/jdk1.6.0/include -I/ home/z03057/src/jdk1.6.0/include/linux -o testdll.so -shared GetPid.c
/usr/lib64/gcc/x86_64-suse-linux/4.1.0/../../../../x86_64-suse-linux/bin/ld: /tmp/ ccq6CLL8.o:
relocation R_X86_64_32 against `__gxx_personality_v0' can not be used when making a shared object; recompile with -fPIC
/tmp/ccq6CLL8.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
linux-z03057:~/workspace/CtoJavaTest #
linux-z03057:~/workspace/CtoJavaTest # javac GetPid.java
linux-z03057:~/workspace/CtoJavaTest # g++ -I/home/z03057/src/jdk1.6.0/include - I/home/z03057/src/jdk1.6.0/include/linux -w -fPIC -DPIC -o testdll.so -shared Ge tPid.c
linux-z03057:~/workspace/CtoJavaTest # java GetPid
/home/z03057/src/jdk1.6.0/jre/lib/i386/client:/home/z03057/src/jdk1.6.0/jre/lib/ i386:/home/z03057/src/jdk1.6.0/jre/../lib/i386:.:/home/z03057/src/jdk1.6.0/jre/l ib/i386/client:/home/z03057/src/jdk1.6.0/jre/lib/i386:/lib:/usr/lib:/usr/java/pa ckages/lib/i386:/lib:/usr/lib
Exception in thread "main" java.lang.UnsatisfiedLinkError: /root/workspace/CtoJa vaTest/testdll.so: /root/workspace/CtoJavaTest/testdll.so: wrong ELF class: ELFC LASS64
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1751)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1676)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1030)
at GetPid.<clinit>(GetPid.java:5)