exception "error: expected ';' after top level declarator" from ndk-build - c++

OS:macOS 10.13.3
IDE:Android studio 1.5.1
g++:4.2.1
sceneļ¼š
I wanna develop with NDK using c++.When I define a c++ class to build,the Gradle Console return an error like below:
> src/main/jni/addcomputer.c:23:12: error: expected ';' after top level declarator
jclass Test{
^
;
1 error generated.
make: *** [build/intermediates/ndk/obj/local/arm64-v8a/objs/addcomputer/addcomputer.o] Error 1
:app:ndkBuild FAILED
and my .cpp like below:
#include "com_BTC_ui_JNI.h"
//......
JNIEXPORT jstring JNICALL
Java_com_BTC_ui_JNI_getString(JNIEnv *env, jobject instance)
{
return (*env)->NewStringUTF(env, "mytest-sample-x");
}
JNIEXPORT jint JNICALL
Java_com_BTC_ui_JNI_plus(JNIEnv *env, jobject instance, jint a, jint b)
{
return a+b;
}
jclass Test{//this is where the error occur
private:
jint mytest;
};
When I open a terminal to build a .cpp file which contain a class define,it return a same error.I cannot find the reason.Anyone can help me?

The error has gone , I just fixed some basic problem.Although the compile process is ok, the running time has occur another error of cannot find implementation of native method.I guess that's caused by compiler.Now I should close this question.Thanks all for focusing this question.

Related

error: pasting "->" and "object" does not give a valid preprocessing

I have the following macro:
#define FIELD_ACCESSOR_FUNCTIONS(typeName, fieldAccessorNamePrefix) \
JNIEXPORT jobject JNICALL my_pckg_NativeExecutor_get ## typeName ## FieldValue(JNIEnv* jNIEnv, jobject nativeExecutorInstance, jobject target, jobject field) { \
return environment-> ## fieldAccessorNamePrefix ## FieldAccessor->getValue(jNIEnv, target, field); \
}
FIELD_ACCESSOR_FUNCTIONS(Object, object)
And when I start the compilation I get the following error:
NativeExecutor.cpp:59:20: error: pasting "->" and "object" does not give a valid preprocessing token
return environment-> ## fieldAccessorNamePrefix ## FieldAccessor->getValue(jNIEnv, target, field); \
^~
How can I solve?
Drop the first ##. The name you are trying to generate is fieldAccessorNamePrefix ## FieldAccessor. The -> must not be part of the token.

error C2220: warning treated as error - no 'object' file generated in my code

I am getting the below error while exporting the dll
"error C2220: warning treated as error - no 'object' file generated"
Code:
extern "C" __declspec(dllexport) std::list<DBData> __cdecl FetchTable()
{
std::list<DBData> Test;
FetchData(id, Test);
return Test;
}

nvcc generating invalid error compiling JNI code

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:03_CDT_2017
Cuda compilation tools, release 9.0, V9.0.176
The error is:
MyFile.cu(231): error: expression must have pointer type
The relevant code:
JNIEXPORT jboolean JNICALL Java_MyFile_convergeMatrixCuda (
JNIEnv *env, jclass clazz, jfloatArray fxnMatrixJ, jfloatArray mulMatrixJ, jfloatArray addMatrixJ,
jfloatArray resultsJ, jint numRowsJ, jint numColsJ, jint maxIterations, jfloat epsilonJ)
{
int numRows = (int) numRowsJ;
int numCols = (int) numColsJ;
int maxIter = (int) maxIterations;
float epsilon = (float) epsilonJ;
float *fxnMatrixH = (*env)->GetFloatArrayElements (env, fxnMatrixJ, NULL);
GetFloatArrayElements returns a float*. Replacing "(*env)->GetFloatArrayElements" with "env->GetFloatArrayElements" gets these errors:
float *fxnMatrixH = env->GetFloatArrayElements (env, fxnMatrixJ, NULL);
MyFile.cu(231): error: argument of type "JNIEnv *" is incompatible with parameter of type "jfloatArray"
MyFile.cu(231): error: argument of type "jfloatArray" is incompatible with parameter of type "jboolean *"
MyFile.cu(231): error: too many arguments in function call
nvcc does work correctly when compiling non-JNI code
NVidia's documentation states that
Source files for CUDA applications consist of a mixture of conventional C++ host code, plus GPU device functions.
(*env)->GetFloatArrayElements (env, fxnMatrixJ, NULL); is the way you'd invoke a JNI function in C. But in C++ it would be env->GetFloatArrayElements(fxnMatrixJ, NULL);

PC Lint Error while using templates

When I run the PC lint application for the below mentioned code I get errors:
The errors for line (1) are
Error 129: declaration expected, identifier '__created' ignored
Error 10: Expecting identifier or other declarator
Error 129: declaration expected, identifier 'typename' ignored
template<typename T,UINT32 capacity> //(1)
class A
{
public:
A();
T *alloc();
T *free( T *ptr);
//Private members
private:
T *m_headPtr;
T m_pool[capacity];
}
How can I get rid of the errors?
Save the file as a C++ file with the correct ending or tell lint to treat it as such, currently, it seems to be treated as a C file.
Adding +fcp to the build command solved my problem

Why are function declarations returning bool not compiling in my C++ project?

I'm using Visual Studio 2008 Express Edition to compile the following code in a header file:
bool is_active(widget *w);
widget is defined earlier as,
typedef void widget;
The compiler complains with the error:
>c:\projects\engine\engine\engine.h(451) : error C2061: syntax error : identifier 'is_active'
1>c:\projects\engine\engine\engine.h(451) : error C2059: syntax error : ';'
1>c:\projects\engine\engine\engine.h(451) : error C2059: syntax error : 'type'
I get similar errors for all other functions returning bool.
NB. The following compiles fine:
void widget_activate_msg(widget *g, message *msg);
Why would this give a compiler error?
Some people have requested I post the code - here it is:
Line 449: widget * widget_new_from_resource(int resource_id);
Line 450: void widget_delete_one(widget *w);
Line 451: bool is_active(widget *w);
EDIT - this is now fixed:
#BatchyX commented below about whether I was using C or C++. What I didn't know was that Visual C++ 2008 will compile any file by default (but you can override this setting ) with the .c extension as C and those with .cpp as C++. ( the error was caused when compiling a .c file including "Engine.h" ).
Most likely, something above this line has a syntax error. Did you forget }s or ; after a class declaration ?
Also make sure you are using C++ and not C. C doesn't have a bool type. If you're using C, then use an int instead.
I'm guessing that it's not possible to typedef void. Why not use typdef void* WidgetPtr; and then bool is_active(WidgetPtr w);
EDIT: Having done some tests it's clear that void can be typedef'd and it can be part of the function signature as shown in the users code. So the only other solution is that whichever header has declared typedef void Widget is not included within the file that declares/defines the function or you're having a #def guard statement clash.