0

I am trying to build applications for my Raspberry Pi using a cross compiler by using crosstool-ng. My build system is Ubuntu 14.04 LTS.

When I tried building a test program, it works.

On build system:

$ cat > prog_without_so.cpp
#include <iostream>
int main() { std::cout << "Hello, world!\n"; return 0; }
^D
$ arm-unknown-linux-gnueabi-gcc -std=c++0x  -lstdc++ -o prog_without_so prog_without_so.cpp

On Pi:

$ ./prog_without_so
Hello, world!

However, when I tried to run a program that uses shared libraries, it returns this:

On Pi:

$ LD_LIBRARY_PATH="." ./prog_with_so 
./prog_with_so: error while loading shared libraries: librsmath.so: cannot open shared object file: No such file or directory

Curious, I tried ldd and file:

On Pi:

$ ldd prog_with_so
    not a dynamic executable

$ file ./prog_with_so
./prog_with_so: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 4.3.0, not stripped

Also tried the same on the first program:

$ ldd prog_without_so
    not a dynamic executable

$ file ./prog_without_so
./prog_without_so: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 4.3.0, not stripped

I wasn't able to find anything new here.

What's going on? Is my cross-compiler incorrectly set up? Why does it fail only when the application depends on a shared library?

This is my source for the second program:

//somemath.h
#include <iostream>
class CSum {
    int _sum;
public:
    CSum(int i):_sum(i) {}
    CSum operator+=(int i);
    friend std::ostream& operator<<( std::ostream& out, const CSum& sum); // output;
};

//somemath.cpp
#include "somemath.h"

CSum CSum::operator+=(int i)
{
    _sum += i;
}

std::ostream& operator<<( std::ostream& out, const CSum& sum) // output
{
    out << "(" << sum._sum << ")";
    return out;
}

//test.cpp
#include <iostream>
#include <cstdlib>
#include "somemath.h"

CSum sum(1);
int main(int argc, char* argv[])
{
    if (argc>1)
    {
        int n = 1;
        while(n<argc)
        {
            sum+=atoi(argv[n]);
            std::cout<< sum << " ";
            ++n;
        }
        std::cout << "\n";
    } 
    else
    {
        std::cout <<"Usage: " << argv[0] << " [2 3 4 ... ]\n";
    }
    return 0;
}

Here's how I compile and link them on my build system:

arm-unknown-linux-gnueabi-gcc -shared somemath.cpp -std=c++0x -fPIC -lstdc++ -o librsmath.so 
arm-unknown-linux-gnueabi-gcc test.cpp -o prog_with_so -std=c++0x -L ./  -lstdc++ -lrsmath

1 Answer 1

0

I later found that crosstool-ng has provided some samples which can be used.

ct-ng list-samples

I selected the configuration for pi 2, which works very well. :-)

ct-ng armv7-rpi2-linux-gnueabihf
ct-ng build

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.