Compile and Run Samples
Prerequisites
- To compile and execute operator APIs, ensure that the basic environment is set up, including the driver, firmware, CANN software package, and ops package.
- For details about the invocation procedure of the operator API and the compile and run operations, see the "Single Operator Invocation > Single Operator API Execution > Sample Code for Calling aclnn APIs" section in Application Development (C&C++).
Preparations Before Compilation
This chapter uses the co-location of development and operation environments as an example. That is, the machine with an AI processor serves as both the development environment and the operation environment. In this use case, code development and code execution are performed on the same machine. The AddExample operator is used as an example here. The invocation logic, procedure, and build script of other operators are roughly the same as those of the AddExample operator. Modify the API invocation script (*.cpp) and the build script (CMakeLists) as required.
Sample Code
The AddExample operator implements tensor addition. Obtain the sample code from test_aclnn_add_example.cpp and name the code file “test_aclnn_add_example.cpp”.
CMakeLists File
The following is an example of the CMake file. Modify it based on the actual situation:
# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved. # CMake lowest version requirement cmake_minimum_required(VERSION 3.14) # Set project name project(ACLNN_EXAMPLE) # Compile options add_compile_options(-std=c++11) # Set compile options set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "./bin") set(CMAKE_CXX_FLAGS_DEBUG "-fPIC -O0 -g -Wall") set(CMAKE_CXX_FLAGS_RELEASE "-fPIC -O2 -Wall") # Set the executable file name (for example, opapi_test) and specify the directory where the *.cpp file of the operator to be run resides add_executable(opapi_test test_aclnn_add_example.cpp) # Set ASCEND_PATH (CANN software package directory; modify it based on the actual path) and INCLUDE_BASE_DIR (header file directory) if(NOT "$ENV{ASCEND_CUSTOM_PATH}" STREQUAL "") set(ASCEND_PATH $ENV{ASCEND_CUSTOM_PATH}) else() set(ASCEND_PATH "/usr/local/Ascend/cann") endif() set(INCLUDE_BASE_DIR "${ASCEND_PATH}/include") include_directories( ${INCLUDE_BASE_DIR} ${INCLUDE_BASE_DIR}/aclnn ) # Set the path of the library file to be linked target_link_libraries(opapi_test PRIVATE ${ASCEND_PATH}/lib64/libascendcl.so ${ASCEND_PATH}/lib64/librasopbase.so ${ASCEND_PATH}/lib64/libopapi_ras.so) # The executable file is stored in the bin directory under the directory where the CMakeLists file resides install(TARGETS opapi_test DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
Compile and Run
Prepare the operator invocation code (*.cpp) and the build script (CMakeLists.txt) in advance.
Configure environment variables.
After installing the CANN software, log in to the CANN runtime environment as the user and execute the following command to make the environment variables take effect.
source ${INSTALL_DIR}/set_env.shWhere
${INSTALL_DIR}is the storage path of the CANN software after installation. Replace it with the actual path as needed.Compile and run.
Go to the directory where CMakeLists.txt is located and run the following command to create a build directory for storing the generated compilation files.
mkdir -p buildGo to the build directory, run the cmake command to compile, and then run the make command to generate the executable file.
cd build cmake ../ -DCMAKE_CXX_COMPILER=g++ -DCMAKE_SKIP_RPATH=TRUE makeAfter successful compilation, the opapi_test executable file is generated in the bin folder under the build directory.
Go to the bin directory and run the executable file opapi_test.
cd bin ./opapi_test
The following is an example of the execution result of the AddExample operator:
result[0] is: 1.200000 result[1] is: 2.200000 result[2] is: 3.200000 result[3] is: 5.400000 result[4] is: 6.400000 result[5] is: 7.400000 result[6] is: 9.600000 result[7] is: 10.600000