Android framework中使用stl庫

yingxian_Fei發表於2016-12-19

在Android的framework或者ndk中使用stl庫中的方法,如std::string 時需要修改Android.mk檔案新增相應的庫支援。

主要步驟如下:

   LOCAL_SHARED_LIBRARIES中新增libstlport;

   LOCAL_C_INCLUDES中新增    external/stlport/stlport  bionic  bionic/libc/private


如下為本文修改的方法:

# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES := test.cpp

LOCAL_SHARED_LIBRARIES := \
    libandroid_runtime \
    libcutils \
    libutils \
    liblog  \
    libnativehelper \
    libstlport  \
    
LOCAL_C_INCLUDES += \
    $(JNI_H_INCLUDE) \
    external/stlport/stlport \
    bionic \
    bionic/libc/private \
    
      
LOCAL_MODULE:= liblsphone

include $(BUILD_SHARED_LIBRARY)

使用demo

在cpp中使用std標準庫中的string類時需要注意如下步驟:

1.匯入標頭檔案

   #include <string>

2.新增類的引用名稱空間

   using namespace std;

相關文章