NDK編譯protobuf-3.12.0-rc2

今天講一下NDK編譯protobuf-3.12.0-rc2:

下載android-ndk-r13b-windows-x86_64.zip:https://dl.google.com/android/repository/android-ndk-r13b-windows-x86_64.zip?utm_source=androiddevtools&utm_medium=website,解壓到::C:/android/android-ndk-r13b

下載protobuf-3.12.0-rc2:https://github.com/protocolbuffers/protobuf/releases/,解壓到:c:/protobuf-3.12.0-rc2

一。添加NDK環境變量:

我的NDK安裝路徑爲:C:/android/android-ndk-r13b,因爲在CMD下編譯,要運行 ndk-build命令,所有要加2個系統環境變量:

cocos2d-x要的變量爲:NDK_ROOT

ndk-build命令環境變量在Path中加:%NDK_ROOT%\build 

二.在c:/protobuf-3.12.0-rc2/src 建立make文件(文件名注意大小寫):

1.application.mk

APP_MODULES      := protobuf
APP_PLATFORM     := android-19
APP_ABI          := armeabi-v7a
APP_STL := c++_static
APP_OPTIM        := release

2.android.mk

LOCAL_PATH := $(call my-dir)
 
include $(CLEAR_VARS)
 
LOCAL_MODULE := protobuf
LOCAL_CFLAGS := -std=c++11 -fexceptions -frtti
LOCAL_MODULE_FILENAME := libprotobuf
LOCAL_SRC_FILES :=  google/protobuf/stubs/bytestream.cc                          \
  google/protobuf/stubs/bytestream.h                           \
  google/protobuf/stubs/common.cc                              \
  google/protobuf/stubs/hash.h                                 \
  google/protobuf/stubs/int128.cc                              \
  google/protobuf/stubs/int128.h                               \
  google/protobuf/io/io_win32.cc                               \
  google/protobuf/stubs/map_util.h                             \
  google/protobuf/stubs/mathutil.h                             \
  google/protobuf/stubs/status.cc                              \
  google/protobuf/stubs/status.h                               \
  google/protobuf/stubs/status_macros.h                        \
  google/protobuf/stubs/statusor.cc                            \
  google/protobuf/stubs/statusor.h                             \
  google/protobuf/stubs/stringpiece.cc                         \
  google/protobuf/stubs/stringpiece.h                          \
  google/protobuf/stubs/stringprintf.cc                        \
  google/protobuf/stubs/stringprintf.h                         \
  google/protobuf/stubs/structurally_valid.cc                  \
  google/protobuf/stubs/strutil.cc                             \
  google/protobuf/stubs/time.cc                                \
  google/protobuf/stubs/time.h                                 \
  google/protobuf/any_lite.cc                                  \
  google/protobuf/arena.cc                                     \
  google/protobuf/extension_set.cc                             \
  google/protobuf/generated_enum_util.cc                       \
  google/protobuf/generated_message_util.cc                    \
  google/protobuf/generated_message_table_driven_lite.h        \
  google/protobuf/generated_message_table_driven_lite.cc       \
  google/protobuf/implicit_weak_message.cc                     \
  google/protobuf/message_lite.cc                              \
  google/protobuf/parse_context.cc                             \
  google/protobuf/repeated_field.cc                            \
  google/protobuf/wire_format_lite.cc                          \
  google/protobuf/io/coded_stream.cc                           \
  google/protobuf/io/strtod.cc                                 \
  google/protobuf/io/zero_copy_stream.cc                       \
  google/protobuf/io/zero_copy_stream_impl.cc                  \
  google/protobuf/io/zero_copy_stream_impl_lite.cc		\
  google/protobuf/any.pb.cc                                    \
  google/protobuf/api.pb.cc                                    \
  google/protobuf/any.cc                                       \
  google/protobuf/descriptor.cc                                \
  google/protobuf/descriptor_database.cc                       \
  google/protobuf/descriptor.pb.cc                             \
  google/protobuf/duration.pb.cc                               \
  google/protobuf/dynamic_message.cc                           \
  google/protobuf/empty.pb.cc                                  \
  google/protobuf/extension_set_heavy.cc                       \
  google/protobuf/field_mask.pb.cc                             \
  google/protobuf/generated_message_reflection.cc              \
  google/protobuf/generated_message_table_driven_lite.h        \
  google/protobuf/generated_message_table_driven.cc            \
  google/protobuf/map_field.cc                                 \
  google/protobuf/message.cc                                   \
  google/protobuf/reflection_internal.h                        \
  google/protobuf/reflection_ops.cc                            \
  google/protobuf/service.cc                                   \
  google/protobuf/source_context.pb.cc                         \
  google/protobuf/struct.pb.cc                                 \
  google/protobuf/stubs/substitute.cc                          \
  google/protobuf/stubs/substitute.h                           \
  google/protobuf/text_format.cc                               \
  google/protobuf/timestamp.pb.cc                              \
  google/protobuf/type.pb.cc                                   \
  google/protobuf/unknown_field_set.cc                         \
  google/protobuf/wire_format.cc                               \
  google/protobuf/wrappers.pb.cc                               \
  google/protobuf/io/gzip_stream.cc                            \
  google/protobuf/io/printer.cc                                \
  google/protobuf/io/tokenizer.cc                              \
  google/protobuf/compiler/importer.cc                         \
  google/protobuf/compiler/parser.cc                           \
  google/protobuf/util/delimited_message_util.cc               \
  google/protobuf/util/field_comparator.cc                     \
  google/protobuf/util/field_mask_util.cc                      \
  google/protobuf/util/internal/constants.h                    \
  google/protobuf/util/internal/datapiece.cc                   \
  google/protobuf/util/internal/datapiece.h                    \
  google/protobuf/util/internal/default_value_objectwriter.cc  \
  google/protobuf/util/internal/default_value_objectwriter.h   \
  google/protobuf/util/internal/error_listener.cc              \
  google/protobuf/util/internal/error_listener.h               \
  google/protobuf/util/internal/expecting_objectwriter.h       \
  google/protobuf/util/internal/field_mask_utility.cc          \
  google/protobuf/util/internal/field_mask_utility.h           \
  google/protobuf/util/internal/json_escaping.cc               \
  google/protobuf/util/internal/json_escaping.h                \
  google/protobuf/util/internal/json_objectwriter.cc           \
  google/protobuf/util/internal/json_objectwriter.h            \
  google/protobuf/util/internal/json_stream_parser.cc          \
  google/protobuf/util/internal/json_stream_parser.h           \
  google/protobuf/util/internal/location_tracker.h             \
  google/protobuf/util/internal/mock_error_listener.h          \
  google/protobuf/util/internal/object_location_tracker.h      \
  google/protobuf/util/internal/object_source.h                \
  google/protobuf/util/internal/object_writer.cc               \
  google/protobuf/util/internal/object_writer.h                \
  google/protobuf/util/internal/protostream_objectsource.cc    \
  google/protobuf/util/internal/protostream_objectsource.h     \
  google/protobuf/util/internal/protostream_objectwriter.cc    \
  google/protobuf/util/internal/protostream_objectwriter.h     \
  google/protobuf/util/internal/proto_writer.cc                \
  google/protobuf/util/internal/proto_writer.h                 \
  google/protobuf/util/internal/structured_objectwriter.h      \
  google/protobuf/util/internal/type_info.cc                   \
  google/protobuf/util/internal/type_info.h                    \
  google/protobuf/util/internal/type_info_test_helper.cc       \
  google/protobuf/util/internal/type_info_test_helper.h        \
  google/protobuf/util/internal/utility.cc                     \
  google/protobuf/util/internal/utility.h                      \
  google/protobuf/util/json_util.cc                            \
  google/protobuf/util/message_differencer.cc                  \
  google/protobuf/util/time_util.cc                            \
  google/protobuf/util/type_resolver_util.cc

 
 
LOCAL_EXPORT_C_INCLUDES :=
LOCAL_EXPORT_LDLIBS :=
 
LOCAL_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/src
 
LOCAL_LDLIBS := -llog -lz
 
#include $(BUILD_SHARED_LIBRARY)
include $(BUILD_STATIC_LIBRARY)

 

裏面包含的文件很多,其實要包含的文件都在Makefile.am文件中:

有兩個爲 libprotobuf_lite_la_SOURCES 的文件全部包含進來。還要注意的是,我的APP_PLATFORM     := android-19,必須要下載 android-19 SDK,Android  SDK 可在https://www.androiddevtools.cn/index.html下載。

準備好上面現在開始編譯:

Win+R  執行 cmd命令,進入c:/protobuf-3.12.0-rc2/src目錄 輸入:

ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./android.mk NDK_APPLICATION_MK=./application.mk

 

會出現以下錯誤:

 

這個要修改一下C:/protobuf-3.12.0-rc2/src/google/protobuf/stubs/common.h文件就行了

打開加入下面內容:

#ifndef HAVE_PTHREAD
#define HAVE_PTHREAD
#endif

 

再次執行上次命令,就可以編譯了:

 

這樣就編譯好了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章