QuantLib using Swig to Java in MAC environment

Source: Internet
Author: User

First, Mac installation Boost method: http://blog.csdn.net/xujiezhige/article/details/8230493

Second, Swig, use sudo install Swig here

Third, how to convert a section C + + to Java:

Like having a example.c.

/*file:example.c*/#include<time.h>DoubleMy_variable =3.0; intFactintN) {if(N <=1)return 1; Else returnN*fact (n1); }  intMy_mod (intXinty) {return(%y); }      Char*Get_time () {time_t ltime; Time (&ltime); returnCTime (&ltime); } 

1. Now, in order to add these files to your favorite language, you need to write an interface file (interface files) into the swig. These C functions interface files may resemble the following:

/*example.i*/%Module Example%{ /*Put header files here or function declarations like below*/ extern Doublemy_variable;extern intFactintN); extern intMy_mod (intXinty); extern Char*get_time ();%}  extern Doublemy_variable;extern intFactintN); extern intMy_mod (intXinty); extern Char*get_time ();

2. Command line:

$ Swig-java EXAMPLE.I

If it is a C + + file

$ swig-c++-java example.i

There are 3 more files under directory:

Example.java , Examplejni.java, Example_wrap.cxx

3. Find the Java_home on your Mac, such as mine:

Library/java/javavirtualmachines/jdk1.8.0_77.jdk/contents/home/include

Command line: $

Cc-c Example.cpp example_wrap.cxx-i/library/java/javavirtualmachines/jdk1.8.0_77.jdk/contents/home/include/,

At this point will be an error that can not find the file Jni_md.h, find the include, at this time found that include a Darwin, the following jni_md.h copied to include below,

Then enter the above command line, through.

There are two more files under directory now:

EXAMPLE.O, EXAMPLE_WRAP.O

4. Enter the command line: cc-framework javavm-bundle example.o example_wrap.o-o libexample.jnilib

Generated a new file Libexample.jnilib, if not this file, directly Javac Main.java, will be error:

Exception in thread "main" java.lang.UnsatisfiedLinkError:no example in Java.library.path

At Java.lang.ClassLoader.loadLibrary (classloader.java:1867)

At Java.lang.Runtime.loadLibrary0 (runtime.java:870)

At Java.lang.System.loadLibrary (system.java:1122)

At Main.main (main.java:12)

5. Create a new Main.java under the folder, enter

 Public class Main {   publicstaticvoid  main (String argv[]) {     system.loadlibrary ( "Example");     System.out.println (Example.getmy_variable ());     System.out.println (Example.fact (5));     System.out.println (Example.get_time ());   } }

Note that if there is no system.loadlibrary ("example"); This sentence, will report the error:

Exception in thread "main" Java.lang.UnsatisfiedLinkError:exampleJNI.My_variable_get () D

At Examplejni.my_variable_get (Native Method)

At Example.getmy_variable (example.java:16)

At Main.main (main.java:13)

Input command line: $ javac Main.java, folder 3 more files: Example.class, Examplejni.class, Main.class

Input command line: $ java Main

Output:

3.0

120

Mon APR 25 10:56:18 2016

This is done using the Swig conversion to Java.

QuantLib using Swig to Java in MAC environment

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.