1# Building an NDK Project with Prebuilt Libraries 2 3In an NDK project, you can use the CMake syntax to import and use prebuilt libraries. When prebuilt libraries are referenced, both those in the module's **libs** directory and those declared in the **CMakeList.txt** build script are packaged. 4 5## Constraints for Using Prebuilt Libraries 6 71. Ensure that the imported dynamic libraries (.so files) are compiled using the [OpenHarmony NDK toolchain](build-with-ndk-overview.md). For details about how to compile prebuilt libraries with the OpenHarmony NDK toolchain, see [Adaptation Process of Using CMake to Build Third-Party Libraries](https://developer.huawei.com/consumer/en/doc/best-practices/bpta-cmake-adapts-to-harmonyos#section1826019653918). 8 92. Ensure that dependencies of the imported .so dynamic libraries are also imported into the project and compiled using the OpenHarmony NDK toolchain. 10 11## Importing a Prebuilt Library 12 13You can import a prebuilt library by directly copying the library files into the project. For example, the prebuilt library **libavcodec_ffmpeg.so** is located in the following directory during development. 14 15 16 17To use it in your project, add it through **add_library** in the module's **CMakeLists.txt** build script and declare information such as the path to the prebuilt library. Then you can declare the link to the prebuilt library in **target_link_libraries**. The following is an example of the build script: 18 19```cmake 20add_library(library SHARED hello.cpp) 21 22add_library(avcodec_ffmpeg SHARED IMPORTED) 23set_target_properties(avcodec_ffmpeg 24 PROPERTIES 25 IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/third_party/FFmpeg/libs/${OHOS_ARCH}/libavcodec_ffmpeg.so) 26 27target_link_libraries(library PUBLIC libace_napi.z.so avcodec_ffmpeg) 28``` 29 30Add **include_directories** in the module's **CMakeLists.txt** build script. 31 32```cmake 33include_directories( 34 ... 35 ${CMAKE_CURRENT_SOURCE_DIR}/third_party/FFmpeg/include 36) 37``` 38 39When prebuilt libraries are used in the HAR, the currently built libraries and prebuilt libraries required for linking are packed to the **libs** directory in the HAR, as shown in the following figure. 40 41 42 43### Resolving the SONAME Issue with Prebuilt Libraries 44 45Ensure that the imported prebuilt dynamic libraries (.so files) have the correct SONAME set. 46 47If a prebuilt .so file lacks a SONAME, the linker will embed the absolute path of the .so file into the **dynamic section** of dependent binaries. When these binaries are packaged into a HAP for release, the dynamic loader may fail to locate the .so file, leading to runtime errors. 48 49Use the **llvm-readelf** tool to verify whether a .so file has a SONAME set. The tool's path depends on your installation: **${*DevEco Studio installation directory*}/sdk/default/openharmony/native/llvm/bin** or **${*command-line-tools installation directory*}/sdk/default/openharmony/native/llvm/bin/llvm-readelf**. 50Example: 51 52```bash 53> ${YOUR_PATH}/command-line-tools/sdk/default/openharmony/native/llvm/bin/llvm-readelf -d libavcodec_ffmpeg.so | grep SONAME 540x000000000000000e (SONAME) Library soname: [libavcodec_ffmpeg.so] 55``` 56 57For prebuilt dynamic libraries (.so files) built with CMake, no additional configuration is required for setting the SONAME, since CMake automatically sets the SONAME for them as long as the target platform supports it. 58For prebuilt dynamic libraries (.so files) built with other tools, explicitly configure **ldflags** to set the SONAME. 59 60```bash 61${...}/clang++ ${...} -Wl,-soname,libavcodec_ffmpeg.so 62``` 63 64## Using a Prebuilt Library Integrated in a Remote HAR Dependency 65 66To use a prebuilt library integrated in a remote dependency HAR, write the reference script in **CMakeLists.txt** as follows: 67 68```cmake 69set(DEPENDENCY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules) 70add_library(library SHARED IMPORTED) 71set_target_properties(library 72 PROPERTIES 73 IMPORTED_LOCATION ${DEPENDENCY_PATH}/library/libs/${OHOS_ARCH}/liblibrary.so) 74add_library(entry SHARED hello.cpp) 75target_link_libraries(entry PUBLIC libace_napi.z.so library) 76``` 77 78## Using a Prebuilt Library Integrated in a Local HAR 79 80To use a prebuilt library integrated in a local HAR, write the reference script in **CMakeLists.txt** as follows: 81 82```cmake 83set(LIBRARY_DIR "${NATIVERENDER_ROOT_PATH}/../../../../library/build/default/intermediates/libs/default/${OHOS_ARCH}/") 84add_library(library SHARED IMPORTED) 85set_target_properties(library 86 PROPERTIES 87 IMPORTED_LOCATION ${LIBRARY_DIR}/liblibrary.so) 88add_library(entry SHARED hello.cpp) 89target_link_libraries(entry PUBLIC libace_napi.z.so library) 90``` 91