Skip to content

Instantly share code, notes, and snippets.

@ymurase
Last active August 29, 2015 14:12
Show Gist options
  • Save ymurase/3f703809ab51e7876fb8 to your computer and use it in GitHub Desktop.
Save ymurase/3f703809ab51e7876fb8 to your computer and use it in GitHub Desktop.
C++で定義された関数を呼ぶ方法。
import x10.io.Console;
import x10.compiler.Native;
import x10.compiler.NativeCPPInclude;
import x10.compiler.NativeCPPCompilationUnit;
@NativeCPPInclude("MyCpp.hpp")
@NativeCPPCompilationUnit("MyCpp.cpp")
public class NativeCppTest {
@Native("c++", "my_double(#1)")
native static def double(n:Int): Int;
public static def main( args: Rail[String] ) {
{
@Native("c++", "foo();") {}
}
val n = 30n;
val n2 = double(n); // 返り値もOK
Console.OUT.println("n2: " + n2);
}
}
@ymurase
Copy link
Author

ymurase commented Dec 25, 2014

@NativeCPPInclude でヘッダファイル、 @NativeCPPCompilationUnit で実装をそれぞれ指定する。
少なくともprimitive型は返り値にできる。ただし型の推論はできないので、Intが返ってくることを明示する必要がある。
C++のクラスや構造体を返すことができるかどうかはわからない。

@ymurase
Copy link
Author

ymurase commented Dec 25, 2014

外部のライブラリなどをリンクする場合、次のようにして -post オプション内でC++のコンパイル方法を指定する 。
x10c++ Test.x10 -post '# # -I /usr/local/blas # -L /usr/local/blas -lblas'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment