不能在 Android NDK 中包含像 vector 这样的 C++ 头文件 [英] Can't include C++ headers like vector in Android NDK
问题描述
当我尝试在我的 Android NDK 项目(使用 NDK r5b,最新版本)中包含任何 C++ 类(如 vector)时,我收到如下错误...
When I try to include any C++ class like vector in my Android NDK project (using NDK r5b, the latest), I get an error like the following...
Compile++ thumb : test-libstl <= test-libstl.cpp/Users/nitrex88/Desktop/Programming/EclipseProjects/STLTest/jni/test-libstl.cpp:3:18: error: vector: No such file or directory
其他在网上报告此问题的人声称添加成功
Other people who reported this issue online have claimed success by adding
APP_STL := stlport_static
到他们的 Application.mk 文件.我已经这样做了,并尝试了 APP_STL 的所有其他可能值.我已经清理到项目,运行 ndk-build clean,删除了 obj 和 libs 文件夹,但在我编译时仍然找不到向量类.我已经为此工作了数周(自 NDK r5 发布以来),如果有人有任何建议,我将不胜感激.谢谢!
to their Application.mk file. I have done this as well as tried every other possible value for APP_STL. I've cleaned to project, ran ndk-build clean, deleted the obj and libs folders, and still when I compile it cannot find the vector class. I've been working on this for a number of weeks now (since NDK r5 came out) and would really appreciate if someone has any advice. Thanks!
推荐答案
这是可能的.下面是一些步骤:
It is possible. Here is some step by step:
在$PROJECT_DIR/jni/Application.mk:
APP_STL := stlport_static
我尝试使用 stlport_shared,但没有成功.与 libstdc++ 相同.
I tried using stlport_shared, but no luck. Same with libstdc++.
在$PROJECT_DIR/jni/Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
这里没什么特别的,但请确保您的文件是 .cpp.
Nothing special here, but make sure your files are .cpp.
在$PROJECT_DIR/jni/hello-jni.cpp:
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <iostream>
#include <vector>
#define LOG_TAG "hellojni"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#ifdef __cplusplus
extern "C" {
#endif
// Comments omitted.
void
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
std::vector<std::string> vec;
// Go ahead and do some stuff with this vector of strings now.
}
#ifdef __cplusplus
}
#endif
这里唯一让我印象深刻的是#ifdef __cplusplus.
The only thing that bite me here was #ifdef __cplusplus.
观看目录.
要编译,请使用 ndk-build clean &&ndk-build
.
这篇关于不能在 Android NDK 中包含像 vector 这样的 C++ 头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!