3

我正在用双簧管开始一个新项目。我在Github/Oboe上使用完全相同的 CMakeLists.txt 。

老实说,这是我的第一个android原生项目。我对 C++ 有扎实的了解,但我一生中从未使用过 CMake。我已经尝试了几个小时来“修复”示例 CMakeLists.txt 文件(到处研究以了解每一行的作用),但这很累,因为我就像玻璃器皿上的大象。每次我触摸一条线,错误都会因为我而“变异”。

我的目标不是学习 CMake(至少现在,尽管我在不久的将来肯定会需要它),而是让 lib 工作,这样我就可以开始完成工作了。

所以这是我的问题:我在哪里可以找到 CMakeLists.txt 的工作副本,其中双簧管作为单一依赖项开始制造噪音?如果它根本不存在,并且我必须在对本机编程进行任何移动之前肯定学习 CMake 相关的东西,那么任何起点或建议都将受到高度赞赏。

我已经阅读了 NDK CMake 指南,并搜索了很多带有错误“无法为不是由该项目构建的目标 'xxxxxxx' 指定链接库”的 SO 问题,但这个错误甚至从双簧管 CMakeLists.txt 文件中上升。

提前致谢。

4

2 回答 2

3

CMakeLists.txt 有点像 gradle 的桥梁,用于获取您想在应用程序中使用的所有本机内容。

这是我的示例 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)

include_directories(third_party)
include_directories(src/main/cpp/)

# 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.
        audio-native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        #${audiosupport_sources}
        # main game files
        src/main/cpp/audio-support.cpp
        src/main/cpp/main/AudioPlayer.cpp

        # audio engine
        src/main/cpp/audio/AAssetDataSource.cpp
        src/main/cpp/audio/Player.cpp

        # UI engine
        src/main/cpp/ui/OpenGLFunctions.cpp

        # utility functions
        src/main/cpp/utils/logging.h
        src/main/cpp/utils/UtilityFunctions.cpp
        )


set (TARGET_LIBS log android oboe GLESv2)
MESSAGE(STATUS "Using NDK media extractor")
add_definitions(-DUSE_FFMPEG=0)
target_sources( audio-native-lib PRIVATE src/main/cpp/audio/NDKExtractor.cpp )
set (TARGET_LIBS ${TARGET_LIBS} mediandk)

# 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( audio-native-lib ${TARGET_LIBS} )

# Set the path to the Oboe directory.
# TODO change this to where you downloaded oboe library
set (OBOE_DIR /Users/MyUser/Documents/repos/android_audio/oboe)
# Add the Oboe library as a subdirectory in your project.
# add_subdirectory tells CMake to look in this directory to
# compile oboe source files using oboe's CMake file.
# ./oboe specifies where the compiled binaries will be stored
add_subdirectory (${OBOE_DIR} ./oboe-bin)



target_compile_options(audio-native-lib
        PRIVATE -std=c++14 -Wall -Werror "$<$<CONFIG:RELEASE>:-Ofast>")

您必须定义所有包含目录并链接构建文件,除此之外,您还必须将 gradle 文件与此 CMakeLits.txt 链接

externalNativeBuild {
    cmake {
        path file('CMakeLists.txt')
    }
}
sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}

最后,您还应该注意文件位于正确的位置,因此项目结构应如下 所示

于 2019-09-23T15:30:00.927 回答
1

如果可以的话,它可能会更顺利:

于 2019-03-06T01:09:02.133 回答