App Build and Run

  1. Build the code.
    For details, see Image Classification with Caffe ResNet-50 (Synchronous Inference). The procedure is as follows:
    1. Create a build script.
      Obtain the CMakeLists.txt build script from the Image Classification with Caffe ResNet-50 (Synchronous Inference) sample and tweak the following lines:
      • include_directories: Add the directories of the header files to be included. For details about the required header files, see Dependent Header Files and Library Files.

        The following is an example.

        include_directories(
                directoryPath1
                directoryPath2
        )
      • link_directories: Add the directories of the library files to be linked with.

        The following is an example.

        link_directories(
                 directoryPath3 
                 directoryPath4
        )
      • add_executable: Set the name of your executable file (for example, main) and add the directory of the .cpp files.

        The following is an example.

        add_executable(main
                directoryPath5
                directoryPath6)
      • target_link_libraries: Set the name of the executable file (consistent with that specified in add_executable) and add the library files on which the executable file depends. The dependency library files are related to the header files where the API is located. For details, see Dependent Header Files and Library Files.

        The following is an example.

        target_link_libraries(main
                ascendcl
                libName1 
                libName2)

        When building code based on the AscendCL APIs, depend on the corresponding library files based on the included header files. If redundant .so files (such as libascendcl.a) are referenced, version functions may be abnormal or compatibility issues may occur during version update.

      • Build options: Modify the name of the executable file (consistent with that of add_executable)
        The following example indicates that the main file will be installed to the ${CMAKE_INSTALL_PREFIX}/out directory. The path defined by the ${CMAKE_INSTALL_PREFIX} variable is relative to the path where the cmake command is executed.
        set(CMAKE_RUNTIME_OUTPUT_DIRECTORY  "../../../out")
        
        install(TARGETS main DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
    2. Set the following environment variable.

      To modify the CMakeLists.txt build script in the Image Classification with Caffe ResNet-50 (Synchronous Inference) sample, set the DDK_PATH and NPU_HOST_LIB environment variables, which point to the AscendCL header file directory and library file directory, respectively.

      The following is an example of setting environment variables. ${INSTALL_DIR} indicates the CANN installation directory. For example, in $HOME/Ascend/ascend-toolkit/latest/{arch-os}, arch indicates the OS architecture, and {os} indicates the OS.

      export DDK_PATH=${INSTALL_DIR}
      export NPU_HOST_LIB=${INSTALL_DIR}/{arch-os}/devlib

      The .so library files in the ${INSTALL_DIR}/{arch-os}/devlib directory are required to build code using AscendCL APIs, without depending on any .so library files of other components (such as Driver). Therefore, when using the cmake command, set DCMAKE_SKIP_RPATH to TRUE, indicating that the rpath (that is, the ${INSTALL_DIR}/{arch-os}/devlib directory) is not added to the executable file generated during build.

      When the app runs with the build succeeded, you can configure the environment variables to link the app with the .so library files in the lib64 directory in the operating environment. At run time, the app is automatically linked with other .so library files that depend on other components.

    3. Run the cmake command to build the code.

      The following is an example command. ../../../src indicates the directory where the CMakeLists.txt file is located, and -DCMAKE_CXX_COMPILER specify the compiler. Change these parameters based on the actual requirements.

      cmake ../../../src -DCMAKE_CXX_COMPILER=g++ -DCMAKE_SKIP_RPATH=TRUE

      For details about the CMake parameters, go to https://cmake.org/cmake/help/latest/guide/tutorial/index.html and query the required version.

    4. Run the make command to generate an executable file.
      make
  2. Run the executable file.

    Upload the directories of the AscendCL initialization configuration file (acl.json), executable file, test image, and .om file to the same directory of the operating environment. Then log in to the operating environment, switch to the directory where the executable file is located, and run the executable file. The following command is an example:

    ./main

    If a null pointer is passed to the aclInit API call for AscendCL initialization, the directory of the AscendCL initialization configuration file (acl.json) does not need to be uploaded to the operating environment.

    If an error is reported during app running, fix it by referring to Troubleshooting.