Android - C++ cygwin/android-ndk 中的编译错误
我创建了一个简单的应用程序来帮助我学习 ndk、jni,并使我的 C++ 技能恢复到标准水平。然而,我有点卡住了,因为当我从 cygwin 命令行在我的项目上运行 ndk-build 时,我遇到了一些编译错误。自从我在某种程度上使用 C++ 以来已经有一段时间了,所以我知道我的语法可能还有很大差距,所以如果有人可以帮助我清理这个问题或解释我如何做到这一点,那么那就太棒了......所以这里我的c++代码:
编辑:最新代码
#include <iostream>
#include <Eigen/Dense>
#include <math.h>
#include <jni.h>
using namespace Eigen;
Vector3f vec;
Vector3f vec2;
Vector3f vecRtrn;
void vecLoad(float x, float y, float z, float x2, float y2, float z2){
vec(0) = x;
vec(1) = y;
vec(2) = z;
vec2(0) = x2;
vec2(1) = y2;
vec2(2) = z2;
}
void vecAdd(Vector3f vecA, Vector3f vecB){
vecRtrn = vecA+vecB;
}
extern "C"
{
JNIEXPORT jfloatArray JNICALL Java_jnimath_act_JnimathActivity_test
(JNIEnv *env, jobject obj, jfloatArray fltarray1, jfloatArray fltarray2){
float array1[3];
jfloatArray flt1 = fltarray1;
jfloatArray flt2 = fltarray2;
//flt1 = env->GetFloatArrayElements( fltarray1,0);
//flt2 = env->GetFloatArrayElements( fltarray2,0);
vecLoad(flt1[0], flt1[1], flt1[2], flt2[0], flt2[1], flt2[2]);
vecAdd(vec, vec2);
array1[0] = vecRtrn[0];
array1[1] = vecRtrn[1];
array1[2] = vecRtrn[2];
return array1;
};
}
现在我很确定我没有使用正确的方法通过jni将数组返回到java,但这是一个单独的问题……但也请随意对此发表评论;)
这是我最后几个构建错误的更新列表......它绝对是比原始列表短很多:)
$ /cygdrive/c/android-ndk-r7/ndk-build
Compile++ thumb : test <= test.cpp
jni/test.cpp: In function '_jfloatArray* Java_jnimath_act_JnimathActivity_test(JNIEnv*, _jobject*, _jfloatArray*, _jfloatArray*)':
jni/test.cpp:42: error: cannot convert '_jfloatArray' to 'float' for argument '1' to 'void vecLoad(float, float, float, float, float, float)'
jni/test.cpp:49: error: cannot convert 'float*' to '_jfloatArray*' in return
make: *** [obj/local/armeabi/objs/test/test.o] Error 1
编辑:这是我的 java 代码......
package jnimath.act;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class JnimathActivity extends Activity {
/** Called when the activity is first created. */
public EditText x;
public EditText y;
public EditText z;
public EditText x2;
public EditText y2;
public EditText z2;
public float[] vecArray;
public TextView textView1;
public Button run;
float[] array3 = new float[3];
float[] array1 = new float[3];
float[] array2 = new float[3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
x = (EditText)findViewById(R.id.x);
y = (EditText)findViewById(R.id.y);
z = (EditText)findViewById(R.id.z);
x2 = (EditText)findViewById(R.id.x);
y2 = (EditText)findViewById(R.id.y);
z2 = (EditText)findViewById(R.id.z);
textView1 = (TextView)findViewById(R.id.textView1);
run = (Button)findViewById(R.id.run);
run.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
array1[0] = Float.parseFloat(x.getText().toString());
array1[1] = Float.parseFloat(y.getText().toString());
array1[2] = Float.parseFloat(z.getText().toString());
array2[0] = Float.parseFloat(x2.getText().toString());
array2[1] = Float.parseFloat(y2.getText().toString());
array2[2] = Float.parseFloat(z2.getText().toString());
array3 = test(array1, array2);
String text = array3[0]+" "+array3[1]+" "+array3[2];
textView1.setText(text);
}
});
}
public native float[] test(float[] array1, float[] array2);
static {
System.loadLibrary("test");
}
}
Ive created a simple app to help me learn the ndk, jni, and get my c++ skills back up to par. However Im kind of stuck because Im getting some compile errors when I run ndk-build on my project from the cygwin command line. Its been a while since Ive used c++ to any extent so I know that my syntax is probably way off so if some one could help me clean this up or explain how I could do so then that would be awesome.....so heres my c++ code:
EDIT: latest code
#include <iostream>
#include <Eigen/Dense>
#include <math.h>
#include <jni.h>
using namespace Eigen;
Vector3f vec;
Vector3f vec2;
Vector3f vecRtrn;
void vecLoad(float x, float y, float z, float x2, float y2, float z2){
vec(0) = x;
vec(1) = y;
vec(2) = z;
vec2(0) = x2;
vec2(1) = y2;
vec2(2) = z2;
}
void vecAdd(Vector3f vecA, Vector3f vecB){
vecRtrn = vecA+vecB;
}
extern "C"
{
JNIEXPORT jfloatArray JNICALL Java_jnimath_act_JnimathActivity_test
(JNIEnv *env, jobject obj, jfloatArray fltarray1, jfloatArray fltarray2){
float array1[3];
jfloatArray flt1 = fltarray1;
jfloatArray flt2 = fltarray2;
//flt1 = env->GetFloatArrayElements( fltarray1,0);
//flt2 = env->GetFloatArrayElements( fltarray2,0);
vecLoad(flt1[0], flt1[1], flt1[2], flt2[0], flt2[1], flt2[2]);
vecAdd(vec, vec2);
array1[0] = vecRtrn[0];
array1[1] = vecRtrn[1];
array1[2] = vecRtrn[2];
return array1;
};
}
Now Im pretty sure Im not using the correct way to return the array back to java through jni but thats a separate problem......but please feel free to comment on that as well ;)
Heres an updated list of the last couple of build errors I have.....its definitely a lot shorter than the original list :)
$ /cygdrive/c/android-ndk-r7/ndk-build
Compile++ thumb : test <= test.cpp
jni/test.cpp: In function '_jfloatArray* Java_jnimath_act_JnimathActivity_test(JNIEnv*, _jobject*, _jfloatArray*, _jfloatArray*)':
jni/test.cpp:42: error: cannot convert '_jfloatArray' to 'float' for argument '1' to 'void vecLoad(float, float, float, float, float, float)'
jni/test.cpp:49: error: cannot convert 'float*' to '_jfloatArray*' in return
make: *** [obj/local/armeabi/objs/test/test.o] Error 1
EDIT: heres my java code......
package jnimath.act;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class JnimathActivity extends Activity {
/** Called when the activity is first created. */
public EditText x;
public EditText y;
public EditText z;
public EditText x2;
public EditText y2;
public EditText z2;
public float[] vecArray;
public TextView textView1;
public Button run;
float[] array3 = new float[3];
float[] array1 = new float[3];
float[] array2 = new float[3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
x = (EditText)findViewById(R.id.x);
y = (EditText)findViewById(R.id.y);
z = (EditText)findViewById(R.id.z);
x2 = (EditText)findViewById(R.id.x);
y2 = (EditText)findViewById(R.id.y);
z2 = (EditText)findViewById(R.id.z);
textView1 = (TextView)findViewById(R.id.textView1);
run = (Button)findViewById(R.id.run);
run.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
array1[0] = Float.parseFloat(x.getText().toString());
array1[1] = Float.parseFloat(y.getText().toString());
array1[2] = Float.parseFloat(z.getText().toString());
array2[0] = Float.parseFloat(x2.getText().toString());
array2[1] = Float.parseFloat(y2.getText().toString());
array2[2] = Float.parseFloat(z2.getText().toString());
array3 = test(array1, array2);
String text = array3[0]+" "+array3[1]+" "+array3[2];
textView1.setText(text);
}
});
}
public native float[] test(float[] array1, float[] array2);
static {
System.loadLibrary("test");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如错误所述,编译器不喜欢 vecLoad 中的内容。您确定 Vector3f 有正确的“(”运算符吗?我会先检查一下。
As the error states, compiler does not like what you have in vecLoad. Are you sure Vector3f has correct '(' operator? I would check that first.
第一个错误是由于 net 指定了 float array1[]; 的大小,与 java 不同,这是不允许的。另一方面,您可以将数组指定为
float *array1;
而不对其进行初始化。第二个和第三个错误是指您在使用
->
运算符之前取消引用指针。正确的方法是:最后一个错误是您缺少一个数字,看起来应该是
flt2[2]
而不是flt[2]
。The first error is due to net specifying a size for
float array1[];
Unlike java this isn't allowed. You can on the other hand specify the array asfloat *array1;
without initializing it.The second and third error refer to you dereferencing your pointer before using the
->
operator. The correct way to do it would be:The last error you are just missing a number it looks like it should be
flt2[2]
instead offlt[2]
.