An Error Is Reported During Application Sample Compilation and Running, Indicating That the Header File or Library File Missing

Symptom

Obtain the sample code from the Ascend samples repository (Image Classification):

  • During source code compilation, a message is displayed, indicating that the header file acl.h cannot be found. The following is an example:
     fatal error: acl/acl.h: No such file or directory
     #include "acl/acl.h"
              ^~~~~~~~~~~
    compilation terminated.
    CMakeFiles/main.dir/build.make:62: recipe for target 'CMakeFiles/main.dir/main.cpp.o' failed
    make[2]: *** [CMakeFiles/main.dir/main.cpp.o] Error 1
    CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
    make[1]: *** [CMakeFiles/main.dir/all] Error 2
    Makefile:129: recipe for target 'all' failed
  • During source code compilation, a message is displayed, indicating that the library file libascendcl.so cannot be found. (In the error message, -l in -lascendcl indicates that the library file was searched for.) The following is an example:
    /usr/bin/ld: cannot find -lascendcl
    collect2: error: ld returned 1 exit status
    CMakeFiles/main.dir/build.make:94: recipe for target '/home/HwHiAiUser/sample/resnet50_firstapp/out/main' failed
    make[2]: *** [/home/HwHiAiUser/sample/resnet50_firstapp/out/main] Error 1
    CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
    make[1]: *** [CMakeFiles/main.dir/all] Error 2
    Makefile:129: recipe for target 'all' failed

Possible Cause

The {DDK_PATH} and {NPU_HOST_LIB} environment variables may be incorrectly set. In this sample, these environment variables are used to search for AscendCL API header file and library file, which are the dependency files for the compilation of the app source code.

Solution

  1. Log in to the source code compilation environment and run the following commands to check the values of the {DDK_PATH} and {NPU_HOST_LIB} environment variables:
    • Checking the {DDK_PATH} value:
      echo $DDK_PATH

      Information similar to the following is displayed: (If no information is displayed, the environment variable is not set. Set it by referring to 4.)

      /home/HwHiAiUser/Ascend/ascend-toolkit/latest
    • Checking the {NPU_HOST_LIB} value:
      echo $NPU_HOST_LIB

      Information similar to the following is displayed: (If no information is displayed, the environment variable is not set. Set it by referring to 4.)

      /home/HwHiAiUser/Ascend/ascend-toolkit/latest/runtime/lib64/stub
  2. Check whether the header file exists in the directory specified by {DDK_PATH}, the environment variable value obtained in 1.

    The compilation script of the sample searches for the header file on which compilation depends based on the {DDK_PATH}/runtime/include/acl directory. Therefore, you can check whether this directory exists. If yes, check whether the acl.h header file exists in this directory. If either the directory or the header file does not exist, set {DDK_PATH} again by referring to 4.

    • Check whether the directory exists:
      cd /home/HwHiAiUser/Ascend/ascend-toolkit/latest/runtime/include/acl
    • Check whether the acl.h file exists:
      ll acl.h
  3. Check whether the library file exists in the directory specified by {NPU_HOST_LIB}, the environment variable value obtained in 1.

    The compilation script of the sample searches for the library file on which compilation depends based on the directory specified by {NPU_HOST_LIB}. Therefore, you can check whether this directory exists. If yes, check whether the libascendcl.so library file exists in this directory. If neither the directory nor the library file exists, set {NPU_HOST_LIB} again by referring to 4.

    • Check whether the directory exists:
      cd /home/HwHiAiUser/Ascend/ascend-toolkit/latest/runtime/lib64/stub
    • Check whether the libascendcl.so file exists:
      ll libascendcl.so
  4. Set the environment variables.

    The compilation script searches for the header file on which compilation depends based on the {DDK_PATH}/runtime/include/acl directory, and searches for the library file based on the directory specified by {NPU_HOST_LIB}. Replace $HOME/Ascend with the actual Ascend-CANN-Toolkit installation path.

    • If the OS architectures of the development and operating environments are the same, set the environment variables as follows:
      export DDK_PATH=$HOME/Ascend/ascend-toolkit/latest
      export NPU_HOST_LIB=$DDK_PATH/runtime/lib64/stub
    • If the OS architectures of the development and operating environments are different, set the environment variables as follows:

      For example, if the development environment uses the x86 architecture and the operating environment uses the AArch64 architecture, cross compilation is involved. In this case, you need to install the software package of the AArch64 architecture in the development environment and set the {DDK_PATH} environment variable to the installation directory of this software package. In this way, the header file and library file in the software package are the same as those in the operating environment, which facilitates code compilation.

      export DDK_PATH=$HOME/Ascend/ascend-toolkit/latest/arm64-linux
      export NPU_HOST_LIB=$DDK_PATH/runtime/lib64/stub
    • You can log in to the corresponding environment and run the uname -a command to query the OS architecture.
    • If you do not know the installation path of the software package (Ascend-cann-toolkit_{version}_linux-{arch}.run), you can also run the find -name "filename" command to find the paths of the acl.h and libascendcl.so files and then configure the environment variables.

      If multiple software versions are installed in the environment, select a version as required. By default, the latest directory points to the last installed software version.

      If there are multiple acl.h and libascendcl.so files in the same version, some of them are soft links for compatibility with earlier versions. This helps users compile app source code in earlier versions.