Android 如何为 C++ 代码启用位置独立标志 -fpic
Android How to enable position independent flag -fpic for c++ code
我在我的应用程序中使用 c++ class 来存储我的 constants/urls 等。为此我配置了 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})
我想启用与位置无关的标志-fpic 以遵守安全约束,因为它解释了 here here. It is explained in this 线程,我们可以在 externalNativeBuild/cmake 中的构建 gradle 文件中传递标志,但我我不确定。
我的问题是如何在 Android 中为本机代码启用 PIC 可执行文件?应该使用什么标志?以及将该标志放在构建 gradle 文件或 CMakeLists 文件中的什么位置?
选项 -fPIC
是 Android 上的默认选项。
您可以通过以下方式在 CMakeLists.txt
中添加其他编译器选项:
set_source_files_properties(myfile.c PROPERTIES COMPILE_FLAGS " -mfpu=neon -fno-vectorize")
我在我的应用程序中使用 c++ class 来存储我的 constants/urls 等。为此我配置了 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})
我想启用与位置无关的标志-fpic 以遵守安全约束,因为它解释了 here here. It is explained in this 线程,我们可以在 externalNativeBuild/cmake 中的构建 gradle 文件中传递标志,但我我不确定。
我的问题是如何在 Android 中为本机代码启用 PIC 可执行文件?应该使用什么标志?以及将该标志放在构建 gradle 文件或 CMakeLists 文件中的什么位置?
选项 -fPIC
是 Android 上的默认选项。
您可以通过以下方式在 CMakeLists.txt
中添加其他编译器选项:
set_source_files_properties(myfile.c PROPERTIES COMPILE_FLAGS " -mfpu=neon -fno-vectorize")