ポイントは関数にextern "C" をつけること.
C++では、関数のオーバーロード(多重定義)などが原因で
関数名で関数が特定できない(識別子がつくらしい)。
そこで、C方式の関数名として宣言しておく.
http://www.geocities.jp/ky_webid/cpp/language/038.html
http://ja.softuses.com/112083
また,コンパイルオプションがCの場合と少し違う
ライブラリがないといわれるので、staticオプションをつけてライブラリとリンクしとく
http://convenient.blog106.fc2.com/blog-entry-764.html
cpp_test.scala
import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.NativeLibrary; object cpp_test2{ def main(args: Array[String]): Unit={ val lib=NativeLibrary.getInstance("cpp_test2.dll"); val func=lib.getFunction("test1"); func.invokeVoid(Array(a,a.length.asInstanceOf[java.lang.Integer])) } }
cpp_test2.cpp
#include<iostream> using namespace std; extern "C" void test1(){ cout << "hello22\n"; }
cpp_test2.h
#ifndef cpp_test2 #define cpp_test2 #include<iostream> using namespace std; extern "C" void test1(); #endif
コンパイル dll
filename=$1; filename=`basename $filename .cpp` #filename2=`basename $2 .cpp` echo $filename; #echo $filename2; x86_64-w64-mingw32-g++ -c $filename.cpp -static-libstdc++ -static-libgcc echo "test" x86_64-w64-mingw32-g++ -shared -o $filename.dll $filename.o -static-libstdc++ -static-libgcc #echo "test2"; #x86_64-w64-mingw32-g++ $filename2.cpp -o $filename2.out -L. -l$filename -static-libstdc++ -static-libgcc
実行結果
hello22