Теперь с хоста по SSH можно подцепиться к симуляции.
[gazpar@localhost work]$ ssh -p10022 arm@localhost arm@debian:~$ arm@debian:~$ uname -a Linux debian 3.2.0-4-vexpress #1 SMP Debian 3.2.84-1 armv7l GNU/Linux
Теперь можно и собрать программку. По Makefile’у ясно, что будет калькулятор. Простенький.
#include #include #include // Function to check input expression bool checkExpression(std::string exp) < for(uint i=0; i'9' || c == '\'') < if(c != ' ') return false; >> return true; > // Template function to evaluate atomic expression template T eval(int a, int b, const char op) < switch(op)< case '+':< return a+b; >case '-': < return a-b; >case '*': < return a*b; >case '/': < return a/b; >default: throw("atomEval: Undefined math operation"); > >; // Function to evaluate expression without brackets template std::string evalExpWithoutBrackets(std::string exp) < std::vectoroperands; std::vector operations; const uint explen = exp.length(); // Allocating arguments and operations without ordering for(uint shift=0, position = 0; shift if( exp[shift] == '+' || exp[shift] == '-' || exp[shift] == '*' || exp[shift] == '/') < std::string expTemp; expTemp.assign(exp, position, shift-position); operands.push_back((T) std::stod(expTemp)); operations.push_back(exp[shift]); std::string tempExp; position = shift+1; for(shift++; shiftif(shift == explen-1) < tempExp.assign(exp, position, explen - position); >> operands.push_back((T)std::stod(tempExp)); position = shift+1; > > // Calculator std::vector evalOrder; // Order of operations uint highPriority = 0, lowPriority = 0; // Ordering operations // First of all we need operations with high priority for(uint i=0; i < operations.size(); i++)< if(operations[i] == '*' || operations[i] == '/')< evalOrder.push_back(i); highPriority++; >> // Now we need to order low priority operations for(uint i=0; i < operations.size(); i++)< if(operations[i] == '-' || operations[i] == '+')< evalOrder.push_back(i); lowPriority++; >> // Evaluating epression by order for(uint i=0; i < evalOrder.size(); i++)< T rexp = (T)NULL; try< rexp = eval(operands[evalOrder[i]], operands[evalOrder[i]+1], operations[evalOrder[i]]); > catch(char const *er) < std::cout // Erasing operations and operands, because operands[evalOrder[i]] and operands[evalOrder[i]+1] // became single argument after completing operations[evalOrder[i]] operation if(evalOrder[i] < operands.size()-1)< operands.erase(operands.begin()+evalOrder[i]+1); operations.erase(operations.begin()+evalOrder[i]); >// Recallculating order for(uint j = i; j < evalOrder.size(); j++)< if(evalOrder[j] >evalOrder[i]) evalOrder[j]--; > // Storing result of eval operands[evalOrder[i]] = rexp; > return std::to_string(operands[0]); > template std::string evalExpression(std::string exp) < uint open = 0, close = 0; for(uint i=0; iif(open != close) return (std::string)"error: Expression have uncoupled brackets"; // Divide expression to the blocks if there are any brackets for(uint closeBracketPosition=0; closeBracketPosition> > > return evalExpWithoutBrackets(exp);; > int main(int argc, char **argv) < std::string evalexp(argv[1]); // Check input expression for unhandling symbols if(!checkExpression(evalexp)) return -1; // Clear expression from spaces for(uint i=0 ; i < evalexp.length(); i++)< if(evalexp[i] == ' ')< evalexp.erase(evalexp.begin() + i); if(i >0) i--; > > std::cout (evalexp)
Собираем на хосте исполняемый файл.
[gazpar@localhost slcalc]$ make cross /home/gazpar/toolchain/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ -Wextra -std=c++11 -march=armv7-a -mcpu=cortex-a5 --sysroot=/home/gazpar/toolchain/sysroot-glibc-linaro-2.21-2016.05-arm-linux-gnueabihf/ slc.cpp -o acalc -static [gazpar@localhost slcalc]$ ls -la drwxrwxr-x. 2 gazpar gazpar 4096 Jan 15 16:35 . drwxrwxr-x. 7 gazpar gazpar 4096 Aug 15 07:56 .. -rwxrwxr-x. 1 gazpar gazpar 9704352 Jan 15 16:35 acalc -rwxrwxrwx. 1 gazpar gazpar 59 Jan 10 22:04 .directory -rwxrwxrwx. 1 gazpar gazpar 469 Jan 14 11:14 Makefile -rwxrwxrwx. 1 gazpar gazpar 4951 Jan 13 21:15 slc.cpp
Отмечу, что проще собрать с ключом -static, если нет особого желания предаваться плотским утехам с библиотеками на целевой платформе.
Копируем исполняемый файл на таргет и проверяем.
[gazpar@localhost slcalc]$ scp -P 10022 acalc arm@localhost:/home/arm/acalc
arm@debian:~$ ./acalc 12*13-11*(21-3) Evaluating expression is: "12*13-11*(21-3)" Result is: -42
Собственно, вот такая она, эта кросскомпиляция.
UPD: Подправил информацию по toolchain’ам по комментарию grossws.
This article illustrates how to install on a Linux PC the complete toolchain to cross compile the Linux Kernel, device drivers and applications for the Acme Systems Linux board.
This procedure has been tested on: Ubuntu 18.04.4 LTS and Debian Buster 10
Install the GCC, G++ cross compilers and support programs by typing:
sudo apt update sudo apt install libc6-armel-cross libc6-dev-armel-cross binutils-arm-linux-gnueabi libncurses5-dev build-essential bison flex libssl-dev bc
If you are using an Acqua or RoadRunner board:
Now you are ready to cross-compile on your PC all the source available for the Acme Boards based on Microchip MPUs.
Let’s try to cross compile a Hello World example in C and running it on an Acme board.
#include "stdio.h" int main(void)
Compile it by typing, if you are using an Arietta, Aria or FOX G20 board:
or, if you are using an Acqua or RoadRunner board:
As you can see we are using the ARM version of gcc just installed on your PC. It will generate an executable file for your Linux board.
Copy the executable file on the board via ssh:
Then open a command session on your board and run the example:
Let’s try to cross compile a Hello World example in C++ and running it on an Acme board.
#include "iostream" using namespace std; int main(int argc, char *argv[])
Compile it typing, if you are using an Arietta, Aria or FOX G20 board:
or, if you are using an Acqua or RoadRunner board:
As you can see we are using the ARM version of gcc just installed on your PC. It will generate an executable file for your Linux board.
Copy the executable file on the board via ssh:
Then open a command session on your board and run the example: