App Build and Run
This section describes how to build and run your app after coding.
App Build and Run
- Build the code.
For details, see Image Classification with ResNet-50 (Synchronous Inference). The procedure is as follows:
- Create a build script.
Obtain the CMakeLists.txt build script from the Image Classification with ResNet-50 (Synchronous Inference) sample and tweak the following parameters:
- 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: Change the name of the executable file (consistent with that in add_executable) and add the library file 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 in add_executable) and the installation path of the executable file.
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})
- 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.
- Set environment variables.
If you are using a build script developed by yourself without using the DDK_PATH or NPU_HOST_LIB environment variable, you do not need to set environment variables. Add configurations based on your script logic.
To modify the CMakeLists.txt build script in the Image Classification with 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 software installation directory, for example, $HOME/Ascend/ascend-toolkit/latest. arch indicates the OS architecture, and {os} indicates the operating system.
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.
- 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.
- If the development environment and operating environment use the same OS architecture, run the following command to perform compilation:
cmake ../../../src -DCMAKE_CXX_COMPILER=g++ -DCMAKE_SKIP_RPATH=TRUE
- If development environment and operating environment use different OS architectures, run the following command to perform cross-compilation:
For example, if the development environment uses the x86 architecture while the operating environment uses the AArch64 architecture, run the following command to perform cross-compilation:
cmake ../../../src -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_SKIP_RPATH=TRUE
- If the development environment and operating environment use the same OS architecture, run the following command to perform compilation:
- Run the make command to generate an executable file.
make
- Create a build script.
- 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.
Troubleshooting
If an error occurs during app running, you can refer to Log Reference to obtain the log file and view the error details. Take the following steps to fix the error:
- If the error is caused by an API call sequence, adjust your API call sequence by referring to the general restrictions and the API-specific restrictions.
- If the error is reported during operator running on the AI Core, you need to further locate the cause by calling the AscendCL API to obtain the description of the error operator for further analysis. For details, see AI Core Troubleshooting.
- For typical cases and solutions, see Troubleshooting.