不能在 Android NDK 中包含像矢量这样的 C++ 标头 [英] Can't include C++ headers like vector in Android NDK

查看:20
本文介绍了不能在 Android NDK 中包含像矢量这样的 C++ 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在我的 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...

编译++ thumb : test-libstl <= test-libstl.cpp/Users/nitrex88/Desktop/Programming/EclipseProjects/STLTest/jni/test-libstl.cpp:3:18:错误:向量:没有这样的文件或目录

其他在线报告此问题的人通过添加声称成功

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 中包含像矢量这样的 C++ 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