前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >建立一个跨平台可复用C++代码的实例工程(windows vs2017,android studio 可用)

建立一个跨平台可复用C++代码的实例工程(windows vs2017,android studio 可用)

作者头像
xiny120
发布2019-07-03 17:23:17
1.7K0
发布2019-07-03 17:23:17
举报
文章被收录于专栏:毛毛v5

一直想建立一个可复用c/c++代码的跨平台交叉编译的工程,特别是能分别使用vs2017,android studio , xcode 三种ide来在各自平台进行编码。这样就可以做到算法用c/c++多平台复用,平台特有api直接用nactive编码。最后再结合每个平台上的webview来实现界面web化,用h5来写界面。 这样一套就是,界面用H5,平台IO访问接口(除了文件IO,和socket io,因为c/c++代码完全可以进行文件io和socketio)用原生代码,算法用c/c++编写,做到写一次交叉编译到多个平台,节约编码时间和跨平台难度。 最近在github.com就发现了一个开源工程,实现了,用vs2017,anroid studio,xcode 协调编辑同一套代码,并分发不同平台的功能,但是他的as工程用的ndk build模式,as 3.1已经不推荐使用了,因为代码比较多,迁移起来好像狠麻烦,于是想自己做。得益于as迁移到用CMake来组织JNI工程代码,好像实现起来也狠顺利。 首先新建一个根目录 Shot4Read。三个平台的project都放在这个目录。 结构如下,其中Prj.Android 放的as 工程。Prj.Windows放的vs2017工程。ddmcore放的是可三端复用的c/c++代码。

代码语言:javascript
复制
H:\DEVTOOLS\project\git\Shot4Read>dir
 驱动器 H 中的卷没有标签。
 卷的序列号是 CC73-754C

 H:\DEVTOOLS\project\git\Shot4Read 的目录

2019-07-02  16:37    <DIR>          .
2019-07-02  16:37    <DIR>          ..
2019-07-02  18:45               419 .gitignore
2019-07-02  18:36    <DIR>          ddmcore
2019-06-28  16:54    <DIR>          Prj.Android
2019-07-02  17:04    <DIR>          Prj.Windows
               1 个文件            419 字节
               6 个目录 11,622,670,336 可用字节

在Shot4Read目录建好后,还是空的时候,先用vs2017建立一个MFC对话框工程放到Prj.Windows目录,然后再建立一个dll工程名字就是ddmcore,放到shot4read目录。这样windows工程就建立好了。 然后在shot4read目录建立一个as的jni工程,放到Prj.Android目录中。要使as能勾复用dmmcore中的代码,需要在cpp/CMakeLists.txt文件中改变如下:

代码语言:javascript
复制
# 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)
set(CMAKE_VERBOSE_MAKEFILE ON)

# 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.


#定义CASSDK位置 // 这里就是导入复用的c/c++代码目录。
if(NOT DDMCORE_DIR)
    set( DDMCORE_DIR ${CMAKE_SOURCE_DIR}/../../../../../ddmcore)
endif()
if( IS_DIRECTORY ${DDMCORE_DIR} )
    # Add header file include directories
    include_directories(${DDMCORE_DIR})#添加多目录的路径
    # Add block directories
    # 第二个cassdk.out参数用于指定外部文件夹在输出文件夹中的位置
    add_subdirectory( ${DDMCORE_DIR} cassdk.out)
else()
    message(FATAL_ERROR   "INVALID FOLDER 'CASSDK_DIR'=${DDMCORE_DIR}" )
endif()






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
        ddmcore-lib
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

然后还需要在ddmcore目录创建一个CMakeLists.txt,内容如下:

代码语言:javascript
复制
# 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)
set(CMAKE_VERBOSE_MAKEFILE ON)

# 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.




add_library( # Sets the name of the library.
        ddmcore-lib ##引入ddmcore库

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        apitest.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.
        ddmcore-lib #连接ddmcore-lib。
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

这两个CMakeLists.txt文件编辑好后,刷新as之后,可以发现as直接把ddmcore目录的代码自动包含到工程了。如下图:

AS工程刷新后的目录结构

至此就成功了。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.07.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档