zoukankan      html  css  js  c++  java
  • CMake : 递归的添加所有cpp文件

    最近在使用android studio的时候又发现了一个非常不方便的地方:

    每次创建新的C++文件,都需要手动把新创建的C++文件名添加到CMakeLists.txt文件中,不然的话编辑器的顶部就会一直报:

    目前我的项目结构如下:

     对应的CMakeLists.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)
    
    # Add header file path
    # include_directories(src/main/cpp/include)
    
    # 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.
            HaAlgorithm
    
            # Sets the library as a shared library.
            SHARED
    
            src/main/cpp/HaAptUserTuneJNI.cpp
            src/main/cpp/AndroidLog.cpp
    
            # Algorithm
            src/main/cpp/HaAlgorithm/apt_tune.cpp
            src/main/cpp/HaAlgorithm/invfreqz.cpp
            src/main/cpp/HaAlgorithm/main.cpp
            src/main/cpp/HaAlgorithm/sort_sos.cpp
            src/main/cpp/HaAlgorithm/tf2sos.cpp
            src/main/cpp/HaAlgorithm/user_tune_eq.cpp
            src/main/cpp/HaAlgorithm/wdrc_payload.cpp
    
            # CommandDefine
            src/main/cpp/HaCommandDefine/NoiseReductionPayload.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.
            HaAlgorithm
            android
            # Links the target library to the log library
            # included in the NDK.
            ${log-lib})

    可以看到,add_library 一行中已经添加了很多的cpp 文件, 有些还在第二级的子目录。

    解决办法:

    可以通过file指令搜索出所有后缀名为".cpp"的文件,然后添加到项目中,新的CMakeLists.txt修改如下:

    cmake_minimum_required(VERSION 3.4.1)
    
    set(CMAKE_CXX_STANDARD 11)

    file(GLOB_RECURSE native_srcs src/main/cpp/*.cpp) add_library(HaAlgorithm SHARED ${native_srcs}) target_link_libraries( HaAlgorithm android log )

    重点在于标红的那一句,什么意思呢,相当于告诉CMake, 搜索当前目录以及子目录中所有的以.cpp结尾的文件,然后把它们保存到  native_srcs 变量中。这里要注意CMakeLists.txt文件的位置,我是放在项目根目录下,所以我在搜索的时候指定了前缀:src/main/cpp。

    这样修改之后,以后再添加新的C++代码就不需要再修改CMake文件了,编译APK的时候CMake会自动找出符合条件的C++文件并且参与到构建当中。

    美中不足的是,虽然编译最终apk的时候,新添加的文件也会被编译,但是新添加的C++文件上方还是会出现:

     并且点击右边的“Sync Now” 之后还是没用!

    经过反复尝试发现可以通过点击菜单栏上的“Refresh Linked  C++ Projects” 让它消失。

  • 相关阅读:
    聊聊Spark的分区、并行度 —— 前奏篇
    深入探讨HBASE
    分布式流平台Kafka
    GeoServer中使用SLD样式
    OpenLayer修改WFS中的要素
    leaflet加载GeoServer的WFS服务
    OL实现属性查询的功能
    OL3实现空间查询的代码示例
    WFS—GetFeature方法
    OpenLayer+Geoserver+postgis实现路径分析
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/14689417.html
Copyright © 2011-2022 走看看