how to compile blacklib (c++)

2019-07-23 22:29发布

问题:

I am running a beaglebone and want to write a program to sample the ADC. I am trying to use the blacklib (http://blacklib.yigityuce.com/index.html) from here. I cloned the git: https://github.com/yigityuce/BlackLib

and tried to compile the example with

g++ exampleAndTiming.cpp -std=c++11

This however gives me a ton of errors like these:

In file included from exampleAndTiming.cpp:33:0:
exampleAndTiming/exampleAndTiming_GPIO.h: In function 'void exampleAndTiming_GPIO()':
exampleAndTiming/exampleAndTiming_GPIO.h:97:12: error: 'sleep' was not declared in this scope
     sleep(1);
            ^
In file included from exampleAndTiming.cpp:34:0:
exampleAndTiming/exampleAndTiming_ADC.h: In function 'void exampleAndTiming_ADC()':
exampleAndTiming/exampleAndTiming_ADC.h:67:16: error: 'usleep' was not declared in this scope
     usleep(1000);
                ^

so I include unistd.h (in exampleAndTiming.cpp), but then I get errors like these:

/tmp/ccbgiXE9.o: In function `exampleAndTiming_GPIO()':
exampleAndTiming.cpp:(.text+0x50): undefined reference to `Timing::startMeasure(std::string)'
exampleAndTiming.cpp:(.text+0x80): undefined reference to `BlackLib::BlackGPIO::BlackGPIO(BlackLib::gpioName, BlackLib::direction, BlackLib::workingMode)'
exampleAndTiming.cpp:(.text+0xbc): undefined reference to `Timing::endMeasure(std::string)'
exampleAndTiming.cpp:(.text+0xec): undefined reference to `BlackLib::BlackGPIO::BlackGPIO(BlackLib::gpioName, BlackLib::direction, BlackLib::workingMode)'
exampleAndTiming.cpp:(.text+0x104): undefined reference to `BlackLib::BlackGPIO::BlackGPIO(BlackLib::gpioName, BlackLib::direction, BlackLib::workingMode)'
exampleAndTiming.cpp:(.text+0x11c): undefined reference to `BlackLib::BlackGPIO::BlackGPIO(BlackLib::gpioName, BlackLib::direction, BlackLib::workingMode)'
exampleAndTiming.cpp:(.text+0x158): undefined reference to `Timing::startMeasure(std::string)'

I've been looking at some library examples and compiling it, but I cannot make sense of it all. I've compiled plenty of c++ and c programs before, but I can't get this one to work. So any help will be appreciated.

回答1:

COMPLETE GUIDE how to compile BLACKLIB directly on BEAGLEBONE BLACK (rev C) running ANGSTROM:

Programs:

Putty - to communicate with BBB from Windows (using SSH with USB cable)

WinSCP - to manage (upload, create, delete) files directly on BBB

Code::Blocks - to write C++ programs

optionally

Termite 2.9 - to send and receive UART transmission from UART<->USB converter (actually Putty could be used to do that as well)

1) get the BlackLib from official site

2) unzip the library and copy following files into separate folder :

BlackADC.cpp, BlackADC.h, BlackCore.cpp, BlackCore.h, BlackDef.h, BlackErr.h, BlackGPIO.cpp, BlackGPIO.h, BlackI2C.cpp, BlackI2C.h, BlackLib.h, BlackPWM.cpp, BlackPWM.h, BlackSPI.cpp, BlackSPI.h, BlackUART.cpp, BlackUART.h

3) open following files in Code::Blocks BlackUART.cpp, BlackSPI.cpp, BlackI2C.cpp and add

#include <unistd.h>

right after #include "BlackUART.h", the "unistd.h" includes all the functions like sleep(), open(), close(), ... that otherwise seems missing

4) create your own program main.cpp, you may use the following code for testing UART1 and UART2:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sstream>
#include <string>
#include <iostream>


#include "BlackLib.h"


int main(){

    std::string writeToUart1;
    std::string writeToUart2;
    std::string readFromUart1;
    std::string readFromUart2;
    int counter;
    std::ostringstream os1;
    std::ostringstream os2;

    BlackLib::BlackUART  Uart1(BlackLib::UART1,
                               BlackLib::Baud9600,
                               BlackLib::ParityEven,
                               BlackLib::StopOne,
                               BlackLib::Char8 );
    // Pins on BeagleBone Black REV C
    // UART1_RX -> GPIO_15 (P9.24)
    // UART1_RX -> GPIO_14 (P9.26)

    BlackLib::BlackUART  Uart2(BlackLib::UART2,
                               BlackLib::Baud9600,
                               BlackLib::ParityEven,
                               BlackLib::StopOne,
                               BlackLib::Char8 );
    // Pins on BeagleBone Black REV C
    // UART2_RX -> GPIO_2 (P9.22)
    // UART2_RX -> GPIO_3 (P9.21)

    std::cout << "Program UART start" << std::endl << std::flush;

    Uart1.open( BlackLib::ReadWrite | BlackLib::NonBlock );
    Uart2.open( BlackLib::ReadWrite | BlackLib::NonBlock );

    counter = 0;

    while (true){

        os1.str("");
        os1.clear();
        os1 << "Uart1 to TX: " << counter << "\n";
        writeToUart1 = os1.str();
        Uart1 << writeToUart1;

        readFromUart1 = "";
        Uart1 >> readFromUart1;
        if (readFromUart1.compare("") != 0){
                std::cout << "Uart1 from RX: " << readFromUart1 << "\n" << std::flush;
        }
        Uart1.flush( BlackLib::bothDirection );
        counter++;
        sleep(2);


        os2.str("");
        os2.clear();
        os2 << "Uart2 to TX: " << counter << "\n";
        writeToUart2 = os2.str();
        Uart2 << writeToUart2;

        readFromUart2 = "";
        Uart2 >> readFromUart2;
        if (readFromUart2.compare("") != 0){
                std::cout << "Uart2 from RX: " << readFromUart2 << "\n" << std::flush;
        }
        Uart2.flush( BlackLib::bothDirection );
        counter++;
        sleep(2);


    }

    return 1;
}

5) save the main.cpp to the same folder as the BlackLib files

6) using WinSCP, create directory on the BBB (e.g. /home/uart) and copy all the BlackLib files and main.cpp into this folder

7) open Putty and navigate to the folder by : cd /home/uart

8) compile the files by using : gcc *.cpp -o main -std=c++11

9) run the program : ./main

10) connect the wires to UART<->USB converter and BBB. The ouput from BBB should look like :

Uart2 to TX: 1   OR   Uart1 to TX: 0
Uart2 to TX: 3   OR   Uart1 to TX: 2

depending on connection of wires



回答2:

It seems I managed to fix it myself, some nooblike behaviour not including all the cpp files, but even more, I also needed to add #include to BlackCore.h to avoid tons of undefined function errors.

final command:

g++ exampleAndTiming.cpp exampleAndTiming/Timing.cpp BlackADC.cpp BlackCore.cpp BlackGPIO.cpp BlackI2C.cpp BlackPWM.cpp BlackSPI.cpp BlackUART.cpp -std=c++11

I'd probably need to make a makefile to compile the library seperately, time to do some more digging and learning.



回答3:

I am the creator of BlackLib, Yiğit YÜCE. You found your answer by yourself. The makefile which you mentioned on your comment will be published shortly.