0

I have a Raspberry Pi 4B with Raspberry Pi OS x32. I use Code::blocks and gcc-compiler for the development. I wrote a simple C++ project that contains only one source file and use some libraries. The code is below:

#include <iostream>
#include <chrono>
#include <thread>
#include <wiringPi.h>

using namespace std;

void switchOnLeds(int delay, int firstLed, int lastLed);
void digitalWrite(int pin, bool statement);
void simpleDelay(int cycles);
int calculateOperationTime();
bool lastStatement = false;
const int DELAY_PIN = 14;    //Pin only to delay    

int main()
{
    cout << "In this program I try to control the LED strip without SPI-wire. The LED strip must be connected with the Pin 11/GPIO 17. It is 6 pin on the left side from up. Pin 23/GPIO 11 must be free and not connected" << endl;
    int firstLed = 0;
    int lastLed = 0;
    wiringPiSetup () ;
    for (int i = 0; i < 10; i++){
        cout << "Enter first LED number: " << endl;
        cin >> firstLed;
        cout << "Enter second LED number: " << endl;
        cin >> lastLed;
        int delay = calculateOperationTime();
        cout << "Start to test with delta time: " << delay << "LEDs between " << firstLed << " and " << lastLed << " must be switched on" <<endl;
        switchOnLeds(delay, firstLed, lastLed);
    }
    return 0;
}

void switchOnLeds(int delay, int firstLed, int lastLed) {
    const auto BIT_0_HIGH_VOLTAGE_TIME = (250);    //nanoseconds to start transfer the logical 0 for ws2811 in fast mode
    const auto BIT_0_LOW_VOLTAGE_TIME = (1000);    //nanoseconds to end transfer the logical 0 for ws2811 in fast mode

    const auto BIT_1_HIGH_VOLTAGE_TIME = (600);    //nanoseconds to start transfer the logical 1 for ws2811 in fast mode
    const auto BIT_1_LOW_VOLTAGE_TIME = (650);     //nanoseconds to end transfer the logical 1 for ws2811 in fast mode

    const int CYCLES_0_HIGH = BIT_0_HIGH_VOLTAGE_TIME/delay;
    const int CYCLES_0_LOW = BIT_0_LOW_VOLTAGE_TIME/delay;

    const int CYCLES_1_HIGH = BIT_1_HIGH_VOLTAGE_TIME/delay;
    const int CYCLES_1_LOW = BIT_1_LOW_VOLTAGE_TIME/delay;

    const int BITS_FOR_SINGLE_LED = 24;         //how many bits contains a signal for a single LED
    const int PIN = 0;                          //Output pin
    const int LEDS = 10;


    pinMode (PIN, OUTPUT) ;
    auto programFinish = std::chrono::high_resolution_clock::now();
    auto programStart = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < LEDS; i++) {
        if (i >= firstLed && i <= lastLed) {
            for (int bit = 0; bit < BITS_FOR_SINGLE_LED; bit++) {
                digitalWrite(PIN, HIGH);
                simpleDelay(CYCLES_1_HIGH);
                digitalWrite(PIN, LOW);
                simpleDelay(CYCLES_1_LOW);
            }
        }
        else {
            for (int bit = 0; bit < BITS_FOR_SINGLE_LED; bit++) {
                digitalWrite(PIN, HIGH);
                simpleDelay(CYCLES_0_HIGH);
                digitalWrite(PIN, LOW);
                simpleDelay(CYCLES_0_LOW);
            }
        }
    }
    programFinish = std::chrono::high_resolution_clock::now();
    long operationTime = (std::chrono::duration_cast<std::chrono::nanoseconds>(programFinish - programStart).count())/(BITS_FOR_SINGLE_LED*LEDS) ;

    printf("Completed in ");
    std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(programFinish - programStart).count() << " ns but must be no longer as 300 000 nanoseconds on the Raspberry Pi 3b" << endl;
    std::cout <<"Operation must be 1250 nanoseconds nut it is: "<<operationTime << " nanoseconds" << endl;
}



void simpleDelay(int cycles){
    for (int i = 0; i < cycles; i++){
        if (lastStatement == true){
            digitalWrite(DELAY_PIN, LOW);
            lastStatement = false;
        }
        else{
            digitalWrite(DELAY_PIN, HIGH);
            lastStatement = true;
        }
    }
}

