在Android NDK项目添加cpp文件,并引用C++标准库头文件:
#include <string>
#include <iostream>
使用ndk-build编译报如下错误:
fatal error: 'string' file not found
fatal error: 'iostream' file not found
原因:在使用C++的标准库进行NDK开发时,必须设置APP_STL
Application.mk解决方案
在jni文件夹下新建Application.mk文件,并加入:
#以下两行二选一
APP_STL := stlport_static
#APP_STL := gnustl_static
Gradle解决方案
android.defaultConfig.ndk {
moduleName = "hello-jni"
stl = "stlport_static"
}
APP_STL
APP_STL可取范围:https://developer.android.com/ndk/guides/cpp-support#c_runtime_libraries
不同的标准库能力也各有差异,见下表
| APP_STL | C++ Exceptions | C++ RTTI | Standard Library |
|---|---|---|---|
| system | no | no | no |
| gabi++ | no | yes | no |
| stlport | no | yes | yes |
| gnustl | yes | yes | yes |
特别注意的是,引用的类库越全面,最后生成的库文件体积也越大。




