Issue
I am using NDK in my android app . There was no problem . Here is the code for c++ file
#include <jni.h>
#include <string>
#include <stdio.h>
extern "C" JNIEXPORT jstring JNICALL
Java_com_examples_core_MyApplication_getKeyJNI(
JNIEnv *env,
jobject /* this */) {
std::string secret_key = "mysecret";
return env->NewStringUTF(secret_key.c_str());
}
Edit
Here is my approach
my native-lib.cpp
#include <jni.h>
#include <string>
#include <unistd.h> // for getcwd()
#include <iostream>
#include <stdio.h>
#include "constants.h"
extern "C" JNIEXPORT jstring JNICALL
Java_com_examples_core_MyApplication_getKeyJNI(
JNIEnv *env,
jobject /* this */) {
std::string secret_key = secret_key;
return env->NewStringUTF(secret_key.c_str());
}
my constants.h
#pragma once
#include <string>
extern const std::string secret_key; // declaration
my constants.cpp
#include "constants.h"
const std::string secret_key = "mysecret"; // definition
When I compile I get the following error
native-lib.cpp:13: undefined reference to `secret_key'
Solution
You don't want to put the definition in a header file, as that could lead to multiple definitions of the same variable.
But you could do something like this:
constants.h
#pragma once
#include <string>
extern const std::string secret_key; // declaration
constants.cpp
#include "constants.h"
const std::string secret_key = "mysecret"; // definition
main.cpp
#include <iostream>
#include "constants.h"
int main()
{
std::cout << secret_key; // usage
}
Answered By - Michael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.