33、静态编译boost源码,可提供Android的c++开发模块使用

基本思想:本来项目需求,需要写Android代码,开发组件模块,因为极其讨厌java;顾转而使用Android studio 提供了c++的功能,开发功能模块;之前的代码使用了boost库,所以需要使用boost源码静态编译成静态包,然后导入Android studio工程使用;

以PC机为例子写了本教程;

1、下载源代码

axel -n 100 https://dl.bintray.com/boostorg/release/1.73.0/source/boost_1_73_0.tar.bz2
tar -jxvf boost_1_73_0.tar.bz2 
cd boost_1_73_0/
mkdir liboost
./bootstrap.sh
./b2 -a  cxxflags='-fPIC -std=c++11' install --prefix=/home/ubuntu/boost_1_73_0/liboost link=static threading=multi
 

然后在liboost文件夹产生了对应的头文件include和lib静态包

ubuntu@ubuntu:~/boost_1_73_0/liboost$ tree -L 2
.
├── include
│   └── boost
└── lib
    ├── cmake
    ├── libboost_atomic.a
    ├── libboost_chrono.a
    ├── libboost_container.a
    ├── libboost_context.a
    ├── libboost_contract.a
    ├── libboost_coroutine.a
    ├── libboost_date_time.a
    ├── libboost_exception.a
    ├── libboost_fiber.a
    ├── libboost_filesystem.a
    ├── libboost_graph.a
    ├── libboost_iostreams.a
    ├── libboost_locale.a
    ├── libboost_log.a
    ├── libboost_log_setup.a
    ├── libboost_math_c99.a
    ├── libboost_math_c99f.a
    ├── libboost_math_c99l.a
    ├── libboost_math_tr1.a
    ├── libboost_math_tr1f.a
    ├── libboost_math_tr1l.a
    ├── libboost_nowide.a
    ├── libboost_prg_exec_monitor.a
    ├── libboost_program_options.a
    ├── libboost_python27.a
    ├── libboost_random.a
    ├── libboost_regex.a
    ├── libboost_serialization.a
    ├── libboost_stacktrace_addr2line.a
    ├── libboost_stacktrace_backtrace.a
    ├── libboost_stacktrace_basic.a
    ├── libboost_stacktrace_noop.a
    ├── libboost_system.a
    ├── libboost_test_exec_monitor.a
    ├── libboost_thread.a
    ├── libboost_timer.a
    ├── libboost_type_erasure.a
    ├── libboost_unit_test_framework.a
    ├── libboost_wave.a
    └── libboost_wserialization.a

4 directories, 40 files

然后就原则上可以把这个liboost文件夹导入Android Studio 的工程中使用静态库了;

测试代码:

#include <boost/date_time/gregorian/gregorian.hpp> 
#include <iostream> 
using namespace std;
int main() 
{ 
    boost::gregorian::date d(boost::gregorian::day_clock::local_day());
    cout << d.year()<<"." << d.month()<<"." <<d.day() <<endl; 
    return 0;
}

编译与执行:

ubuntu@ubuntu:~/test$ g++ -I liboost/include/ liboost/lib/libboost_timer.a  testliboost.cpp 
ubuntu@ubuntu:~/test$ ./a.out 
2020.Aug.4
ubuntu@ubuntu:~/test$ g++ -I liboost/include/ -I liboost/lib/  testliboost.cpp 
ubuntu@ubuntu:~/test$ ./a.out 
2020.Aug.4

如果使用CMakeList.txt进行编译的;需要将生成的文件夹拷贝到工程中然后修改对应的CMakeList.txt文件,文件结构和修改点如下:

然后即可以使用pc端编译的liboost库;

移植Android 端使用交叉编译的liboost库:

需要使用交叉编译器去编译liboost的的源码,我为了方便并未使用交叉编译器去编译https://blog.csdn.net/sxj731533730/article/details/106115331,;{ 进入目录执行./bootstrap.sh;

 此时形成bjam文件和project-config.jam 编辑project-config.jam, 仅修改using gcc这行。因为我使用的是arm-none-linux-gnueabi-g++,所以将其改以下即可: using gcc : arm  : arm-none-linux-gnueabi-g++; (注意空格和冒号)}

