Rate This Document
Findability
Accuracy
Completeness
Readability

Normalizing Dependencies

Dynamic and static link libraries are common third-party dependencies. For these dependencies, the common normalization method is to specify the relative or absolute paths in the compilation script and place the compiled library files in corresponding paths.

Procedure

For a third-party library, compile its source code to generate library files. Create paths of different architectures (for example, xx/lib/aarch64 and xx/lib/x86_64) and then place the compiled library files in corresponding paths. In the compilation script, modify the paths for compilation and linking so that the library file in the corresponding architecture is automatically selected.

Example:

In the following code example, a libcrc32.so dynamic library is compiled for dependency normalization so that the library can run on both the Kunpeng and x86 platforms.

  • Compile and generate Kunpeng and x86 dynamic libraries.

    Compile the following basic code on the Kunpeng and x86 platforms to generate libcrc32.so files, and save them to the xx/lib/x86_64 and xx/lib/aarch64 directories respectively.

    crc32.cpp

    #include "crc32.h" 
    #ifdef __x86_64__
    uint32_t crc32_u8(uint32_t crc, uint8_t value) 
    {
        __asm__("crc32b %1, %0" : "+r"(crc) : "rm"(value));
        return crc;
    }
    #endif
    #ifdef __aarch64__
    uint32_t crc32_u8(uint32_t crc, uint8_t value) 
    {
        __asm__("crc32cb %w[c], %w[c], %w[v]":[c]"+r"(crc):[v]"r"(value));
        return crc;
    }
    #endif

    crc32.h

    #ifndef CRC32__HHHH_HH
    #define CRC32__HHHH_HH
    #include <stdint.h>
    uint32_t crc32_u8(uint32_t crc, uint8_t v);
    #endif

    Compile the dynamic library:

    g++ crc32.cpp -I ./include -fPIC -shared -o libcrc32.so 

    For the Kunpeng platform, add the -march=armv8-a+crc compilation option. ./include indicates the actual path of the crc32.h file.

  • Modify the compilation script.

    Modify the compilation script so that the target path is automatically selected based on the architecture during compilation and linking.

    #!/bin/bash
    ARCH_TYPE=`uname -m`
    if [ "$ARCH_TYPE" = "x86_64" ];then
    g++ main.cpp -I ./include/ -L ./lib/x86_64/ -lcrc32 -o main
    fi
    if [ "$ARCH_TYPE" = "aarch64" ];then
    g++ main.cpp -I ./include/ -L ./lib/aarch64/ -lcrc32 -o main
    fi

    The structure of the sample project file is as follows:

    crc32Project
    ├── build.sh
    ├── include
    │   └── crc32.h
    ├── lib
    │   ├── aarch64
    │   │   └── libcrc32.so
    │   └──x86_64
    │       └── libcrc32.so
    └── main.cpp