int calculateOperationTime(){
    auto programEnd = std::chrono::high_resolution_clock::now();
    const int steps = 10*24;
    auto programStart = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < steps; i++ ){
        digitalWrite(DELAY_PIN, HIGH);
        digitalWrite(DELAY_PIN, LOW);
    }
    programEnd = std::chrono::high_resolution_clock::now();
    long deltaTime = std::chrono::duration_cast<std::chrono::nanoseconds>(programEnd - programStart).count();
    deltaTime/=(steps*2);
    return deltaTime;
}

I need to send this code to my customer for tests. He has a Raspberry Pi 3B with the same operating system. I can launch the compiled file from the release directory of the project using simple command in the terminal: sudo ./FileName .But when I try to launch this compiled file on a clear fresh installed Raspberry Pi OS 32 bit it can not be launched. The terminal says: sudo ./FileName: command not found. I need to install wiringPi and Code::blocks, compile the file from the IDE and after that I can launch the compiled file. How can I export the compiled file as executable for Raspberry Pi OS 32 bit (ARM-architecture)? How can I make executable in C++ for Linux ARM?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Does a clean install have a C++ environment and the library? C++ programs are not self-contained by default. You could experiment with a static build, but there's still no guarantee that there won't be dependencies. The simplest thing would be to document the dependencies required, and provide a build script. Better still, have your build script fetch the dependencies. – sweenish May 02 '23 at 19:02
  • A proper C++ environment would still be required on the destination; it might not have been clear. – sweenish May 02 '23 at 19:03
  • The clear Raspberry Pi OS image contains gcc-compiler. If I right understood the user can simple open the text editor nano, write a code, save the file with the extension: cpp and launch the compiler. The library WiringPi is not installed as default in the operating system. – Alexander Gorodilov May 02 '23 at 19:18
  • It could also be the file compiled on the 4B uses a different interpreter, and that can't be found. Check to see what the executable expects with `file FileName` and see what comes up. Then compare that against a hello world compiled on the 3B. – user4581301 May 02 '23 at 19:21
  • @user4581301 you don't understood or didn't good read: I try to explain again: 1) I compile my code in a file, that runs perfect. 2) I save this file in a cloud 3) I format my SD card with the operating system for the Raspberry Pi 4B. 4) I install again the same operating system on the same SD card. 5) I plug the SD card in my Raspberry Pi 4B again 6) On the fresh OS I load the compiled file from the cloud and run it. It doesn't work. All this operations are made on the same Raspberry Pi 4B and the same official OS image – Alexander Gorodilov May 02 '23 at 19:33
  • That I understood. I misread the question and thought the problem was only on the client side, RPi 3B. – user4581301 May 02 '23 at 20:14
  • `sudo ./Filename` outputting "command not found" could mean that either `sudo` is not found or that `Filename` does not exist. Which is it? This does not (yet) have anything to do with C++. And why are you running this command as root? – Botje May 02 '23 at 20:29
  • @Botje 1) sudo is found on every Linux distribution I have tested. I can delete sudo and try only: ./FileName. It is not important. 2) Filename exists - the trouble is in this two characters: ./ (dot and slash). I'm new in C++ programming, I have only 5 years Java experience and can not understand what is that. 3) It is not important to call as root. When I wrote the same code on Java I had to launch the code only as root because the java virtual machine need to communicate with the GPIO and must have root-privilegies. I didn't find the info but I think C++ code needn't to be launched as root – Alexander Gorodilov May 03 '23 at 05:22
  • If `Filename` exists in the current directory then `./Filename` should at least produce an error. Please edit your post with proof that `Filename` exists in the current directory and the exact error that calling `sudo ./Filename` produces. Also, fix the permissions of the GPIO device and/or add your user to the right group instead of using the bazooka that is sudo. – Botje May 03 '23 at 07:22
  • today when I try to launch the same file on the clear operating system I got the error: ./FileName: error while loading shared libraries: libwiringPi.so: cannot open shared object file: No such file or directory. If I right understood the library libwiringPi.so was not added to the compiled file. How can I compile the code with the library for deployment? – Alexander Gorodilov May 03 '23 at 19:35
  • You need to ship a copy of libwiringpi.so along with your program. Static linking (eg embedding it in your program) is only allowed if you satisfy the [requirements of the LGPL](https://github.com/WiringPi/WiringPi/blob/master/COPYING.LESSER) – Botje May 04 '23 at 14:09

0 Answers0