Calculator_C++ example
Overview
calculator_c++ is an example for using standard C++ in shared objects. It creates an HLOS application that remotely invokes C++ functions on the DSP using FastRPC. This example is derived from the calculator example. For more information on the setup and how to compile, please refer to the calculator example. The remaining of this page focuses only on how to use C++ code for the DSP.
The calculator_c++ example includes the following five functions to showcase some C++ features
calculator_plus_sum
- calculates sum of elements of vector of integers.calculator_plus_static_sum
- calculates sum of elements of vector of integers of statically initialized object.calculator_plus_test_tls
- tests thread-local storage feature of C++11.calculator_plus_iostream_sum
- calculates sum of elements of stream of data passed as file using standard input/output streams library(iostream).calculator_plus_uppercase_count
- counts uppercase letters in passed string.
For more information on C++(eg. supported standards, tools), please check the C/C++ section of the reference manuals.
Refer to the Feature Matrix for example support and to know the DSP architecture on the target.
Simulator testing
The Hexagon simulator hexagon-sim
is located under $DEFAULT_HEXAGON_TOOLS\Tools\bin\
.
While building hexagon variant (with a command like make hexagon BUILD=Debug DSP_ARCH=v68
), the calculator_c++ example will create a shared object libcalculator_plus_q.so
. The simulator command generated by the make file to run libcalculator_plus_q.so
can be found in last section(Command line used to invoke simulator:
) of the generated file hexagon_Debug_toolv88_v68/pmu_stats.txt
. You can use this command directly and/or modify it as desired for running additional simulations. Some of the available simulator command line options are discussed in simulator testing section of calculator. For more information on available options, please refer hexagon simulator document.
Once the dynamic object has been created, you can execute it on the simulator as follows:
Windows
%DEFAULT_HEXAGON_TOOLS_ROOT%/Tools/bin/hexagon-sim -mv68 --simulated_returnval --usefs hexagon_Debug_toolv88_v68 --pmu_statsfile hexagon_Debug_toolv88_v68/pmu_stats.txt --cosim_file hexagon_Debug_toolv88_v68/q6ss.cfg --l2tcm_base 0xd800 --rtos hexagon_Debug_toolv88_v68/osam.cfg %HEXAGON_SDK_ROOT%/rtos/qurt/computev68/sdksim_bin/runelf.pbn -- %HEXAGON_SDK_ROOT%/libs/run_main_on_hexagon/ship/hexagon_toolv88_v68/run_main_on_hexagon_sim -- libcalculator_plus_q.so
Linux
$DEFAULT_HEXAGON_TOOLS_ROOT/Tools/bin/hexagon-sim -mv68 --simulated_returnval --usefs hexagon_Debug_toolv88_v68 --pmu_statsfile hexagon_Debug_toolv88_v68/pmu_stats.txt --cosim_file hexagon_Debug_toolv88_v68/q6ss.cfg --l2tcm_base 0xd800 --rtos hexagon_Debug_toolv88_v68/osam.cfg $HEXAGON_SDK_ROOT/rtos/qurt/computev68/sdksim_bin/runelf.pbn -- $HEXAGON_SDK_ROOT/libs/run_main_on_hexagon/ship/hexagon_toolv88_v68/run_main_on_hexagon_sim -- libcalculator_plus_q.so
The test should complete indicating that "2 tests passed' in the Summary report displayed on the command line.
Using the walkthrough script
The walkthrough script calculator_c++_walkthrough.py
automates the steps of signing the device, building, pushing and running the calculator_c++
example. You can run the walkthrough script with the dry-run (-DR) option to display all the commands that the script would execute without actually running them.
Review the generic setup and walkthrough_scripts instructions to learn more about setting up your device and using the walkthrough script.
Arguments to the executable
Arguments | Description |
---|---|
-m sum -i <N> |
calculates the sum of the first |
-m static_sum -i <N> |
calculates the sum of the first |
-m test_tls |
tests thread-local storage feature of C++11 |
-m iostream_sum -i <Input file location> |
calculates sum of elements of stream of data passed as file using standard input/output streams library(iostream). The output file for the iostream test will be written to /vendor/lib/rfsa/dsp/sdk/calculator.output |
-m uppercase_count -i <Input string> |
counts uppercase letters in passed string |
All tests return 0 on success. Refer to the DSP logs for more details on the execution of each of these tests.
Note: test_tls
is unsupported on Waipio generation and older devices, due to the limitation of POSIX APIs on the unsigned cDSP on these devices.
Common queries
-
How to compile binaries for alternate C++ standards?
You can pass
-std
flag along with standard name inCXX_FLAGS
. For example:CXX_FLAGS += -std=c++14
Alternate available C++ standards are C++03 and C++14. For more information on C++ library support, please refer to the Hexagon LLVM C/C++ Compiler.
-
Which library to use:
libstdc++
orlibc++
for C++ code?libstdc++
should only be used when running a library compiled for the C++03 standard. For any newer standard, thelibc++
andlibc++abi
libraries should be used instead as shown inhexagon.min
. For more information on C++ library support, see Hexagon LLVM C/C++ Compiler -
How to find the libraries
libc++.so.1
andlibc++abi.so.1
?When the example is run on the target or in the hexagon simulator, the loader tries to load the dependent c++ libraries and looks for their symbolic links during this process. The symbolic links for the libraries
libc++.so
andlibc++abi.so
have to be present in the current directory to be available to the loader. These C++ libraries have symbolic links as follows:libc++.so -> libc++.so.1 -> libc++.so.1.0 libc++abi.so -> libc++abi.so.1 -> libc++abi.so.1.0
These symbolic links are copied to the current directory by adding copy rules as follows in the
hexagon.min
file:TARGET_DIR = $(HEXAGON_LIB_DIR)/$(V_ARCH)/G0/pic # Copy needed libraries to local build directory $(V)/libcalculator_plus_skel.so: $(V)/libc++.so.1 $(V)/libc++abi.so.1 # Copy both versions of the library to the local build directory # The loader will select one version at link time $(V)/libc++.so.1: $(V)/libc++abi.so.1 $(call cp_af,$(TARGET_DIR)/libc++.so.1 $(V)) $(call cp_af,$(TARGET_DIR)/libc++.so.1.0 $(V)) $(V)/libc++abi.so.1: $(call cp_af,$(TARGET_DIR)/libc++abi.so.1 $(V)) $(call cp_af,$(TARGET_DIR)/libc++abi.so.1.0 $(V))