Liunx Java-eclipse The call of the so library under C + + (next) __c++

Source: Internet
Author: User

Before the face of Linux to play so library is introduced.

This chapter, will be on the win platform compiled C + + packaging so library to introduce, in fact this and Linux under the same operation, why to win the introduction of the platform, because I was in the win platform packaging encountered problems, followed by I have not used Linux system, accustomed to win platform. Well, don't talk nonsense.

WIN32 Platform:

Preparatory work (version optional):

XP + JDK1.6

Cygwin the latest version of the installation: Ubunit,g++,gcc,gdb (which G++,GCC has risen to 4.5.3 version, the default installed is 3.4.4)

swig:2.0.4

The. h and. cpp and. I documents are set out in the Interfacecar.h and Interfacecar.cpp and cplus.i files in the central assembly.

Put the above files in the same directory, this directory is best not to use a space and the Chinese path directory. If you have Chinese you can use subst to map to other disk characters (hey, I usually do this).

or all use \ To turn the space to discuss such as/cygdrive/c/program\ files/.... The space in the middle is used \ to discuss

Ready to work OK.

1. Compile the. i file into a Java file using Swig. (My file path: f:\so\socplus)

[If you do not set the environment variable, you must first enter the CD into the Swig directory]

Swig-c++-java f:\so\socplus\cplus.i

will produce. Java and xxx_wrap.cxx files

With the Xxxx_wrap.cxx file, you can play the so library.

2. Compile the. cpp and cxx files into. o files first.

First CD into the appropriate directory, note Cygwin directory needs to use/cygdrive/for mapping

g++-wall-c interfacecar.cpp Cplus_wrap.cxx

Description

This command is not like under Linux, some people compared, how did not connect jni.h and Jni_md.h header file AH. The reason is because my JDK is installed under the D:\program Files\java, everybody notices this program files this directory has a space if use-i/cygdrive/d/program files/java/... You will be prompted not to find the files/java/.... Understand why you don't use the space path reason. Hey, don't be nervous, there's a solution. Search the Cygwin directory for jni.h and jni_md.h, and replace the jni.h and jni_md.h in your JDK (why replace it because my swig exported xxx_ Wrap.cxx is using JDK1.6 and my Cygwin is not in this version of the JNI, so conflict).

Error when jni.h is not found for JNI or version-incorrect:

Generates two. o files after compilation;

3. Packaging. o files as so library files.

g++-shared-fpic interfacecar.o Cplus_wrap.o-o libcp.so

This is no different from the Linux command.

OK, Package complete.

Above for win platform package complete.

Test:

All of the. So and. Java files are dropped to the Linux platform for testing. (It seems that you can also compile Java files into class files to play a Jar,jar CVF Xxx.jar *.class) This Java research itself.

There are several mistakes:

Java.lang.UnstaisfiedLinkError:no CP in Java.library.path

This is not the libcp.so to find this library. All I know is that it can be found under the/usr/lib, of course, the master of Eclipse can set the search path to the libcp.so corresponding path can also be found.

Because the packaged version and run version do not cause

Because my win platform and the virtual machine Linux platform on the different JDK, so there has been no test success. Readers are asked to verify their own.

Finally: sum up some points.

Be sure to use the JDK, version when packing. Run the environment version to the one with the packaged version, or the load will fail. (personally understood, not validated)

Secondly, is the path, environment variables need to be set up, otherwise there will be no library to find problems.

Again, if you are playing C + + so library, you need to do some necessary conversion on the type. This is just a preliminary introduction, in the actual project, will encounter a variety of problems, and then Google themselves.

OK, finished.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.