janbar / openssl-cmake

Build OpenSSL with CMake on MacOS, Win32, Win64 and cross compile for Android, IOS
Other
267 stars 138 forks source link

compile with android env #6

Closed telenewbie closed 4 years ago

telenewbie commented 4 years ago

output:

/home/txz/test/testCMake/t10/build-android/src/openssl/crypto/threads_pthread.c:183: error: undefined reference to 'pthread_atfork'
clang38: error: linker command failed with exit code 1 (use -v to see invocation)
crypto/CMakeFiles/crypto.dir/build.make:16552: recipe for target 'crypto/libcrypto.so' failed
make[5]: *** [crypto/libcrypto.so] Error 1

compile command: /cross/github/jni/android-ndk-r14b/ is my android-ndk path.

cmake  -DCMAKE_TOOLCHAIN_FILE=/cross/github/jni/android-ndk-r14b/build/cmake/android.toolchain.cmake -DANDROID_TOOLCHAIN=clang -DANDROID_ABI=armeabi-v7a -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang   ..;

and how can i compile crypto with static option? i found this code on this project

 set( CMAKE_THREAD_PREFER_PTHREAD TRUE )
 find_package ( Threads )
 if( NOT Threads_FOUND )
   add_submodule ( . threads_none.c )
 elseif( WIN32 )
   add_submodule ( . threads_win.c )
 else()
   add_submodule ( . threads_pthread.c )
 endif()

 if( ANDROID )
   add_library( crypto SHARED ${LIBSRC} ${OBJECTS_SRC} )
 else()
   add_library( crypto ${LIBSRC} ${OBJECTS_SRC} )
 endif()

on my demo project:

 set(ANDROID_BUILD_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_BINARY_DIR}"
     "-DCMAKE_TOOLCHAIN_FILE=/cross/github/jni/android-ndk-r14b/build/cmake/android.toolchain.cmake"
     "-DANDROID_UNIFIED_HEADERS=ON"
     "-DANDROID_TOOLCHAIN=clang"
     "-DCMAKE_ANDROID_STANDALONE_TOOLCHAIN=/cross/github/jni/android-ndk-r14b/my-android-toolchain"
     "-DANDROID_ABI=armeabi-v7a"
     "-DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang")

if(ANDROID)
     LIST(APPEND MY_CMAKE_ARGS ${ANDROID_BUILD_ARGS})
 endif()

     externalproject_add(
         openssl
         #GIT_REPOSITORY https://github.com/janbar/openssl-cmake.git
         URL ${CMAKE_SOURCE_DIR}/vendor/openssl-cmake-1.1.1.tar.gz
         #CONFIGURE_COMMAND ""
         CMAKE_COMMAND ${CMAKE_COMMAND}
         BUILD_COMMAND make
         PREFIX ${CMAKE_CURRENT_BINARY_DIR}
         #UPDATE_DISCONNECTED 1
         CMAKE_ARGS
         ${MY_CMAKE_ARGS}
         #        "-DCMAKE_CXX_FLAGS=-fPIC"
         #"-DCMAKE_C_FLAGS=-fPIC"
         BUILD_IN_SOURCE 1
         INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}
         )
janbar commented 4 years ago

If you need to link statically the libs crypto/ssl with your binary , you should remove the lines forcing shared. In many cases it is preferable to deploy with your app the shared libs, and then the linker will use them if they are not exist in the android root. Most of android system have already the libs crypto and ssl.

telenewbie commented 4 years ago

thanks, because i want to ,finally, compile to one shared lib on my android app ,i must compile this with static . so i can compile with this

 target_link_libraries(${PROJECT_NAME}                                                                             
    -Wl,--whole-archive                                                                                           
    -lmad                                                                                                         
    -lbz2                                                                                                         
    -lcurl                                                                                                        
    -lz                                                                                                           
    -levent_extra                                                                                                 
    -ltag                                                                                                         
    -ltce                                                                                                         
    -ltxz_json                                                                                                    
    -lpcre                                                                                                        
    -lprotobuf                                                                                                    
    -ltxzOSAL                                                                                                     
    -lcrypto                                                                                                      
    -lprotos                                                                                                      
    -lssl                                                                                                         
    -levent_pthreads                                                                                              
    -lopus                                                                                                        
    -levent_core                                                                                                  
    -Wl,--no-whole-archive                                                                                        
    -ldl                                                                                                          
    -lpthread                                                                                                                                                                                                                     
    )

and android app only use one shared lib to intergrated。

and i found the way to resolve: must specify android-platform

-DANDROID_PLATFORM=android-19