26

I want to import a prebuilt library using this CmakeLists.txt snippet:

add_library(openssl-crypto
            SHARED
            IMPORTED )
set_target_properties(openssl-crypto
                      PROPERTIES
                      IMPORTED_LOCATION
                      ${external_DIR}/libs/${ANDROID_ABI}/libcrypto.so )
include_directories(${external_DIR}/include/openssl)

I linked this to my library as:

target_link_libraries(aes-crypto openssl-crypto)

Attempting to build returns this error:

'/libs/arm64-v8a/libcrypto.so', needed by ...,  missing and no known rule to make it
gelliott181
  • 1,008
  • 11
  • 19
user7377570
  • 269
  • 1
  • 3
  • 3
  • 1
    What if you use: `add_dependencies(aes-crypto openssl-crypto) ` Does that help? And how do you build openssl-crypto? – fedepad Jan 05 '17 at 08:31
  • The `add_dependencies` did not work. I used the `add_library(openssl-crypto SHARED IMPORTED)` to build the openssl-crypto library – user7377570 Jan 06 '17 at 01:51
  • 1
    The problem seems to be that the variable "external_DIR" is empty. – David Marquant Jan 06 '17 at 09:56
  • Thank you. The external_DIR is empty,actually, I do not see the same error when I change external_DIR to the absolute path. But now I encounter a new error which is "error adding symbols: File in wrong format" when I build another prebuilt library like `add_library(jni-native-helper SHARED IMPORTED) set_target_properties(jni-native-helper PROPERTIES IMPORTED_LOCATION ../../../../src/main/cpp/external/libs/arm64-v8a/libnativehelper.so:)`, – user7377570 Jan 09 '17 at 10:01
  • 1
    @user7377570 you should create a new question for it. I'm running into the same issues you are. – Nathan F. Jun 07 '17 at 22:15
  • @NathanFiscaletti Any luck resolving this issue? I am encountering the same error. – savi Jun 08 '17 at 01:28
  • @savi see my answer for what I did to resolve it on my end. Hopefully it works for someone else. – Nathan F. Jun 08 '17 at 17:35

3 Answers3

30

I found that the IMPORTED_LOCATION property, when passed to the set_target_properties function doesn't like relative paths.


From the CMake Documentation on IMPORTED_LOCATION

Full path to the main file on disk for an IMPORTED target.


To resolve this issue, I used the full path to the library instead.

Example:

set_target_properties ( curl-lib 
                        PROPERTIES IMPORTED_LOCATION 
                        libs/${ANDROID_ABI}/libcurl.a )

. . . becomes . . . 

set_target_properties ( curl-lib 
                        PROPERTIES IMPORTED_LOCATION 
                        ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/${ANDROID_ABI}/libcurl.a )

The path to the library may differ slightly for you from the one above, but ${PROJECT_SOURCE_DIR} is normally a good starting point as it is defined as:

...the source directory of the last call to the project() command made in the current directory scope or one of its parents. Note, it is not affected by calls to project() made within a child directory scope (i.e. from within a call to add_subdirectory() from the current scope).

Nathan F.
  • 3,250
  • 3
  • 35
  • 69
3

You can use set_property function with attribute TARGET instead of set_target_properties, and then you can set path relatively using macros ${PROJECT_SOURCE_DIR}.

# import shared library libmylib.so    
add_library( my-imported-lib 
             SHARED 
             IMPORTED) 

# set the path to appropriate so files with appropriate architectures
set_property(TARGET 
             my-imported-lib 
             PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/<path_to_libs_directory>/${ANDROID_ABI}/libmy-imported-lib.so) 

...

# link imported library to your developed library
target_link_libraries( my-developed-lib 
                       my-imported-lib ) 

Maybe you can use macros ${PROJECT_SOURCE_DIR} while set lib path with set_target_properties but I didn't check this way.

  • Using `${PROJECT_SOURCE_DIR}` when set lib path with `set_target_properties` works for me. This answer should be accepted – Marvin Sep 06 '17 at 05:53
0

Maybe file have wrong datetime. Set current datetime for ${external_DIR}/libs/${ANDROID_ABI}/libcrypto.so:

touch ${external_DIR}/libs/${ANDROID_ABI}/libcrypto.so
biruk1230
  • 3,042
  • 4
  • 16
  • 29