Reputation: 825
I'm trying to build my native library with CMake in latest Android Studio. I prepared gradle scripts for this, no any problems, but I found a small problem - I can not compile my library for x86 arch.
Some time before...
My library uses OpenSSL AES/DES encryption/decryption. I compiled OpenSSL 1.0.2k as is (static library), linked it to my shared library and all was fine except x86 arch - there was an error shared library text segment is not shareable
while dlopen
on then device. Then I recompiled OpenSSL with -fPIC
flag, linked it again and error went away. I was building with NDK 13b.
Now...
I am trying to migrate from NDK to CMake because it is has more features for me and Android Studio can normally autocomplete and lint C/C++ code only with CMake. I wrote CMakeList.txt
and it works, but problem with shared library text segment is not shareable
appeared again, but on linkage step of CMake build process. Error:
D:/User/AppData/Local/Android/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/windows-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin\ld: warning: shared library text segment is not shareable
D:/User/AppData/Local/Android/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/windows-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin\ld: error: treating warnings as errors
I disabled treating this warning as error and shared library text segment is not shareable
appeared while dlopen
on the device again.
What is the problem? Why NDK builds without any problems and CMake does not?
P.S. I tried different CMAKE
flags (such as CMAKE_POSITION_INDEPENDENT_CODE
) and nothing works. This problem occurrs only for x86
arch.
CMakeLists.txt (Build failed for x86, all others - no problems):
cmake_minimum_required(VERSION 3.4.1)
include_directories(include/)
find_library(log-lib log)
add_library(libcrypto STATIC IMPORTED)
add_library(libssl STATIC IMPORTED)
add_library(testlib SHARED test.c)
set_target_properties(libcrypto
PROPERTIES
IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/static/${ANDROID_ABI}/libcrypto.a)
set_target_properties(libssl
PROPERTIES
IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/static/${ANDROID_ABI}/libssl.a)
target_link_libraries(testlib libcrypto libssl ${log-lib})
set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fPIC")
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -fPIC")
Android.mk (NDK14b, All archs - no problems):
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := $(LOCAL_PATH)/static/$(TARGET_ARCH_ABI)/libcrypto.a
LOCAL_MODULE := crypto
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := $(LOCAL_PATH)/static/$(TARGET_ARCH_ABI)/libssl.a
LOCAL_MODULE := ssl
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LCOAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := test.c
LOCAL_MODULE := testlib
LOCAL_STATIC_LIBRARIES := crypto ssl
include $(BUILD_SHARED_LIBRARY)
Thanks in advance for any help!
Upvotes: 3
Views: 2462
Reputation: 825
The solution was that I need to compile OpenSSL with no-asm
flag. After that OpenSSL linked and worked normally on x86 arch.
Upvotes: 2
Reputation: 1
just in x86, add --no-warn-shared-textrel, you should add this to CMakeList.txt:
if (${ANDROID_ABI} STREQUAL "x86")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-warn-shared-textrel")
endif ()
good luck.
Upvotes: 0
Reputation: 103
set(CMAKE_SHARED_LINKER_FLAGS "-Wall -v -Wl, --no-warn-shared-textrel")
this exact toolcain is treating warnings as errors
, so just add this to suppress the warning. --no-warn-shared-textrel is the key
Upvotes: 0