修改文件project-config.jam

# Boost.Build Configuration
# Automatically generated by bootstrap.sh

import option ;
import feature ;

# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
if ! gcc in [ feature.values <toolset> ]
{
    using gcc : : /usr/local/arm-linux-androideabi/bin/arm-linux-androideabi-gcc ; 
}

project : default-build <toolset>gcc ;

# Python configuration
import python ;
if ! [ python.configured ]
{
    using python : 2.7 : "/usr/local/arm-linux-androideabi" ;
}

path-constant ICU_PATH : /usr ;


# List of --with-<library> and --without-<library>
# options. If left empty, all libraries will be built.
# Options specified on the command line completely
# override this variable.
libraries =  ;

# These settings are equivalent to corresponding command-line
# options.
option.set prefix : /usr/local ;
option.set exec-prefix : /usr/local ;
option.set libdir : /usr/local/lib ;
option.set includedir : /usr/local/include ;

# Stop on first error
option.set keep-going : false ;

对应交叉编译命令为:

 ./b2 --without-python --without-mpi --without-serialization link=static runtime-link=static target-os=linux --stagedir=android  install --prefix=/home/ubuntu/Downloads/boost_1_73_0/android

文件结构为(预先创建android目录):

ubuntu@ubuntu:~/Downloads/boost_1_73_0/android$ tree -L 2
.
├── include
│   └── boost
└── lib
    ├── cmake
    ├── libboost_atomic.a
    ├── libboost_chrono.a
    ├── libboost_container.a
    ├── libboost_context.a
    ├── libboost_contract.a
    ├── libboost_coroutine.a
    ├── libboost_date_time.a
    ├── libboost_exception.a
    ├── libboost_fiber.a
    ├── libboost_filesystem.a
    ├── libboost_graph.a
    ├── libboost_iostreams.a
    ├── libboost_log.a
    ├── libboost_log_setup.a
    ├── libboost_math_c99.a
    ├── libboost_math_c99f.a
    ├── libboost_math_c99l.a
    ├── libboost_math_tr1.a
    ├── libboost_math_tr1f.a
    ├── libboost_math_tr1l.a
    ├── libboost_nowide.a
    ├── libboost_prg_exec_monitor.a
    ├── libboost_program_options.a
    ├── libboost_random.a
    ├── libboost_regex.a
    ├── libboost_stacktrace_basic.a
    ├── libboost_stacktrace_noop.a
    ├── libboost_system.a
    ├── libboost_test_exec_monitor.a
    ├── libboost_thread.a
    ├── libboost_timer.a
    ├── libboost_type_erasure.a
    ├── libboost_unit_test_framework.a
    └── libboost_wave.a

4 directories, 34 files    

修改build.gradle文件,将交叉编译的lib文件和include的头文件复制到对应的Android Studio的文件夹位置;

修改CMakeList.txt文件

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

include_directories(${CMAKE_SOURCE_DIR}/include)

# 导入opencv的so
add_library(boost_date_time STATIC IMPORTED)
set_target_properties(boost_date_time PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/../jniLibs/libs/${ANDROID_ABI}/lib/libboost_date_time.a)


add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib
        boost_date_time
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

对应的native-lib.cpp文件

#include <jni.h>
#include <string>
#include <iostream>

#include <boost/date_time/gregorian/gregorian.hpp>


using namespace std;
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_boost_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {


    boost::gregorian::date d(boost::gregorian::day_clock::local_day());
    cout <<"year"<< d.year()<<"." << d.month()<<"." <<d.day() <<endl;

    std::string hello = "Hello from C++1 2 3 4 ";
    return env->NewStringUTF(hello.c_str());
}

测试结果为,给个Android手机特写~~ 哈哈哈哈

猜你喜欢

转载自blog.csdn.net/sxj731533730/article/details/107800057