【问题标题】:Android How to enable position independent flag -fpic for c++ codeAndroid 如何为 C++ 代码启用与位置无关的标志 -fpic
【发布时间】:2021-11-27 10:58:45
【问题描述】:

我在我的应用程序中使用 c++ 类来存储我的常量/url 等。为此,我配置了 ndk 和 CMakeLists.txt 文件。所以我的 jni 文件夹中只有两个文件,即 app-config-native-lib.cpp 和 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)

# 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.
        app-config-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        app-config-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.
        app-config-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

我想启用 Position Independent flag-fpic 以遵守安全约束,正如 here here 所解释的那样。 this 线程中解释说,我们可以在 externalNativeBuild/cmake 的构建 gradle 文件中传递标志,但我不确定。

我的问题是我们如何为 Android 中的本机代码启用 PIC 可执行文件?应该使用什么标志?以及将该标志放在构建 gradle 文件或 CMakeLists 文件中的什么位置?

【问题讨论】:

    标签: android c++ cmake android-ndk ndk-build


    【解决方案1】:

    -fPIC 选项是 Android 上的默认选项。
    您可以通过以下方式在CMakeLists.txt 中添加其他编译器选项:
    set_source_files_properties(myfile.c PROPERTIES COMPILE_FLAGS " -mfpu=neon -fno-vectorize")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 2015-09-28
      • 2011-07-19
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多