1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NDKのC++関数を呼ぶにはextern "C"

Posted at

やはり長期休みに入らないと業務と関係ないジャンルのプログラムをやろうということにはなりません。やっとAndroidStudio開き始めてます。今参考にしているNDKのサンプルはCのコードでしたが、自分で拡張したC++関数をcから呼ぶようにcppに記述してもコンパイルが通りません。

エラー:(661) undefined reference to 'test_cplusplus_function'

エラーメッセージは「関数が未定義」だという直観的でないものでしたが、まあ普通に extern "C"を足して解決。

extern "C"
void test_cplusplus_function()
{
...

今度はjavaのコードからC++の関数を呼ぼうと書いたところビルドは通りますが、実行時に例外を吐くという。しょうがないのでextern "C"を記述すると無事動作。

extern "C"
void Java_com_test_ooo_JAVAFILE_TestFunc(JNIEnv* env, jclass clazz)
{
....
}

Windowsアプリでdll exportする際にextern "C"をつけるのはC++のままだとオーバーロードの仕組みによりインポートライブラリ内で関数名が変更されてしまうことを避けるためだったような話を思い出しました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?