English 中文(简体)
无法在Android NDK中包含类似vector的C++标头
原标题:Can t include C++ headers like vector in Android NDK

当我试图在我的Android NDK项目中包含任何类似C++类的向量(使用最新的NDK r5b)时,我会收到如下错误。。。

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

其他在网上报告此问题的人通过添加

APP_STL:=stlport_static

到他们的Application.mk文件。我已经完成了这项工作,并尝试了APP_STL的所有其他可能值。我已经清理了项目,运行了ndk build clean,删除了obj和libs文件夹,但在编译时仍然找不到向量类。我已经为此工作了好几个星期了(自从NDK r5问世以来),如果有人能给我任何建议,我将不胜感激。谢谢

最佳回答

这是可能的。以下是一些循序渐进的步骤:

$PROJECT_DIR/jni/Application.mk中:

APP_STL                 := stlport_static

我试过使用stlport_shared,但没有成功。与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

$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。

注意目录。

要进行编译,请使用ndk-build-clean&&;ndk构建

问题回答

如果您正在使用Android studio,并且在使用ndk重新编译时仍然看到消息“error:vector:No such file or directory”(或其他与stl相关的错误),那么这可能会对您有所帮助。

在您的项目中,打开模块的build.gradle文件(不是您的项目的build.gade,而是用于您的模块的文件),并在defaultConfig中的ndk元素中添加stl“stlport_shared”。

例如:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.domain.app"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        ndk {
            moduleName "myModuleName"
            stl "stlport_shared"
        }
    }
}

我正在使用安卓工作室,截至2016年1月19日,这对我来说已经成功了。(这似乎每年都会发生变化)

转到:应用程序->;渐变脚本->;build.gradle(模块:应用程序)

然后在模型{…android.ndk{…下添加一行:stl=“gnustl_shared”

像这样:

model {

    ...

    android.ndk {
        moduleName = "gl2jni"
        cppFlags.add("-Werror")
        ldLibs.addAll(["log", "GLESv2"])
        stl = "gnustl_shared"     //  <-- this is the line that I added
    }

    ...

}

如果您使用的是ndk r10c或更高版本,只需将APP_STL=c++_static添加到Application.mk即可

即使塞巴斯蒂安在3年前给出了一个很好的答案,我仍然想在这里分享一个新的经验,以防你在新的ndk版本中面临和我一样的问题。

我有编译错误,例如:

fatal error: map: No such file or directory
fatal error: vector: No such file or directory

My environment is android-ndk-r9d and adt-bundle-linux-x86_64-20140702. I add Application.mk file in same jni folder and insert one (and only one) line:

APP_STL := stlport_static

But unfortunately, it doesn t solve my problem! I have to add these 3 lines into Android.mk to solve it:

ifndef NDK_ROOT
include external/stlport/libstlport.mk
endif

我看到了此处表示“首选stlport_shared”。因此,使用stlport作为共享库而不是静态库可能是更好的解决方案。只需将以下行添加到Android.mk中,然后无需添加文件Application.mk。

ifndef NDK_ROOT
include external/stlport/libstlport.mk
endif
LOCAL_SHARED_LIBRARIES += libstlport

希望这能有所帮助。

让我在塞巴斯蒂安·罗斯的回答

添加Sebastian发布的代码后,可以在命令行中使用ndk-build编译您的项目。但对我来说,Eclipse中存在语法错误,而且我没有完成代码。

请注意,您的项目必须转换为C/C++项目。

如何转换C/C++项目

若要解决此问题,请右键单击项目,单击属性

选择“C/C++常规”->路径和符号并包括${ANDROID_NDK}/sources/cxx-stl/stlport/stlport包含目录

出现对话框时,单击“是”。

之前

之后

更新#1

GNU C. Add directories, rebuild. There won t be any errors in C source files
GNU C++. Add directories, rebuild. There won t be any errors in CPP source files.

这就是在我的案例中导致问题的原因(CMakeLists.txt):

set (CMAKE_CXX_FLAGS "...some flags...")

It makes invisible all earlier defined include directories. After removing / refactoring this line everything works fine.

In android NDK go to android-ndk-r9b>/sources/cxx-stl/gnu-libstdc++/4.X/include in linux machines

I ve found solution from the below link http://osdir.com/ml/android-ndk/2011-09/msg00336.html

打开build.grade(模块)并进行一些编辑(删除一个字母并重新添加)。此操作栏将自动显示。现在,单击“确定,应用suugestion”。现在,在每个cpp和头文件中都修复了问题。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签